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;
46 internal BuildTask (XmlElement taskElement, Target parentTarget)
48 if (taskElement == null)
49 throw new ArgumentNullException ("taskElement");
50 if (parentTarget == null)
51 throw new ArgumentNullException ("parentTarget");
53 this.taskElement = taskElement;
54 this.parentTarget = parentTarget;
58 public void AddOutputItem (string taskParameter,
61 XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
62 taskElement.AppendChild (element);
64 if (taskParameter != null)
65 element.SetAttribute ("TaskParameter", taskParameter);
67 element.SetAttribute ("ItemName", itemName);
71 public void AddOutputProperty (string taskParameter,
74 XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
75 taskElement.AppendChild (element);
77 if (taskParameter != null)
78 element.SetAttribute ("TaskParameter", taskParameter);
79 if (propertyName != null)
80 element.SetAttribute ("PropertyName", propertyName);
84 public bool Execute ()
87 TaskEngine taskEngine;
92 taskEngine = new TaskEngine (parentTarget.Project);
93 taskEngine.Prepare (InitializeTask (), this.taskElement, GetParameters (), this.Type);
94 result = taskEngine.Execute ();
96 taskEngine.PublishOutput ();
97 // FIXME: it should be logged (exception)
98 } catch (Exception e) {
99 Console.Error.WriteLine (e);
103 LogTaskFinished (result);
109 public string[] GetParameterNames ()
111 List <string> tempNames = new List <string> ();
113 foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
114 if (xmlAttribute.Name == "Condition" || xmlAttribute.Name == "ContinueOnError")
116 tempNames.Add (xmlAttribute.Name);
119 return tempNames.ToArray ();
122 public string GetParameterValue (string attributeName)
124 if (attributeName == "Condition")
125 throw new ArgumentException ("Condition attribute cannot be accessed using this method.");
126 if (attributeName == "ContinueOnError")
127 throw new ArgumentException ("ContinueOnError attribute cannot be accessed using this method.");
129 return taskElement.GetAttribute (attributeName);
132 public void SetParameterValue (string parameterName,
133 string parameterValue)
135 SetParameterValue (parameterName, parameterValue, false);
138 public void SetParameterValue (string parameterName,
139 string parameterValue,
140 bool treatParameterValueAsLiteral)
142 if (treatParameterValueAsLiteral)
143 taskElement.SetAttribute (parameterName, Utilities.Escape (parameterValue));
145 taskElement.SetAttribute (parameterName, parameterValue);
148 void LogTaskStarted ()
150 TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null, parentTarget.Project.FullFileName,
151 parentTarget.Project.FullFileName, taskElement.Name);
152 parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
155 void LogTaskFinished (bool succeeded)
157 TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null, parentTarget.Project.FullFileName,
158 parentTarget.Project.FullFileName, taskElement.Name, succeeded);
159 parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
162 ITask InitializeTask ()
166 task = (ITask)Activator.CreateInstance (this.Type);
167 task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, parentTarget.Project, 0, 0, ContinueOnError);
172 IDictionary <string, string> GetParameters ()
174 Dictionary <string, string> parameters = new Dictionary <string, string> ();
176 string[] parameterNames = GetParameterNames ();
178 foreach (string s in parameterNames)
179 parameters.Add (s, GetParameterValue (s));
184 public string Condition {
186 return taskElement.GetAttribute ("Condition");
189 taskElement.SetAttribute ("Condition", value);
194 public bool ContinueOnError {
196 string str = taskElement.GetAttribute ("ContinueOnError");
197 if (str == String.Empty)
200 Expression exp = new Expression ();
201 exp.Parse (str, true);
202 return (bool) exp.ConvertTo (parentTarget.Project, typeof (bool));
206 taskElement.SetAttribute ("ContinueOnError", value.ToString ());
211 public ITaskHost HostObject {
212 get { return hostObject; }
213 set { hostObject = value; }
217 get { return taskElement.Name; }
220 internal Target ParentTarget {
221 get { return parentTarget; }
222 set { parentTarget = value; }
225 internal XmlElement TaskElement {
226 get { return taskElement; }
227 set { taskElement = value; }
232 get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }