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.Specialized;
33 using System.Reflection;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace Microsoft.Build.BuildEngine {
39 public class BuildTask {
43 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,
64 public void AddOutputProperty (string taskParameter,
70 public bool Execute ()
73 TaskEngine taskEngine;
77 taskEngine = new TaskEngine (parentTarget.Project);
79 taskEngine.Prepare (InitializeTask (), this.taskElement,GetParameters (), this.Type);
81 result = taskEngine.Execute ();
83 taskEngine.PublishOutput ();
85 LogTaskFinished (result);
92 public string[] GetParameterNames ()
94 int attributesCount = 0;
95 ArrayList tempNames = new ArrayList ();
98 foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
99 if (xmlAttribute.Name == "Condition")
101 if (xmlAttribute.Name == "ContinueOnError")
103 tempNames.Add (xmlAttribute.Name);
105 names = new string [tempNames.Count];
106 foreach (string name in tempNames)
107 names [attributesCount++] = name;
112 public string GetParameterValue (string attributeName)
114 return taskElement.GetAttribute (attributeName);
118 public void SetParameterValue (string parameterName,
119 string parameterValue)
121 SetParameterValue (parameterName, parameterValue, false);
125 public void SetParameterValue (string parameterName,
126 string parameterValue,
127 bool treatParameterValueAsLiteral)
129 // FIXME: use expression for parameterValue
130 taskElement.SetAttribute (parameterName, parameterValue);
133 private void LogTaskStarted ()
135 TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null, parentTarget.Project.FullFileName,
136 parentTarget.Project.FullFileName, taskElement.Name);
137 parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
140 private void LogTaskFinished (bool succeeded)
142 TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null, parentTarget.Project.FullFileName,
143 parentTarget.Project.FullFileName, taskElement.Name, succeeded);
144 parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
147 private ITask InitializeTask ()
151 task = (ITask)Activator.CreateInstance (this.Type);
152 task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0, ContinueOnError,
153 parentTarget.Project.FullFileName);
158 private IDictionary GetParameters ()
160 IDictionary parameters = new Hashtable ();
162 string[] parameterNames = GetParameterNames ();
164 foreach (string s in parameterNames) {
165 parameters.Add (s, GetParameterValue (s));
172 public string Condition {
174 return taskElement.GetAttribute ("Condition");
177 taskElement.SetAttribute ("Condition", value);
182 public bool ContinueOnError {
184 string str = taskElement.GetAttribute ("ContinueOnError");
185 if (str == String.Empty) {
188 return Boolean.Parse (str);
192 taskElement.SetAttribute ("ContinueOnError", value.ToString ());
197 public ITaskHost HostObject {
198 get { return hostObject; }
199 set { hostObject = value; }
203 get { return taskElement.Name; }
206 internal Target ParentTarget {
207 get { return parentTarget; }
208 set { parentTarget = value; }
211 internal XmlElement TaskElement {
212 get { return taskElement; }
213 set { taskElement = value; }
218 get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }