2007-01-10 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildPropertyGroup.cs
index fce679d43449af45a7c3f218be9c95f6c383ff78..d20027559483678e4edc059062834cfd4f5c8486 100644 (file)
@@ -37,6 +37,7 @@ using System.Xml;
 namespace Microsoft.Build.BuildEngine {
        public class BuildPropertyGroup : IEnumerable {
        
+               bool                    read_only;
                ImportedProject         importedProject;
                XmlElement              propertyGroup;
                GroupingCollection      parentCollection;
@@ -45,16 +46,17 @@ namespace Microsoft.Build.BuildEngine {
                Dictionary <string, BuildProperty>      propertiesByName;
 
                public BuildPropertyGroup ()
-                       : this (null, null, null)
+                       : this (null, null, null, false)
                {
                }
 
-               internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
+               internal BuildPropertyGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
                {
                        this.importedProject = importedProject;
                        this.parentCollection = null;
                        this.parentProject = project;
                        this.propertyGroup = xmlElement;
+                       this.read_only = readOnly;
 
                        if (FromXml) {
                                this.properties = new List <BuildProperty> ();
@@ -85,7 +87,7 @@ namespace Microsoft.Build.BuildEngine {
                        if (FromXml) {
                                XmlElement xe;
                                
-                               xe = propertyGroup.OwnerDocument.CreateElement (propertyName);
+                               xe = propertyGroup.OwnerDocument.CreateElement (propertyName, Project.XmlNamespace);
                                propertyGroup.AppendChild (xe);
                                
                                if (treatPropertyValueAsLiteral)
@@ -95,6 +97,7 @@ namespace Microsoft.Build.BuildEngine {
                                
                                prop = new BuildProperty (parentProject, xe);
                                AddProperty (prop);
+                               parentProject.EvaluatedProperties.AddProperty (prop);
                                return prop;
                        } else
                                throw new InvalidOperationException ("This method is only valid for persisted <System.Object[]> elements.");
@@ -119,9 +122,10 @@ namespace Microsoft.Build.BuildEngine {
                
                public void Clear ()
                {
-                       if (FromXml)
+                       if (FromXml) {
+                               propertyGroup.RemoveAll ();
                                properties = new List <BuildProperty> ();
-                       else
+                       else
                                propertiesByName = new Dictionary <string, BuildProperty> ();
                }
 
@@ -172,19 +176,27 @@ namespace Microsoft.Build.BuildEngine {
                        SetProperty (propertyName, propertyValue, false);
                }
                
+               // FIXME: add a test for SetProperty on property from xml
                public void SetProperty (string propertyName,
                                         string propertyValue,
                                         bool treatPropertyValueAsLiteral)
                {
-                       if (!propertiesByName.ContainsKey (propertyName)) {
-                               BuildProperty bp = new BuildProperty (propertyName, propertyValue);
-                               AddProperty (bp);
-                       }
+                       if (read_only)
+                               return;
+                       
+                       if (propertiesByName.ContainsKey (propertyName))
+                               propertiesByName.Remove (propertyName);
 
+                       BuildProperty bp;
                        if (treatPropertyValueAsLiteral)
-                               propertiesByName [propertyName].Value = Utilities.Escape (propertyValue);
+                               bp = new BuildProperty (propertyName, Utilities.Escape (propertyValue));
                        else
-                               propertiesByName [propertyName].Value = propertyValue;
+                               bp = new BuildProperty (propertyName, propertyValue);
+
+                       AddProperty (bp);
+
+                       if (IsGlobal)
+                               parentProject.ProcessXml ();
                }
                
                internal void Evaluate ()
@@ -238,6 +250,12 @@ namespace Microsoft.Build.BuildEngine {
                        }
                }
 
+               bool IsGlobal {
+                       get {
+                               return parentProject != null && propertyGroup == null;
+                       }
+               }
+
                public BuildProperty this [string propertyName] {
                        get {
                                if (FromXml)
@@ -257,6 +275,10 @@ namespace Microsoft.Build.BuildEngine {
                        get { return parentCollection; }
                        set { parentCollection = value; }
                }
+
+               internal XmlElement XmlElement {
+                       get { return propertyGroup; }
+               }
        }
 }