Inheritance: Element, ICloneable
Esempio n. 1
0
        public virtual int Add(Target t){
            // throw an exception if an attempt is made to add a null target
            if (t == null) {
                throw new BuildException("Null Target!");
            }

            logger.DebugFormat(CultureInfo.InvariantCulture,
                ResourceUtils.GetString("String_AddingTarget"), 
                t.Name);

            // check for existing target with same name.
            if (Find(t.Name) == null) {
                return base.Add(t);
            } else {
                throw new BuildException(string.Format(CultureInfo.InvariantCulture, 
                    ResourceUtils.GetString("NA1073"), t.Name));
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BuildEventArgs" />
 /// class for a <see cref="Task" /> level event.
 /// </summary>
 /// <param name="task">The <see cref="Task" /> that emitted the event.</param>
 public BuildEventArgs(Task task)
 {
     _project = task.Project;
     _target = task.Parent as Target;
     _task = task;
 }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BuildEventArgs" />
 /// class for a <see cref="Target" /> level event.
 /// </summary>
 /// <param name="target">The <see cref="Target" /> that emitted the event.</param>
 public BuildEventArgs(Target target)
 {
     _project = target.Project;
     _target = target;
 }
Esempio n. 4
0
        /// <summary>
        /// Executes a specific target.
        /// </summary>
        /// <param name="targetName">The name of the target to execute.</param>
        /// <param name="forceDependencies">Whether dependencies should be forced to execute</param>
        /// <remarks>
        /// Global tasks are not executed.
        /// </remarks>
        public void Execute(string targetName, bool forceDependencies)
        {
            // sort the dependency tree, and run everything from the
            // beginning until we hit our targetName.
            //
            // sorting checks if all the targets (and dependencies)
            // exist, and if there is any cycle in the dependency
            // graph.
            TargetCollection sortedTargets = TopologicalTargetSort(targetName, Targets);
            int currentIndex = 0;
            Target currentTarget;

            // store calling target
            Target callingTarget = _currentTarget;

            do {
                // determine target that should be executed
                currentTarget = (Target) sortedTargets[currentIndex++];

                // store target that will be executed
                _currentTarget = currentTarget;

                // only execute targets that have not been executed already, if
                // we are not forcing.
                if (forceDependencies || !currentTarget.Executed || currentTarget.Name == targetName) {
                    currentTarget.Execute();
                }
            } while (currentTarget.Name != targetName);

            // restore calling target, as a <call> task might have caused the
            // current target to be executed and when finished executing this
            // target, the target that contained the <call> task should be
            // considered the current target again
            _currentTarget = callingTarget;
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a new <see cref="Task" /> from the given <see cref="XmlNode" /> 
        /// within a <see cref="Target" />.
        /// </summary>
        /// <param name="taskNode">The <see cref="Task" /> definition.</param>
        /// <param name="target">The owner <see cref="Target" />.</param>
        /// <returns>The new <see cref="Task" /> instance.</returns>
        public Task CreateTask(XmlNode taskNode, Target target)
        {
            Task task = TypeFactory.CreateTask(taskNode, this);

            task.Project = this;
            task.Parent = target;
            task.NamespaceManager = NamespaceManager;
            task.Initialize(taskNode);
            return task;
        }
Esempio n. 6
0
        /// <summary>
        /// This method is only meant to be used by the <see cref="Project"/> 
        /// class and <see cref="T:NAnt.Core.Tasks.IncludeTask"/>.
        /// </summary>
        internal void InitializeProjectDocument(XmlDocument doc)
        {
            // load line and column number information into position map
            LocationMap.Add(doc);

            // initialize targets first
            foreach (XmlNode childNode in doc.DocumentElement.ChildNodes) {
                // skip non-nant namespace elements and special elements like
                // comments, pis, text, etc.
                if (childNode.LocalName.Equals(TargetXml) && childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant"))) {
                    Target target = new Target();

                    target.Project = this;
                    target.Parent = this;
                    target.NamespaceManager = NamespaceManager;
                    target.Initialize(childNode);
                    Targets.Add(target);
                }
            }

            // initialize datatypes and execute global tasks
            foreach (XmlNode childNode in doc.DocumentElement.ChildNodes) {
                // skip targets that were handled above, skip non-nant namespace
                // elements and special elements like comments, pis, text, etc.
                if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant")) || childNode.LocalName.Equals(TargetXml)) {
                    continue;
                }

                if (TypeFactory.TaskBuilders.Contains(childNode.Name)) {
                    // create task instance
                    Task task = CreateTask(childNode);
                    task.Parent = this;
                    // execute task
                    task.Execute();
                } else if (TypeFactory.DataTypeBuilders.Contains(childNode.Name)) {
                    // we are an datatype declaration
                    DataTypeBase dataType = CreateDataTypeBase(childNode);

                    Log(Level.Debug, "Adding a {0} reference with id '{1}'.", childNode.Name, dataType.ID);
                    if (! DataTypeReferences.Contains(dataType.ID)) {
                        DataTypeReferences.Add(dataType.ID, dataType);
                    } else {
                        DataTypeReferences[dataType.ID] = dataType; // overwrite with the new reference.
                    }
                } else {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1071"), childNode.Name),
                        LocationMap.GetLocation(childNode));
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Writes a <see cref="Target" /> level message to the build log with 
        /// the given <see cref="Level" />.
        /// </summary>
        /// <param name="target">The <see cref="Target" /> from which the message orignated.</param>
        /// <param name="messageLevel">The level to log at.</param>
        /// <param name="message">The message to log.</param>
        public void Log(Target target, Level messageLevel, string message)
        {
            BuildEventArgs eventArgs = new BuildEventArgs(target);

            eventArgs.Message = message;
            eventArgs.MessageLevel = messageLevel;
            OnMessageLogged(eventArgs);
        }
Esempio n. 8
0
        /// <summary>
        /// Executes a specific target.
        /// </summary>
        /// <param name="targetName">The name of the target to execute.</param>
        /// <param name="forceDependencies">Whether dependencies should be forced to execute</param>
        /// <remarks>
        /// Global tasks are not executed.
        /// </remarks>
        public void Execute(string targetName, bool forceDependencies)
        {
            bool singleThreaded = !RunTargetsInParallel;
            if (singleThreaded) {
                // sort the dependency tree, and run everything from the
                // beginning until we hit our targetName.
                //
                // sorting checks if all the targets (and dependencies)
                // exist, and if there is any cycle in the dependency
                // graph.
                TargetCollection sortedTargets = TopologicalTargetSort(targetName, Targets);
                int currentIndex = 0;
                Target currentTarget;

                // store calling target
                Target callingTarget = _currentTarget;

                do {
                    // determine target that should be executed
                    currentTarget = (Target) sortedTargets[currentIndex++];

                    // store target that will be executed
                    _currentTarget = currentTarget;

                    // only execute targets that have not been executed already, if
                    // we are not forcing.
                    if (forceDependencies || !currentTarget.Executed || currentTarget.Name == targetName) {
                        currentTarget.Execute();
                    }
                } while (currentTarget.Name != targetName);

                // restore calling target, as a <call> task might have caused the
                // current target to be executed and when finished executing this
                // target, the target that contained the <call> task should be
                // considered the current target again
                _currentTarget = callingTarget;
            }
            else {
                // sorting checks if all the targets (and dependencies)
                // exist, and if there is any cycle in the dependency
                // graph.
                TopologicalTargetSort (targetName, Targets);

                // Dictionary is faster than implementation in TargetCollection.Find
                System.Collections.Generic.Dictionary<string, Target> targets = new System.Collections.Generic.Dictionary<string, Target>();
                foreach (Target target in Targets) {
                    targets [target.Name] = target;
                }

                // Use execution graph to run targets in parallel
                using (ExecutionGraph graph = new ExecutionGraph()) {

                    PopulateExecutionGraph(targetName, Targets, graph);
                    graph.WalkThrough(delegate(string currentTargetName) {

                        Target currentTarget;
                        if (!targets.TryGetValue(currentTargetName, out currentTarget)) {
                            Target wildcardTarget = targets [WildTarget];
                            currentTarget = wildcardTarget.Clone ();
                            currentTarget.Name = targetName;
                        }

                        Target savedTarget = _currentTarget;
                        _currentTarget = currentTarget;

                        try {
                            lock (currentTarget) {
                                if (forceDependencies || !currentTarget.Executed || currentTarget.Name == targetName) {
                                    currentTarget.Execute ();
                                }
                            }
                        } finally {
                            _currentTarget = savedTarget;
                        }
                    }
                    );
                }
            }
        }
Esempio n. 9
0
 private BuildEventArgs CreateBuildEventArgsWithTarget(string targetName) {
     Target target = new Target();
     target.Name = targetName;
     return new BuildEventArgs(target);
 }
Esempio n. 10
0
 /// <summary>
 /// Creates a shallow copy of the <see cref="Target" />.
 /// </summary>
 /// <returns>
 /// A shallow copy of the <see cref="Target" />.
 /// </returns>
 public Target Clone() {
     Target clone = new Target();
     base.CopyTo(clone);
     clone._dependencies = _dependencies;
     clone._description = _description;
     clone._executed = _executed;
     clone._ifCondition = _ifCondition;
     clone._name = _name;
     clone._unlessCondition = _unlessCondition;
     return clone;
 }
Esempio n. 11
0
        private void SetDefaultTarget(Target targ, Project proj)
        {
            proj.Targets.Add(targ);

            //Replace any existing build targets with this target
            proj.BuildTargets.Clear();
            proj.BuildTargets.Add(targ.Name);
        }
Esempio n. 12
0
        private Target CreateMultitaskTarget(Project proj, String multitaskTargetName)
        {
            //Create a new target which will contain the tasks from the <multitask>
            //To preserve line numbers as much as possible, build the Target object
            //from the XmlNode for this task; it shouldn't work, but it does, since
            //the Initialize() method for each NAnt element assumes the node being passed to it
            //is the right type.

            //Change the 'name' property so the 'target' will be identifiable
            XmlAttribute name = XmlNode.Attributes["name"];
            String oldName = null;
            if (name == null) {
                name = XmlNode.OwnerDocument.CreateAttribute("name");
            } else {
                oldName = name.Value;
            }

            try {
                name.Value = multitaskTargetName;
                XmlNode.Attributes.Append(name);

                Target targ = new Target();
                targ.Project = proj;
                targ.NamespaceManager = NamespaceManager;
                targ.Initialize(XmlNode);

                return targ;
            } finally {
                //Restore the previous name
                if (oldName == null) {
                    XmlNode.Attributes.Remove(name);
                } else {
                    name.Value = oldName;
                }
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BuildEventArgs" />
 /// class for a <see cref="Project" /> level event.
 /// </summary>
 /// <param name="project">The <see cref="Project" /> that emitted the event.</param>
 public BuildEventArgs(Project project)
 {
     _project = project;
     _target = null;
     _task = null;
 }
Esempio n. 14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BuildEventArgs" /> 
 /// class.
 /// </summary>
 public BuildEventArgs()
 {
     _project = null;
     _target = null;
     _task = null;
 }