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