TaskElement.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // TaskElement.cs: Represents XML element that is a task.
  3. //
  4. // Author:
  5. // Marek Sieradzki ([email protected])
  6. //
  7. // (C) 2005 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. #if NET_2_0
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. using System.Reflection;
  32. using System.Xml;
  33. using Microsoft.Build.Framework;
  34. using Microsoft.Build.Utilities;
  35. namespace Microsoft.Build.BuildEngine {
  36. public class TaskElement : ITaskElement {
  37. XmlAttribute condition;
  38. XmlAttribute continueOnError;
  39. XmlElement taskElement;
  40. Target parentTarget;
  41. object hostObject;
  42. string name;
  43. bool isImported;
  44. static Type requiredAttribute;
  45. static Type outputAttribute;
  46. public TaskElement ()
  47. {
  48. this.isImported = false;
  49. }
  50. static TaskElement ()
  51. {
  52. requiredAttribute = typeof (Microsoft.Build.Framework.RequiredAttribute);
  53. outputAttribute = typeof (Microsoft.Build.Framework.OutputAttribute);
  54. }
  55. public bool Execute ()
  56. {
  57. bool result;
  58. string rawParameter;
  59. string[] parameterNames;
  60. PropertyInfo[] properties;
  61. PropertyInfo currentProperty;
  62. Hashtable values = new Hashtable ();
  63. Task task;
  64. object value;
  65. LogTaskStarted ();
  66. task = (Task)Activator.CreateInstance (Type);
  67. task.BuildEngine = new BuildEngine (parentTarget.Project.ParentEngine, 0, 0,
  68. ContinueOnError, parentTarget.Project.FullFileName);
  69. parameterNames = GetParameterNames ();
  70. foreach (string parameter in parameterNames) {
  71. currentProperty = GetType ().GetProperty (parameter);
  72. if (currentProperty == null)
  73. throw new InvalidProjectFileException (String.Format ("Task does not have property \"{0}\" defined",
  74. parameter));
  75. rawParameter = GetParameterValue (parameter);
  76. value = GetObjectFromString (rawParameter, currentProperty.PropertyType);
  77. values.Add (parameter, value);
  78. }
  79. properties = GetType().GetProperties ();
  80. foreach (PropertyInfo pi in properties) {
  81. if (pi.IsDefined (requiredAttribute, false) && values.ContainsKey (pi.Name) == false) {
  82. throw new InvalidProjectFileException ("Required property not set.");
  83. }
  84. if (values.ContainsKey (pi.Name)) {
  85. pi.SetValue (task, values [pi.Name], null);
  86. }
  87. }
  88. result = task.Execute ();
  89. // output
  90. foreach (XmlNode xn in taskElement.ChildNodes) {
  91. if (xn is XmlElement) {
  92. XmlElement xe = (XmlElement) xn;
  93. PropertyInfo pi;
  94. string property, parameter, item;
  95. object o;
  96. Project p;
  97. if (xe.Name != "Output")
  98. throw new InvalidProjectFileException ("Only Output elements can be Task's child nodes.");
  99. if (xe.GetAttribute ("ItemName") != "" && xe.GetAttribute ("PropertyName") != "")
  100. throw new InvalidProjectFileException ("Only one of ItemName and ProperytyName attributes can be specified.");
  101. if (xe.GetAttribute ("TaskParameter") == "")
  102. throw new InvalidProjectFileException ("TaskParameter attribute must be specified.");
  103. property = xe.GetAttribute ("PropertyName");
  104. parameter = xe.GetAttribute ("TaskParameter");
  105. item = xe.GetAttribute ("ItemName");
  106. p = parentTarget.Project;
  107. pi = GetType ().GetProperty (parameter);
  108. if (pi == null)
  109. throw new Exception ("Could not get property info.");
  110. if (pi.IsDefined (outputAttribute, false) == false)
  111. throw new Exception ("This is not output property.");
  112. o = pi.GetValue (task, null);
  113. if (o == null)
  114. continue;
  115. if (xe.GetAttribute ("ItemName") != "") {
  116. BuildItemGroup newItems = HandleItemGroup (pi, o, item);
  117. if (p.EvaluatedItemsByName.Contains (item)) {
  118. BuildItemGroup big = (BuildItemGroup) p.EvaluatedItemsByName [item];
  119. big.Clear ();
  120. p.EvaluatedItemsByName.Remove (item);
  121. p.EvaluatedItemsByName.Add (item, newItems);
  122. } else {
  123. p.EvaluatedItemsByName.Add (item, newItems);
  124. }
  125. } else {
  126. BuildProperty bp = HandleProperty (pi, o, property);
  127. p.EvaluatedProperties.AddFromExistingProperty (bp);
  128. }
  129. }
  130. }
  131. // end of output
  132. LogTaskFinished (result);
  133. return result;
  134. }
  135. private BuildProperty HandleProperty (PropertyInfo propertyInfo, object o, string name)
  136. {
  137. string output = null;
  138. BuildProperty bp;
  139. if (propertyInfo == null)
  140. throw new ArgumentNullException ("propertyInfo");
  141. if (o == null)
  142. throw new ArgumentNullException ("o");
  143. if (name == null)
  144. throw new ArgumentNullException ("name");
  145. if (propertyInfo.PropertyType == typeof (ITaskItem)) {
  146. ITaskItem item = (ITaskItem) o;
  147. bp = ChangeType.TransformToBuildProperty (name, item);
  148. } else if (propertyInfo.PropertyType == typeof (ITaskItem[])) {
  149. ITaskItem[] items = (ITaskItem[]) o;
  150. bp = ChangeType.TransformToBuildProperty (name, items);
  151. } else {
  152. if (propertyInfo.PropertyType.IsArray == true) {
  153. output = ChangeType.TransformToString ((object[])o, propertyInfo.PropertyType);
  154. } else {
  155. output = ChangeType.TransformToString (o, propertyInfo.PropertyType);
  156. }
  157. bp = ChangeType.TransformToBuildProperty (name, output);
  158. }
  159. return bp;
  160. }
  161. private BuildItemGroup HandleItemGroup (PropertyInfo propertyInfo, object o, string name)
  162. {
  163. BuildItemGroup big;
  164. string temp;
  165. if (propertyInfo == null)
  166. throw new ArgumentNullException ("propertyInfo");
  167. if (o == null)
  168. throw new ArgumentNullException ("o");
  169. if (name == null)
  170. throw new ArgumentNullException ("name");
  171. if (propertyInfo.PropertyType == typeof (ITaskItem)) {
  172. ITaskItem item = (ITaskItem) o;
  173. big = ChangeType.TransformToBuildItemGroup (name, item);
  174. } else if (propertyInfo.PropertyType == typeof (ITaskItem[])) {
  175. ITaskItem[] items = (ITaskItem[]) o;
  176. big = ChangeType.TransformToBuildItemGroup (name, items);
  177. } else {
  178. if (propertyInfo.PropertyType.IsArray == true) {
  179. temp = ChangeType.TransformToString ((object[]) o, propertyInfo.PropertyType);
  180. } else {
  181. temp = ChangeType.TransformToString (o, propertyInfo.PropertyType);
  182. }
  183. big = ChangeType.TransformToBuildItemGroup (name, temp);
  184. }
  185. return big;
  186. }
  187. public string[] GetParameterNames ()
  188. {
  189. int attributesCount = 0;
  190. ArrayList tempNames = new ArrayList ();
  191. string[] names;
  192. foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
  193. if (xmlAttribute.Name == "Condition")
  194. continue;
  195. if (xmlAttribute.Name == "ContinueOnError")
  196. continue;
  197. tempNames.Add (xmlAttribute.Name);
  198. }
  199. names = new string [tempNames.Count];
  200. foreach (string name in tempNames)
  201. names [attributesCount++] = name;
  202. return names;
  203. }
  204. public string GetParameterValue (string attributeName)
  205. {
  206. return taskElement.GetAttribute (attributeName);
  207. }
  208. private object GetObjectFromString (string raw, Type type)
  209. {
  210. Expression e;
  211. object result;
  212. e = new Expression (parentTarget.Project, raw);
  213. if (type == typeof (ITaskItem)) {
  214. result = (object) e.ToITaskItem ();
  215. } else if (type == typeof (ITaskItem[])) {
  216. result = (object) e.ToITaskItemArray ();
  217. } else {
  218. if (type.IsArray) {
  219. result = e.ToArray (type);
  220. } else {
  221. result = e.ToNonArray (type);
  222. }
  223. }
  224. return result;
  225. }
  226. public void SetParameterValue (string parameterName,
  227. string parameterValue)
  228. {
  229. taskElement.SetAttribute (parameterName, parameterValue);
  230. }
  231. internal void BindToXml (XmlElement taskElement,
  232. Target parentTarget)
  233. {
  234. if (taskElement == null)
  235. throw new ArgumentNullException ("taskElement");
  236. if (parentTarget == null)
  237. throw new ArgumentNullException ("parentTarget");
  238. this.taskElement = taskElement;
  239. this.parentTarget = parentTarget;
  240. this.condition = taskElement.GetAttributeNode ("Condition");
  241. this.continueOnError = taskElement.GetAttributeNode ("ContinueOnError");
  242. this.name = taskElement.Name;
  243. }
  244. private void LogTaskStarted ()
  245. {
  246. TaskStartedEventArgs tsea = new TaskStartedEventArgs ("Task started.", null, parentTarget.Project.FullFileName,
  247. parentTarget.Project.FullFileName, taskElement.Name);
  248. parentTarget.Project.ParentEngine.EventSource.FireTaskStarted (this, tsea);
  249. }
  250. private void LogTaskFinished (bool succeeded)
  251. {
  252. TaskFinishedEventArgs tfea = new TaskFinishedEventArgs ("Task finished.", null, parentTarget.Project.FullFileName,
  253. parentTarget.Project.FullFileName, taskElement.Name, succeeded);
  254. parentTarget.Project.ParentEngine.EventSource.FireTaskFinished (this, tfea);
  255. }
  256. private new Type GetType ()
  257. {
  258. return parentTarget.Project.TaskDatabase.GetTypeFromClassName (name);
  259. }
  260. public string Condition {
  261. get {
  262. return condition.Value;
  263. }
  264. set {
  265. condition.Value = value;
  266. }
  267. }
  268. public bool ContinueOnError {
  269. get {
  270. // FIXME: insert here text parsing
  271. if (continueOnError == null)
  272. return false;
  273. else
  274. return Boolean.Parse (continueOnError.Value);
  275. }
  276. set {
  277. continueOnError.Value = value.ToString ();
  278. }
  279. }
  280. public object HostObject {
  281. get {
  282. return hostObject;
  283. }
  284. set {
  285. hostObject = value;
  286. }
  287. }
  288. public string Name {
  289. get {
  290. return name;
  291. }
  292. }
  293. public Type Type {
  294. get {
  295. return GetType ();
  296. }
  297. }
  298. }
  299. }
  300. #endif