2006-12-16 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildTask.cs
index d9c3a5b171e151ceb10b04d441b1298a5b978df1..ef25e6b7e9bf2dc913b2fde70ce4877effb47bed 100644 (file)
@@ -29,6 +29,7 @@
 
 using System;
 using System.Collections;
+using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Reflection;
 using System.Xml;
@@ -38,40 +39,45 @@ using Microsoft.Build.Utilities;
 namespace Microsoft.Build.BuildEngine {
        public class BuildTask {
        
-               XmlAttribute            condition;
-               XmlAttribute            continueOnError;
                ITaskHost               hostObject;
-               string                  name;
                Target                  parentTarget;
                XmlElement              taskElement;
-               Type                    type;
        
-               // FIXME: implement
                internal BuildTask (XmlElement taskElement, Target parentTarget)
                {
-                       //if (taskElement == null)
-                       //      throw new ArgumentNullException ("taskElement");
+                       if (taskElement == null)
+                               throw new ArgumentNullException ("taskElement");
                        if (parentTarget == null)
                                throw new ArgumentNullException ("parentTarget");
-                       if (taskElement != null) {
-                               this.taskElement =  taskElement;
-                               this.parentTarget = parentTarget;
-                               this.condition = taskElement.GetAttributeNode ("Condition");
-                               this.continueOnError = taskElement.GetAttributeNode ("ContinueOnError");
-                               this.name  = taskElement.Name;
-                       }
+
+                       this.taskElement =  taskElement;
+                       this.parentTarget = parentTarget;
                }
                
                [MonoTODO]
                public void AddOutputItem (string taskParameter,
                                           string itemName)
                {
+                       XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
+                       taskElement.AppendChild (element);
+                       
+                       if (taskParameter != null)
+                               element.SetAttribute ("TaskParameter", taskParameter);
+                       if (itemName != null)
+                               element.SetAttribute ("ItemName", itemName);
                }
                
                [MonoTODO]
                public void AddOutputProperty (string taskParameter,
                                               string propertyName)
                {
+                       XmlElement element = parentTarget.Project.XmlDocument.CreateElement ("Output", Project.XmlNamespace);
+                       taskElement.AppendChild (element);
+                       
+                       if (taskParameter != null)
+                               element.SetAttribute ("TaskParameter", taskParameter);
+                       if (propertyName != null)
+                               element.SetAttribute ("PropertyName", propertyName);
                }
                
                [MonoTODO]
@@ -84,7 +90,7 @@ namespace Microsoft.Build.BuildEngine {
                        
                        taskEngine = new TaskEngine (parentTarget.Project);
                        
-                       taskEngine.Prepare (InitializeTask (), this.taskElement,GetParameters (), this.Type);
+                       taskEngine.Prepare (InitializeTask (), this.taskElement, GetParameters (), this.Type);
                        
                        result = taskEngine.Execute ();
                        
@@ -96,12 +102,9 @@ namespace Microsoft.Build.BuildEngine {
                }
 
 
-               [MonoTODO]
                public string[] GetParameterNames ()
                {
-                       int attributesCount = 0;
-                       ArrayList tempNames = new ArrayList ();
-                       string[] names;
+                       List <string> tempNames = new List <string> ();
                        
                        foreach (XmlAttribute xmlAttribute in taskElement.Attributes) {
                                if (xmlAttribute.Name == "Condition")
@@ -110,32 +113,34 @@ namespace Microsoft.Build.BuildEngine {
                                        continue;
                                tempNames.Add (xmlAttribute.Name);
                        }
-                       names = new string [tempNames.Count];
-                       foreach (string name in tempNames)
-                               names [attributesCount++] = name;
-                       return names;
+                       
+                       return tempNames.ToArray ();
                }
                
-               [MonoTODO]
                public string GetParameterValue (string attributeName)
                {
+                       if (attributeName == "Condition")
+                               throw new ArgumentException ("Condition attribute cannot be accessed using this method.");
+                       if (attributeName == "ContinueOnError")
+                               throw new ArgumentException ("ContinueOnError attribute cannot be accessed using this method.");
+
                        return taskElement.GetAttribute (attributeName);
                }
                
-               [MonoTODO]
                public void SetParameterValue (string parameterName,
                                               string parameterValue)
                {
                        SetParameterValue (parameterName, parameterValue, false);
                }
                
-               [MonoTODO]
                public void SetParameterValue (string parameterName,
                                               string parameterValue,
                                               bool treatParameterValueAsLiteral)
                {
-                       // FIXME: use expression for parameterValue
-                       taskElement.SetAttribute (parameterName, parameterValue);
+                       if (treatParameterValueAsLiteral)
+                               taskElement.SetAttribute (parameterName, Utilities.Escape (parameterValue));
+                       else
+                               taskElement.SetAttribute (parameterName, parameterValue);
                }
                
                private void LogTaskStarted ()
@@ -163,9 +168,9 @@ namespace Microsoft.Build.BuildEngine {
                        return task;
                }
                
-               private IDictionary GetParameters ()
+               private IDictionary <string, string> GetParameters ()
                {
-                       IDictionary parameters = new Hashtable ();
+                       Dictionary <string, string> parameters = new Dictionary <string, string> ();
                        
                        string[] parameterNames = GetParameterNames ();
                        
@@ -176,22 +181,29 @@ namespace Microsoft.Build.BuildEngine {
                        return parameters;
                }
                
-               [MonoTODO]
                public string Condition {
-                       get { return condition.Value; }
-                       set { condition.Value = value; }
+                       get {
+                               return taskElement.GetAttribute ("Condition");
+                       }
+                       set {
+                               taskElement.SetAttribute ("Condition", value);
+                       }
                }
 
                [MonoTODO]
                public bool ContinueOnError {
                        get {
-                               if (continueOnError == null)
+                               string str = taskElement.GetAttribute ("ContinueOnError");
+                               if (str == String.Empty)
                                        return false;
-                               else
-                                       return Boolean.Parse (continueOnError.Value);
+                               else {
+                                       Expression exp = new Expression ();
+                                       exp.Parse (str);
+                                       return (bool) exp.ConvertTo (parentTarget.Project, typeof (bool));
+                               }
                        }
                        set {
-                               continueOnError.Value = value.ToString ();
+                               taskElement.SetAttribute ("ContinueOnError", value.ToString ());
                        }
                }
                
@@ -201,9 +213,8 @@ namespace Microsoft.Build.BuildEngine {
                        set { hostObject = value; }
                }
                
-               [MonoTODO]
                public string Name {
-                       get { return name; }
+                       get { return taskElement.Name; }
                }
                
                internal Target ParentTarget {
@@ -218,7 +229,7 @@ namespace Microsoft.Build.BuildEngine {
                
                [MonoTODO]
                public Type Type {
-                       get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (name); }
+                       get { return parentTarget.Project.TaskDatabase.GetTypeFromClassName (Name); }
                }
                
        }