51493b1678a0b458c7fd1da7d8bca76a92a3f62e
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildTask.cs
1 //
2 // BuildTask.cs: Represents a Task element in a project.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2006 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
34 using System.Reflection;
35 using System.Xml;
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Utilities;
38
39 namespace Microsoft.Build.BuildEngine {
40         public class BuildTask {
41         
42                 ITaskHost               hostObject;
43                 Target                  parentTarget;
44                 XmlElement              taskElement;
45         
46                 internal BuildTask (XmlElement taskElement, Target parentTarget)
47                 {
48                         if (taskElement == null)
49                                 throw new ArgumentNullException ("taskElement");
50                         if (parentTarget == null)
51                                 throw new ArgumentNullException ("parentTarget");
52
53                         this.taskElement =  taskElement;
54                         this.parentTarget = parentTarget;
55                 }
56                 
57                 [MonoTODO]
58                 public void AddOutputItem (string taskParameter,
59                                            string itemName)
60                 {
61                         XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
62                         taskElement.AppendChild (element);
63                         
64                         if (taskParameter != null)
65                                 element.SetAttribute ("TaskParameter", taskParameter);
66                         if (itemName != null)
67                                 element.SetAttribute ("ItemName", itemName);
68                 }
69                 
70                 [MonoTODO]
71                 public void AddOutputProperty (string taskParameter,
72                                                string propertyName)
73                 {
74                         XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
75                         taskElement.AppendChild (element);
76                         
77                         if (taskParameter != null)
78                                 element.SetAttribute ("TaskParameter", taskParameter);
79                         if (propertyName != null)
80                                 element.SetAttribute ("PropertyName", propertyName);
81                 }
82                 
83                 [MonoTODO]
84                 public bool Execute ()
85                 {
86                         bool            result;
87                         TaskEngine      taskEngine;
88
89                         LogTaskStarted ();
90
91                         try {
92                                 taskEngine = new TaskEngine (parentTarget.Project);             
93                                 taskEngine.Prepare (InitializeTask (), this.taskElement, GetParameters (), this.Type);
94                                 result = taskEngine.Execute ();
95                                 taskEngine.PublishOutput ();
96                         // FIXME: it should be logged (exception)
97                         } catch {
98                                 result = false;
99                         }
100
101                         LogTaskFinished (result);
102                 
103                         return result;
104                 }
105
106
107                 public string[] GetParameterNames ()
108                 {
109                         List <string> tempNames = new List <string> ();
110                         
111                         foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
112                                 if (xmlAttribute.Name == "Condition" || xmlAttribute.Name == "ContinueOnError")
113                                         continue;
114                                 tempNames.Add (xmlAttribute.Name);
115                         }
116                         
117                         return tempNames.ToArray ();
118                 }
119                 
120                 public string GetParameterValue (string attributeName)
121                 {
122                         if (attributeName == "Condition")
123                                 throw new ArgumentException ("Condition attribute cannot be accessed using this method.");
124                         if (attributeName == "ContinueOnError")
125                                 throw new ArgumentException ("ContinueOnError attribute cannot be accessed using this method.");
126
127                         return taskElement.GetAttribute (attributeName);
128                 }
129                 
130                 public void SetParameterValue (string parameterName,
131                                                string parameterValue)
132                 {
133                         SetParameterValue (parameterName, parameterValue, false);
134                 }
135                 
136                 public void SetParameterValue (string parameterName,
137                                                string parameterValue,
138                                                bool treatParameterValueAsLiteral)
139                 {
140                         if (treatParameterValueAsLiteral)
141                                 taskElement.SetAttribute (parameterName, Utilities.Escape (parameterValue));
142                         else
143                                 taskElement.SetAttribute (parameterName, parameterValue);
144                 }
145                 
146                 void LogTaskStarted ()
147                 {
148                         TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null, parentTarget.Project.FullFileName,
149                                 parentTarget.Project.FullFileName, taskElement.Name);
150                         parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
151                 }
152                 
153                 void LogTaskFinished (bool succeeded)
154                 {
155                         TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null, parentTarget.Project.FullFileName,
156                                 parentTarget.Project.FullFileName, taskElement.Name, succeeded);
157                         parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
158                 }
159                 
160                 ITask InitializeTask ()
161                 {
162                         ITask task;
163                         
164                         task = (ITask)Activator.CreateInstance (this.Type);
165                         task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0, ContinueOnError,
166                                 parentTarget.Project.FullFileName);
167                         
168                         return task;
169                 }
170                 
171                 IDictionary <string, string> GetParameters ()
172                 {
173                         Dictionary <string, string> parameters = new Dictionary <string, string> ();
174                         
175                         string[] parameterNames = GetParameterNames ();
176                         
177                         foreach (string s in parameterNames)
178                                 parameters.Add (s, GetParameterValue (s));
179                         
180                         return parameters;
181                 }
182                 
183                 public string Condition {
184                         get {
185                                 return taskElement.GetAttribute ("Condition");
186                         }
187                         set {
188                                 taskElement.SetAttribute ("Condition", value);
189                         }
190                 }
191
192                 [MonoTODO]
193                 public bool ContinueOnError {
194                         get {
195                                 string str = taskElement.GetAttribute ("ContinueOnError");
196                                 if (str == String.Empty)
197                                         return false;
198                                 else {
199                                         Expression exp = new Expression ();
200                                         exp.Parse (str, true);
201                                         return (bool) exp.ConvertTo (parentTarget.Project, typeof (bool));
202                                 }
203                         }
204                         set {
205                                 taskElement.SetAttribute ("ContinueOnError", value.ToString ());
206                         }
207                 }
208                 
209                 [MonoTODO]
210                 public ITaskHost HostObject {
211                         get { return hostObject; }
212                         set { hostObject = value; }
213                 }
214                 
215                 public string Name {
216                         get { return taskElement.Name; }
217                 }
218                 
219                 internal Target ParentTarget {
220                         get { return parentTarget; }
221                         set { parentTarget = value; }
222                 }
223                 
224                 internal XmlElement TaskElement {
225                         get { return taskElement; }
226                         set { taskElement = value; }
227                 }
228                 
229                 [MonoTODO]
230                 public Type Type {
231                         get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }
232                 }
233                 
234         }
235 }
236
237 #endif