/// <summary> /// Creates and executes the embedded (child XML nodes) elements. /// </summary> protected virtual void ExecuteChildTasks() { foreach (XmlNode childNode in XmlNode) { //we only care about xmlnodes (elements) that are of the right namespace. if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant"))) { continue; } // ignore any private xml elements (by def. this includes any property with a BuildElementAttribute (name). if (IsPrivateXmlElement(childNode)) { continue; } if (TypeFactory.TaskBuilders.Contains(childNode.Name)) { // create task instance Task task = CreateChildTask(childNode); // for now, we should assume null tasks are because of // incomplete metadata about the XML if (task != null) { task.Parent = this; // execute task task.Execute(); } } else if (TypeFactory.DataTypeBuilders.Contains(childNode.Name)) { // we are an datatype declaration DataTypeBase dataType = CreateChildDataTypeBase(childNode); Log(Level.Debug, "Adding a {0} reference with id '{1}'.", childNode.Name, dataType.ID); if (!Project.DataTypeReferences.Contains(dataType.ID)) { Project.DataTypeReferences.Add(dataType.ID, dataType); } else { Project.DataTypeReferences[dataType.ID] = dataType; // overwrite with the new reference. } } else { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1071"), childNode.Name), Project.GetLocation(childNode)); } } }
/// <summary> /// Creates and executes the embedded (child XML nodes) elements. /// </summary> /// <remarks> /// Skips any element defined by the host <see cref="Task" /> that has /// a <see cref="BuildElementAttribute" /> defined. /// </remarks> protected virtual void ExecuteChildTasks() { foreach (XmlNode childNode in XmlNode) { //we only care about xmlnodes (elements) that are of the right namespace. if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant"))) { continue; } // ignore any private xml elements (by def. this includes any property with a BuildElementAttribute (name). if (IsPrivateXmlElement(childNode)) { continue; } if (TypeFactory.TaskBuilders.Contains(childNode.Name)) { Task task = CreateChildTask(childNode); if (task != null) { task.Parent = this; task.Execute(); } } else if (TypeFactory.DataTypeBuilders.Contains(childNode.Name)) { DataTypeBase dataType = Project.CreateDataTypeBase(childNode); Project.Log(Level.Verbose, "Adding a {0} reference with id '{1}'.", childNode.Name, dataType.ID); if (!Project.DataTypeReferences.Contains(dataType.ID)) { Project.DataTypeReferences.Add(dataType.ID, dataType); } else { Project.DataTypeReferences[dataType.ID] = dataType; // overwrite with the new reference. } } else { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1071"), childNode.Name), Project.LocationMap.GetLocation(childNode)); } if (BreakTask.Break) { break; } } }
/// <summary> /// Executes dependent targets first, then the target. /// </summary> public virtual void Execute() { if (IfDefined && !UnlessDefined) { try { Project.OnTargetStarted(this, new BuildEventArgs(this)); // select all the task nodes and execute them foreach (XmlNode childNode in XmlNode) { if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant"))) { continue; } if (TypeFactory.TaskBuilders.Contains(childNode.Name)) { Task task = Project.CreateTask(childNode, this); if (task != null) { task.Execute(); } } else if (TypeFactory.DataTypeBuilders.Contains(childNode.Name)) { DataTypeBase dataType = Project.CreateDataTypeBase(childNode); Project.Log(Level.Verbose, "Adding a {0} reference with id '{1}'.", childNode.Name, dataType.ID); if (!Project.DataTypeReferences.Contains(dataType.ID)) { Project.DataTypeReferences.Add(dataType.ID, dataType); } else { Project.DataTypeReferences[dataType.ID] = dataType; // overwrite with the new reference. } } else { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1071"), childNode.Name), Project.LocationMap.GetLocation(childNode)); } } } finally { _executed = true; Project.OnTargetFinished(this, new BuildEventArgs(this)); } } }
private void ExecuteInProjectDirectory(Task task) { string oldBaseDir = SolutionTask.Project.BaseDirectory; SolutionTask.Project.BaseDirectory = ProjectDirectory.FullName; try { // increment indentation level task.Project.Indent(); // execute task task.Execute(); } finally { // restore original base directory SolutionTask.Project.BaseDirectory = oldBaseDir; // restore indentation level task.Project.Unindent(); } }
/// <summary> /// Executes this target /// </summary> /// <param name="callStack">The current call stack on which this target will be pused</param> /// <param name="logger">The logger this target and its stasks will use for logging messages</param> /// <param name="arguments">Optionally, the arguments to provide to the target. Should match those required by <see cref="Parameters"/></param> /// <exception cref="ArgumentException">If one of the non-defaulted parameters is not satisfied by an argument.</exception> private void DoExecute(TargetCallStack callStack, ITargetLogger logger, IList <CallArgument> arguments = null) { var propertyAccessor = new PropertyAccessor(this.Project, callStack); var sw = Stopwatch.StartNew(); if (IfDefined(propertyAccessor) && !UnlessDefined(propertyAccessor)) { try { using (callStack.Push(this, logger)) { this.PrepareArguments(arguments, callStack); Project.OnTargetStarted(this, new TargetBuildEventArgs(this, sw)); logger.OnTargetLoggingStarted(this, new TargetBuildEventArgs(this, sw)); var paramtersDone = false; // select all the task nodes and execute them foreach (XmlNode childNode in XmlNode) { if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant"))) { continue; } if (childNode.Name.Equals("parameters")) { if (paramtersDone) { throw new BuildException("parameters must appear before all tasks", this.Location); } continue; } else { paramtersDone = true; if (TypeFactory.TaskBuilders.Contains(childNode.Name)) { Task task = Project.CreateTask(childNode, this, callStack); if (task != null) { task.Execute(); } } else if (TypeFactory.DataTypeBuilders.Contains(childNode.Name)) { DataTypeBase dataType = Project.CreateDataTypeBase(childNode, callStack); logger.Log(Level.Verbose, "Adding a {0} reference with id '{1}'.", childNode.Name, dataType.ID); if (!Project.DataTypeReferences.Contains(dataType.ID)) { Project.DataTypeReferences.Add(dataType.ID, dataType); } else { Project.DataTypeReferences[dataType.ID] = dataType; // overwrite with the new reference. } } else { throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1071"), childNode.Name), Project.LocationMap.GetLocation(childNode)); } } } } } finally { _executed = true; sw.Stop(); Project.OnTargetFinished(this, new TargetBuildEventArgs(this, sw)); logger.OnTargetLoggingFinished(this, new TargetBuildEventArgs(this, sw)); } } }
/// <summary> /// Executes a task and returns the console output as a string. /// </summary> /// <param name="task">The task to execute.</param> /// <returns> /// The console output. /// </returns> /// <remarks> /// Any exception that is thrown as part of the execution of the /// <see cref="Task" /> is wrapped in a <see cref="TestBuildException" />. /// </remarks> public static string ExecuteTask(Task task) { using (ConsoleCapture c = new ConsoleCapture()) { string output = null; try { task.Execute(); } catch (Exception e) { output = c.Close(); throw new TestBuildException("Error Executing Task", output, e); } finally { if (output == null) { output = c.Close(); } } return output; } }