2 // BuildTask.cs: Represents a Task element in a project.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
34 using System.Reflection;
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Utilities;
39 namespace Microsoft.Build.BuildEngine {
40 public class BuildTask {
44 XmlElement taskElement;
45 TaskLoggingHelper task_logger;
47 internal BuildTask (XmlElement taskElement, Target parentTarget)
49 if (taskElement == null)
50 throw new ArgumentNullException ("taskElement");
51 if (parentTarget == null)
52 throw new ArgumentNullException ("parentTarget");
54 this.taskElement = taskElement;
55 this.parentTarget = parentTarget;
59 public void AddOutputItem (string taskParameter,
62 XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
63 taskElement.AppendChild (element);
65 if (taskParameter != null)
66 element.SetAttribute ("TaskParameter", taskParameter);
68 element.SetAttribute ("ItemName", itemName);
72 public void AddOutputProperty (string taskParameter,
75 XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
76 taskElement.AppendChild (element);
78 if (taskParameter != null)
79 element.SetAttribute ("TaskParameter", taskParameter);
80 if (propertyName != null)
81 element.SetAttribute ("PropertyName", propertyName);
85 public bool Execute ()
88 TaskEngine taskEngine;
94 task = InitializeTask ();
95 } catch (Exception e) {
96 LogError ("Error initializing task {0}: {1}", taskElement.LocalName, e.Message);
97 LogMessage (MessageImportance.Low, "Error initializing task {0}: {1}",
98 taskElement.LocalName, e.ToString ());
103 taskEngine = new TaskEngine (parentTarget.Project);
104 taskEngine.Prepare (task, this.taskElement, GetParameters (), this.Type);
105 result = taskEngine.Execute ();
107 taskEngine.PublishOutput ();
108 } catch (Exception e) {
109 task_logger.LogError ("Error executing task {0}: {1}", taskElement.LocalName, e.Message);
110 task_logger.LogMessage (MessageImportance.Low,
111 "Error executing task {0}: {1}", taskElement.LocalName, e.ToString ());
115 LogTaskFinished (result);
121 public string[] GetParameterNames ()
123 List <string> tempNames = new List <string> ();
125 foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
126 if (xmlAttribute.Name == "Condition" || xmlAttribute.Name == "ContinueOnError")
128 tempNames.Add (xmlAttribute.Name);
131 return tempNames.ToArray ();
134 public string GetParameterValue (string attributeName)
136 if (attributeName == "Condition")
137 throw new ArgumentException ("Condition attribute cannot be accessed using this method.");
138 if (attributeName == "ContinueOnError")
139 throw new ArgumentException ("ContinueOnError attribute cannot be accessed using this method.");
141 return taskElement.GetAttribute (attributeName);
144 public void SetParameterValue (string parameterName,
145 string parameterValue)
147 SetParameterValue (parameterName, parameterValue, false);
150 public void SetParameterValue (string parameterName,
151 string parameterValue,
152 bool treatParameterValueAsLiteral)
154 if (treatParameterValueAsLiteral)
155 taskElement.SetAttribute (parameterName, Utilities.Escape (parameterValue));
157 taskElement.SetAttribute (parameterName, parameterValue);
160 void LogTaskStarted ()
162 TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null,
163 parentTarget.Project.FullFileName,
164 parentTarget.TargetFile, taskElement.Name);
165 parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
168 void LogTaskFinished (bool succeeded)
170 TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null,
171 parentTarget.Project.FullFileName,
172 parentTarget.TargetFile, taskElement.Name, succeeded);
173 parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
176 void LogError (string message,
177 params object[] messageArgs)
179 parentTarget.Project.ParentEngine.LogError (message, messageArgs);
182 void LogMessage (MessageImportance importance,
184 params object[] messageArgs)
186 parentTarget.Project.ParentEngine.LogMessage (importance, message, messageArgs);
189 ITask InitializeTask ()
194 task = (ITask)Activator.CreateInstance (this.Type);
195 } catch (InvalidCastException) {
196 LogMessage (MessageImportance.Low, "InvalidCastException, ITask: {0} Task type: {1}",
197 typeof (ITask).AssemblyQualifiedName, this.Type.AssemblyQualifiedName);
200 parentTarget.Project.ParentEngine.LogMessage (
201 MessageImportance.Low,
202 "Using task {0} from {1}", Name, this.Type.AssemblyQualifiedName);
204 task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, parentTarget.Project,
205 parentTarget.TargetFile, 0, 0, ContinueOnError);
206 task_logger = new TaskLoggingHelper (task);
211 IDictionary <string, string> GetParameters ()
213 Dictionary <string, string> parameters = new Dictionary <string, string> ();
215 string[] parameterNames = GetParameterNames ();
217 foreach (string s in parameterNames)
218 parameters.Add (s, GetParameterValue (s));
223 public string Condition {
225 return taskElement.GetAttribute ("Condition");
228 taskElement.SetAttribute ("Condition", value);
233 public bool ContinueOnError {
235 string str = taskElement.GetAttribute ("ContinueOnError");
236 if (str == String.Empty)
239 Expression exp = new Expression ();
240 exp.Parse (str, ParseOptions.AllowItemsNoMetadataAndSplit);
241 return (bool) exp.ConvertTo (parentTarget.Project, typeof (bool),
242 ExpressionOptions.ExpandItemRefs);
246 taskElement.SetAttribute ("ContinueOnError", value.ToString ());
251 public ITaskHost HostObject {
252 get { return hostObject; }
253 set { hostObject = value; }
257 get { return taskElement.Name; }
260 internal Target ParentTarget {
261 get { return parentTarget; }
262 set { parentTarget = value; }
265 internal XmlElement TaskElement {
266 get { return taskElement; }
267 set { taskElement = value; }
272 get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }