006781fb90b5741cd18c64d37d2deee07c6d9b3f
[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                 // FIXME: implement
47                 internal BuildTask (XmlElement taskElement, Target parentTarget)
48                 {
49                         if (taskElement == null)
50                                 throw new ArgumentNullException ("taskElement");
51                         if (parentTarget == null)
52                                 throw new ArgumentNullException ("parentTarget");
53
54                         this.taskElement =  taskElement;
55                         this.parentTarget = parentTarget;
56                 }
57                 
58                 [MonoTODO]
59                 public void AddOutputItem (string taskParameter,
60                                            string itemName)
61                 {
62                 }
63                 
64                 [MonoTODO]
65                 public void AddOutputProperty (string taskParameter,
66                                                string propertyName)
67                 {
68                 }
69                 
70                 [MonoTODO]
71                 public bool Execute ()
72                 {
73                         bool            result;
74                         TaskEngine      taskEngine;
75
76                         LogTaskStarted ();
77                         
78                         taskEngine = new TaskEngine (parentTarget.Project);
79                         
80                         taskEngine.Prepare (InitializeTask (), this.taskElement, GetParameters (), this.Type);
81                         
82                         result = taskEngine.Execute ();
83                         
84                         taskEngine.PublishOutput ();
85                         
86                         LogTaskFinished (result);
87                 
88                         return result;
89                 }
90
91
92                 public string[] GetParameterNames ()
93                 {
94                         List <string> tempNames = new List <string> ();
95                         
96                         foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
97                                 if (xmlAttribute.Name == "Condition")
98                                         continue;
99                                 if (xmlAttribute.Name == "ContinueOnError")
100                                         continue;
101                                 tempNames.Add (xmlAttribute.Name);
102                         }
103                         
104                         return tempNames.ToArray ();
105                 }
106                 
107                 public string GetParameterValue (string attributeName)
108                 {
109                         if (attributeName == "Condition")
110                                 throw new ArgumentException ("Condition attribute cannot be accessed using this method.");
111                         if (attributeName == "ContinueOnError")
112                                 throw new ArgumentException ("ContinueOnError attribute cannot be accessed using this method.");
113
114                         return taskElement.GetAttribute (attributeName);
115                 }
116                 
117                 public void SetParameterValue (string parameterName,
118                                                string parameterValue)
119                 {
120                         SetParameterValue (parameterName, parameterValue, false);
121                 }
122                 
123                 public void SetParameterValue (string parameterName,
124                                                string parameterValue,
125                                                bool treatParameterValueAsLiteral)
126                 {
127                         if (treatParameterValueAsLiteral)
128                                 taskElement.SetAttribute (parameterName, Utilities.Escape (parameterValue));
129                         else
130                                 taskElement.SetAttribute (parameterName, parameterValue);
131                 }
132                 
133                 private void LogTaskStarted ()
134                 {
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);
138                 }
139                 
140                 private void LogTaskFinished (bool succeeded)
141                 {
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);
145                 }
146                 
147                 private ITask InitializeTask ()
148                 {
149                         ITask task;
150                         
151                         task = (ITask)Activator.CreateInstance (this.Type);
152                         task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0, ContinueOnError,
153                                 parentTarget.Project.FullFileName);
154                         
155                         return task;
156                 }
157                 
158                 private IDictionary <string, string> GetParameters ()
159                 {
160                         Dictionary <string, string> parameters = new Dictionary <string, string> ();
161                         
162                         string[] parameterNames = GetParameterNames ();
163                         
164                         foreach (string s in parameterNames) {
165                                 parameters.Add (s, GetParameterValue (s));
166                         }
167                         
168                         return parameters;
169                 }
170                 
171                 public string Condition {
172                         get {
173                                 return taskElement.GetAttribute ("Condition");
174                         }
175                         set {
176                                 taskElement.SetAttribute ("Condition", value);
177                         }
178                 }
179
180                 [MonoTODO]
181                 public bool ContinueOnError {
182                         get {
183                                 string str = taskElement.GetAttribute ("ContinueOnError");
184                                 if (str == String.Empty)
185                                         return false;
186                                 else {
187                                         OldExpression exp = new OldExpression (parentTarget.Project);
188                                         exp.ParseSource (str);
189                                         return (bool) exp.ConvertTo (typeof (bool));
190                                 }
191                         }
192                         set {
193                                 taskElement.SetAttribute ("ContinueOnError", value.ToString ());
194                         }
195                 }
196                 
197                 [MonoTODO]
198                 public ITaskHost HostObject {
199                         get { return hostObject; }
200                         set { hostObject = value; }
201                 }
202                 
203                 public string Name {
204                         get { return taskElement.Name; }
205                 }
206                 
207                 internal Target ParentTarget {
208                         get { return parentTarget; }
209                         set { parentTarget = value; }
210                 }
211                 
212                 internal XmlElement TaskElement {
213                         get { return taskElement; }
214                         set { taskElement = value; }
215                 }
216                 
217                 [MonoTODO]
218                 public Type Type {
219                         get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }
220                 }
221                 
222         }
223 }
224
225 #endif