2006-03-18 Crestez Leonard <cdleonard@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.Specialized;
33 using System.Reflection;
34 using System.Xml;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.BuildEngine {
39         public class BuildTask {
40         
41                 XmlAttribute            condition;
42                 XmlAttribute            continueOnError;
43                 ITaskHost               hostObject;
44                 string                  name;
45                 Target                  parentTarget;
46                 XmlElement              taskElement;
47                 Type                    type;
48         
49                 // FIXME: implement
50                 internal BuildTask (XmlElement taskElement, Target parentTarget)
51                 {
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;
62                         }
63                 }
64                 
65                 [MonoTODO]
66                 public void AddOutputItem (string taskParameter,
67                                            string itemName)
68                 {
69                 }
70                 
71                 [MonoTODO]
72                 public void AddOutputProperty (string taskParameter,
73                                                string propertyName)
74                 {
75                 }
76                 
77                 [MonoTODO]
78                 public bool Execute ()
79                 {
80                         bool            result;
81                         TaskEngine      taskEngine;
82
83                         LogTaskStarted ();
84                         
85                         taskEngine = new TaskEngine (parentTarget.Project);
86                         
87                         taskEngine.Prepare (InitializeTask (), this.taskElement,GetParameters (), this.Type);
88                         
89                         result = taskEngine.Execute ();
90                         
91                         taskEngine.PublishOutput ();
92                         
93                         LogTaskFinished (result);
94                 
95                         return result;
96                 }
97
98
99                 [MonoTODO]
100                 public string[] GetParameterNames ()
101                 {
102                         int attributesCount = 0;
103                         ArrayList tempNames = new ArrayList ();
104                         string[] names;
105                         
106                         foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
107                                 if (xmlAttribute.Name == "Condition")
108                                         continue;
109                                 if (xmlAttribute.Name == "ContinueOnError")
110                                         continue;
111                                 tempNames.Add (xmlAttribute.Name);
112                         }
113                         names = new string [tempNames.Count];
114                         foreach (string name in tempNames)
115                                 names [attributesCount++] = name;
116                         return names;
117                 }
118                 
119                 [MonoTODO]
120                 public string GetParameterValue (string attributeName)
121                 {
122                         return taskElement.GetAttribute (attributeName);
123                 }
124                 
125                 [MonoTODO]
126                 public void SetParameterValue (string parameterName,
127                                                string parameterValue)
128                 {
129                         SetParameterValue (parameterName, parameterValue, false);
130                 }
131                 
132                 [MonoTODO]
133                 public void SetParameterValue (string parameterName,
134                                                string parameterValue,
135                                                bool treatParameterValueAsLiteral)
136                 {
137                         // FIXME: use expression for parameterValue
138                         taskElement.SetAttribute (parameterName, parameterValue);
139                 }
140                 
141                 private void LogTaskStarted ()
142                 {
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);
146                 }
147                 
148                 private void LogTaskFinished (bool succeeded)
149                 {
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);
153                 }
154                 
155                 private ITask InitializeTask ()
156                 {
157                         ITask task;
158                         
159                         task = (ITask)Activator.CreateInstance (this.Type);
160                         task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0, ContinueOnError,
161                                 parentTarget.Project.FullFileName);
162                         
163                         return task;
164                 }
165                 
166                 private IDictionary GetParameters ()
167                 {
168                         IDictionary parameters = new Hashtable ();
169                         
170                         string[] parameterNames = GetParameterNames ();
171                         
172                         foreach (string s in parameterNames) {
173                                 parameters.Add (s, GetParameterValue (s));
174                         }
175                         
176                         return parameters;
177                 }
178                 
179                 [MonoTODO]
180                 public string Condition {
181                         get { return condition.Value; }
182                         set { condition.Value = value; }
183                 }
184
185                 [MonoTODO]
186                 public bool ContinueOnError {
187                         get {
188                                 if (continueOnError == null)
189                                         return false;
190                                 else
191                                         return Boolean.Parse (continueOnError.Value);
192                         }
193                         set {
194                                 continueOnError.Value = value.ToString ();
195                         }
196                 }
197                 
198                 [MonoTODO]
199                 public ITaskHost HostObject {
200                         get { return hostObject; }
201                         set { hostObject = value; }
202                 }
203                 
204                 [MonoTODO]
205                 public string Name {
206                         get { return name; }
207                 }
208                 
209                 internal Target ParentTarget {
210                         get { return parentTarget; }
211                         set { parentTarget = value; }
212                 }
213                 
214                 internal XmlElement TaskElement {
215                         get { return taskElement; }
216                         set { taskElement = value; }
217                 }
218                 
219                 [MonoTODO]
220                 public Type Type {
221                         get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (name); }
222                 }
223                 
224         }
225 }
226
227 #endif