2006-12-16 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / TaskEngine.cs
index 2f0337c9297136c5f8bff0c602ca622d775b9c89..d4365bbd52565dfc965a882f56bda2c3752f7a25 100644 (file)
@@ -28,7 +28,7 @@
 #if NET_2_0
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Reflection;
 using System.Xml;
@@ -40,6 +40,7 @@ namespace Microsoft.Build.BuildEngine {
                
                ITask           task;
                XmlElement      taskElement;
+               Type            taskType;
                Project         parentProject;
                
                static Type     requiredAttribute;
@@ -51,27 +52,34 @@ namespace Microsoft.Build.BuildEngine {
                        outputAttribute = typeof (Microsoft.Build.Framework.OutputAttribute);
                }
 
-               public TaskEngine ()
+               public TaskEngine (Project project)
                {
+                       parentProject = project;
                }
                
                public void Prepare (ITask task, XmlElement taskElement,
-                                    IDictionary parameters, Type taskType)
+                                    IDictionary <string, string> parameters, Type taskType)
                {
-                       Hashtable       values;
+                       Dictionary <string, object>     values;
                        PropertyInfo    currentProperty;
                        PropertyInfo[]  properties;
-               
+                       object          value;
+                       
                        this.task = task;
                        this.taskElement = taskElement;
-                       values = new Hashtable ();
+                       this.taskType = taskType;
+                       values = new Dictionary <string, object> ();
                        
-                       foreach (DictionaryEntry de in parameters) {
-                               currentProperty = taskType.GetProperty ((string) de.Key);
+                       foreach (KeyValuePair <string, string> de in parameters) {
+                               currentProperty = taskType.GetProperty (de.Key);
                                if (currentProperty == null)
                                        throw new InvalidProjectFileException (String.Format ("Task does not have property \"{0}\" defined",
                                                de.Key));
-                               values.Add ((string) de.Key, GetObjectFromString ((string) de.Value, currentProperty.PropertyType)); 
+                               
+                               value = GetObjectFromString (de.Value, currentProperty.PropertyType);           
+                               
+                               if (value != null)
+                                       values.Add (de.Key, value);
                        }
                        
                        properties = taskType.GetProperties ();
@@ -86,11 +94,7 @@ namespace Microsoft.Build.BuildEngine {
                
                public bool Execute ()
                {
-                       bool    result;
-                       
-                       result = task.Execute ();
-               
-                       return result;
+                       return task.Execute ();
                }
                
                public void PublishOutput ()
@@ -103,35 +107,36 @@ namespace Microsoft.Build.BuildEngine {
                        object          o;
                
                        foreach (XmlNode xmlNode in taskElement.ChildNodes) {
-                               if (xmlNode is XmlElement) {
-                                       xmlElement = (XmlElement) xmlNode;
-                                       
-                                       if (xmlElement.Name != "Output")
-                                               throw new InvalidProjectFileException ("Only Output elements can be Task's child nodes.");
-                                       if (xmlElement.GetAttribute ("ItemName") != "" && xmlElement.GetAttribute ("PropertyName") != "")
-                                               throw new InvalidProjectFileException ("Only one of ItemName and ProperytyName attributes can be specified.");
-                                       if (xmlElement.GetAttribute ("TaskParameter") == "")
-                                               throw new InvalidProjectFileException ("TaskParameter attribute must be specified.");
-                                               
-                                       taskParameter = xmlElement.GetAttribute ("TaskParameter");
-                                       itemName = xmlElement.GetAttribute ("ItemName");
-                                       propertyName = xmlElement.GetAttribute ("PropertyName");
-                                       
-                                       propertyInfo = GetType ().GetProperty (taskParameter);
-                                       if (propertyInfo == null)
-                                               throw new Exception ("Could not get property info.");
-                                       if (propertyInfo.IsDefined (outputAttribute, false) == false)
-                                               throw new Exception ("This is not output property.");
-                                       
-                                       o = propertyInfo.GetValue (task, null);
-                                       if (o == null)
-                                               continue;
+                               if (!(xmlNode is XmlElement))
+                                       continue;
+                       
+                               xmlElement = (XmlElement) xmlNode;
+                               
+                               if (xmlElement.Name != "Output")
+                                       throw new InvalidProjectFileException ("Only Output elements can be Task's child nodes.");
+                               if (xmlElement.GetAttribute ("ItemName") != String.Empty && xmlElement.GetAttribute ("PropertyName") != String.Empty)
+                                       throw new InvalidProjectFileException ("Only one of ItemName and PropertyName attributes can be specified.");
+                               if (xmlElement.GetAttribute ("TaskParameter") == String.Empty)
+                                       throw new InvalidProjectFileException ("TaskParameter attribute must be specified.");
                                        
-                                       if (itemName != String.Empty) {
-                                               PublishItemGroup (propertyInfo, o, itemName);
-                                       } else {
-                                               PublishProperty (propertyInfo, o, propertyName);
-                                       }
+                               taskParameter = xmlElement.GetAttribute ("TaskParameter");
+                               itemName = xmlElement.GetAttribute ("ItemName");
+                               propertyName = xmlElement.GetAttribute ("PropertyName");
+                               
+                               propertyInfo = taskType.GetProperty (taskParameter);
+                               if (propertyInfo == null)
+                                       throw new Exception ("Could not get property info.");
+                               if (propertyInfo.IsDefined (outputAttribute, false) == false)
+                                       throw new Exception ("This is not output property.");
+                               
+                               o = propertyInfo.GetValue (task, null);
+                               if (o == null)
+                                       continue;
+                               
+                               if (itemName != String.Empty) {
+                                       PublishItemGroup (propertyInfo, o, itemName);
+                               } else {
+                                       PublishProperty (propertyInfo, o, propertyName);
                                }
                        }
                }
@@ -146,14 +151,17 @@ namespace Microsoft.Build.BuildEngine {
                                               string itemName)
                {
                        BuildItemGroup newItems = CollectItemGroup (propertyInfo, o, itemName);
-                       if (parentProject.EvaluatedItemsByName.Contains (itemName)) {
-                               BuildItemGroup big = (BuildItemGroup) parentProject.EvaluatedItemsByName [itemName];
+                       
+                       if (parentProject.EvaluatedItemsByName.ContainsKey (itemName)) {
+                               BuildItemGroup big = parentProject.EvaluatedItemsByName [itemName];
                                big.Clear ();
                                parentProject.EvaluatedItemsByName.Remove (itemName);
                                parentProject.EvaluatedItemsByName.Add (itemName, newItems);
                        } else {
                                parentProject.EvaluatedItemsByName.Add (itemName, newItems);
                        }
+                       foreach (BuildItem bi in newItems)
+                               parentProject.EvaluatedItems.AddItem (bi);
                }
                
                private void PublishProperty (PropertyInfo propertyInfo,
@@ -161,7 +169,7 @@ namespace Microsoft.Build.BuildEngine {
                                              string propertyName)
                {
                        BuildProperty bp = CollectProperty (propertyInfo, o, propertyName);
-                       parentProject.EvaluatedProperties.AddFromExistingProperty (bp);
+                       parentProject.EvaluatedProperties.AddProperty (bp);
                }
                
                private BuildProperty CollectProperty (PropertyInfo propertyInfo, object o, string name)
@@ -227,23 +235,17 @@ namespace Microsoft.Build.BuildEngine {
                        Expression e;
                        object result;
                        
-                       e = new Expression (parentProject, raw);
+                       e = new Expression ();
+                       e.Parse (raw);
                        
-                       if (type == typeof (ITaskItem)) {
-                               result = (object) e.ToITaskItem ();
-                       } else if (type == typeof (ITaskItem[])) {
-                               result = (object) e.ToITaskItemArray ();
-                       } else {
-                               if (type.IsArray) {
-                                       result = e.ToArray (type);
-                               } else {
-                                       result = e.ToNonArray (type);
-                               }
-                       }
+                       if ((string) e.ConvertTo (parentProject, typeof (string)) == String.Empty)
+                               return null;
+                       
+                       result = e.ConvertTo (parentProject, type);
                        
                        return result;
                }
        }
 }
 
-#endif
\ No newline at end of file
+#endif