2007-01-09 Marek Sieradzki <marek.sieradzki@gmail.com>
[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                         taskEngine = new TaskEngine (parentTarget.Project);
92                         
93                         taskEngine.Prepare (InitializeTask (), this.taskElement, GetParameters (), this.Type);
94                         
95                         result = taskEngine.Execute ();
96                         
97                         taskEngine.PublishOutput ();
98                         
99                         LogTaskFinished (result);
100                 
101                         return result;
102                 }
103
104
105                 public string[] GetParameterNames ()
106                 {
107                         List <string> tempNames = new List <string> ();
108                         
109                         foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
110                                 if (xmlAttribute.Name == "Condition" || xmlAttribute.Name == "ContinueOnError")
111                                         continue;
112                                 tempNames.Add (xmlAttribute.Name);
113                         }
114                         
115                         return tempNames.ToArray ();
116                 }
117                 
118                 public string GetParameterValue (string attributeName)
119                 {
120                         if (attributeName == "Condition")
121                                 throw new ArgumentException ("Condition attribute cannot be accessed using this method.");
122                         if (attributeName == "ContinueOnError")
123                                 throw new ArgumentException ("ContinueOnError attribute cannot be accessed using this method.");
124
125                         return taskElement.GetAttribute (attributeName);
126                 }
127                 
128                 public void SetParameterValue (string parameterName,
129                                                string parameterValue)
130                 {
131                         SetParameterValue (parameterName, parameterValue, false);
132                 }
133                 
134                 public void SetParameterValue (string parameterName,
135                                                string parameterValue,
136                                                bool treatParameterValueAsLiteral)
137                 {
138                         if (treatParameterValueAsLiteral)
139                                 taskElement.SetAttribute (parameterName, Utilities.Escape (parameterValue));
140                         else
141                                 taskElement.SetAttribute (parameterName, parameterValue);
142                 }
143                 
144                 void LogTaskStarted ()
145                 {
146                         TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null, parentTarget.Project.FullFileName,
147                                 parentTarget.Project.FullFileName, taskElement.Name);
148                         parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
149                 }
150                 
151                 void LogTaskFinished (bool succeeded)
152                 {
153                         TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null, parentTarget.Project.FullFileName,
154                                 parentTarget.Project.FullFileName, taskElement.Name, succeeded);
155                         parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
156                 }
157                 
158                 ITask InitializeTask ()
159                 {
160                         ITask task;
161                         
162                         task = (ITask)Activator.CreateInstance (this.Type);
163                         task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0, ContinueOnError,
164                                 parentTarget.Project.FullFileName);
165                         
166                         return task;
167                 }
168                 
169                 IDictionary <string, string> GetParameters ()
170                 {
171                         Dictionary <string, string> parameters = new Dictionary <string, string> ();
172                         
173                         string[] parameterNames = GetParameterNames ();
174                         
175                         foreach (string s in parameterNames)
176                                 parameters.Add (s, GetParameterValue (s));
177                         
178                         return parameters;
179                 }
180                 
181                 public string Condition {
182                         get {
183                                 return taskElement.GetAttribute ("Condition");
184                         }
185                         set {
186                                 taskElement.SetAttribute ("Condition", value);
187                         }
188                 }
189
190                 [MonoTODO]
191                 public bool ContinueOnError {
192                         get {
193                                 string str = taskElement.GetAttribute ("ContinueOnError");
194                                 if (str == String.Empty)
195                                         return false;
196                                 else {
197                                         Expression exp = new Expression ();
198                                         exp.Parse (str);
199                                         return (bool) exp.ConvertTo (parentTarget.Project, typeof (bool));
200                                 }
201                         }
202                         set {
203                                 taskElement.SetAttribute ("ContinueOnError", value.ToString ());
204                         }
205                 }
206                 
207                 [MonoTODO]
208                 public ITaskHost HostObject {
209                         get { return hostObject; }
210                         set { hostObject = value; }
211                 }
212                 
213                 public string Name {
214                         get { return taskElement.Name; }
215                 }
216                 
217                 internal Target ParentTarget {
218                         get { return parentTarget; }
219                         set { parentTarget = value; }
220                 }
221                 
222                 internal XmlElement TaskElement {
223                         get { return taskElement; }
224                         set { taskElement = value; }
225                 }
226                 
227                 [MonoTODO]
228                 public Type Type {
229                         get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }
230                 }
231                 
232         }
233 }
234
235 #endif