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 {
41 XmlAttribute condition;
42 XmlAttribute continueOnError;
46 XmlElement taskElement;
50 internal BuildTask (XmlElement taskElement, Target parentTarget)
52 //if (taskElement == null)
53 // throw new ArgumentNullException ("taskElement");
54 if (parentTarget == null)
55 throw new ArgumentNullException ("parentTarget");
56 if (taskElement != null) {
57 this.taskElement = taskElement;
58 this.parentTarget = parentTarget;
59 this.condition = taskElement.GetAttributeNode ("Condition");
60 this.continueOnError = taskElement.GetAttributeNode ("ContinueOnError");
61 this.name = taskElement.Name;
66 public void AddOutputItem (string taskParameter,
72 public void AddOutputProperty (string taskParameter,
78 public bool Execute ()
81 TaskEngine taskEngine;
85 taskEngine = new TaskEngine (parentTarget.Project);
87 taskEngine.Prepare (InitializeTask (), this.taskElement,GetParameters (), this.Type);
89 result = taskEngine.Execute ();
91 taskEngine.PublishOutput ();
93 LogTaskFinished (result);
100 public string[] GetParameterNames ()
102 int attributesCount = 0;
103 ArrayList tempNames = new ArrayList ();
106 foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
107 if (xmlAttribute.Name == "Condition")
109 if (xmlAttribute.Name == "ContinueOnError")
111 tempNames.Add (xmlAttribute.Name);
113 names = new string [tempNames.Count];
114 foreach (string name in tempNames)
115 names [attributesCount++] = name;
120 public string GetParameterValue (string attributeName)
122 return taskElement.GetAttribute (attributeName);
126 public void SetParameterValue (string parameterName,
127 string parameterValue)
129 SetParameterValue (parameterName, parameterValue, false);
133 public void SetParameterValue (string parameterName,
134 string parameterValue,
135 bool treatParameterValueAsLiteral)
137 // FIXME: use expression for parameterValue
138 taskElement.SetAttribute (parameterName, parameterValue);
141 private void LogTaskStarted ()
143 TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null, parentTarget.Project.FullFileName,
144 parentTarget.Project.FullFileName, taskElement.Name);
145 parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
148 private void LogTaskFinished (bool succeeded)
150 TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null, parentTarget.Project.FullFileName,
151 parentTarget.Project.FullFileName, taskElement.Name, succeeded);
152 parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
155 private ITask InitializeTask ()
159 task = (ITask)Activator.CreateInstance (this.Type);
160 task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0, ContinueOnError,
161 parentTarget.Project.FullFileName);
166 private IDictionary GetParameters ()
168 IDictionary parameters = new Hashtable ();
170 string[] parameterNames = GetParameterNames ();
172 foreach (string s in parameterNames) {
173 parameters.Add (s, GetParameterValue (s));
180 public string Condition {
181 get { return condition.Value; }
182 set { condition.Value = value; }
186 public bool ContinueOnError {
188 if (continueOnError == null)
191 return Boolean.Parse (continueOnError.Value);
194 continueOnError.Value = value.ToString ();
199 public ITaskHost HostObject {
200 get { return hostObject; }
201 set { hostObject = value; }
209 internal Target ParentTarget {
210 get { return parentTarget; }
211 set { parentTarget = value; }
214 internal XmlElement TaskElement {
215 get { return taskElement; }
216 set { taskElement = value; }
221 get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (name); }