2007-02-03 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / BuildItemGroup.cs
index c21949eead4049019a1d90671483b99d7823707f..2e39a9dcd466ea7311a376e66715ff76dedf14f2 100644 (file)
@@ -39,22 +39,24 @@ namespace Microsoft.Build.BuildEngine {
        public class BuildItemGroup : IEnumerable {
        
                List <BuildItem>        buildItems;
-               GroupingCollection      parentCollection;
-               Project                 parentProject;
                ImportedProject         importedProject;
                XmlElement              itemGroupElement;
+               GroupingCollection      parentCollection;
+               Project                 parentProject;
+               bool                    read_only;
 
                public BuildItemGroup ()
-                       : this (null, null, null)
+                       : this (null, null, null, false)
                {
                }
                
-               internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject)
+               internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
                {
                        this.buildItems = new List <BuildItem> ();
                        this.importedProject = importedProject;
                        this.itemGroupElement = xmlElement;
                        this.parentProject = project;
+                       this.read_only = readOnly;
                        
                        if (!FromXml)
                                return;
@@ -66,6 +68,7 @@ namespace Microsoft.Build.BuildEngine {
                                XmlElement xe = (XmlElement) xn;
                                BuildItem bi = new BuildItem (xe, this);
                                buildItems.Add (bi);
+                               project.LastItemGroupContaining [bi.Name] = this;
                        }
                }
 
@@ -80,14 +83,44 @@ namespace Microsoft.Build.BuildEngine {
                                             string itemInclude,
                                             bool treatItemIncludeAsLiteral)
                {
-                       BuildItem bi = new BuildItem (itemName, itemInclude);
-                       buildItems.Add (bi);
-                       return bi;
+                       BuildItem item;
+
+                       if (treatItemIncludeAsLiteral)
+                               itemInclude = Utilities.Escape (itemInclude);
+
+                       if (FromXml) {
+                               XmlElement element = itemGroupElement.OwnerDocument.CreateElement (itemName, Project.XmlNamespace);
+                               itemGroupElement.AppendChild (element);
+                               element.SetAttribute ("Include", itemInclude);
+                               item = new BuildItem (element, this);
+                       } else {
+                               item = new BuildItem (itemName, itemInclude);
+                       }
+
+                       item.Evaluate (null, true);
+
+                       if (!read_only)
+                               buildItems.Add (item);
+
+                       if (parentProject != null) {
+                               parentProject.MarkProjectAsDirty ();
+                               parentProject.NeedToReevaluate ();
+                       }
+
+                       return item;
                }
                
                public void Clear ()
                {
+                       if (FromXml)
+                               itemGroupElement.RemoveAll ();
+                       
                        buildItems = new List <BuildItem> ();
+
+                       if (parentProject != null) {
+                               parentProject.MarkProjectAsDirty ();
+                               parentProject.NeedToReevaluate ();
+                       }
                }
 
                [MonoTODO]
@@ -113,12 +146,19 @@ namespace Microsoft.Build.BuildEngine {
 
                public void RemoveItem (BuildItem itemToRemove)
                {
+                       if (itemToRemove == null)
+                               return;
+
+                       itemToRemove.Detach ();
+
                        buildItems.Remove (itemToRemove);
                }
 
                public void RemoveItemAt (int index)
                {
-                       buildItems.RemoveAt (index);
+                       BuildItem item = buildItems [index];
+
+                       RemoveItem (item);
                }
 
                public BuildItem[] ToArray ()
@@ -164,20 +204,33 @@ namespace Microsoft.Build.BuildEngine {
                        return array;
                }
 
+               internal void Detach ()
+               {
+                       if (!FromXml)
+                               throw new InvalidOperationException ();
+
+                       itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
+               }
+
                internal void Evaluate ()
                {
                        foreach (BuildItem bi in buildItems) {
                                if (bi.Condition == String.Empty)
-                                       bi.Evaluate (true);
+                                       bi.Evaluate (parentProject, true);
                                else {
                                        ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
-                                       bi.Evaluate (ce.BoolEvaluate (parentProject));
+                                       bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
                                }
                        }
                }               
+
+               internal void ReplaceWith (BuildItem item, List <BuildItem> list)
+               {
+                       int index = buildItems.IndexOf (item);
+                       buildItems.RemoveAt (index);
+                       buildItems.InsertRange (index, list);
+               }
                
-               [MonoTODO]
-               // FIXME: whether we can invoke get_Condition on BuildItemGroup not based on XML
                public string Condition {
                        get {
                                if (FromXml)
@@ -188,6 +241,8 @@ namespace Microsoft.Build.BuildEngine {
                        set {
                                if (FromXml)
                                        itemGroupElement.SetAttribute ("Condition", value);
+                               else
+                                       throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
                        }
                }
 
@@ -212,7 +267,7 @@ namespace Microsoft.Build.BuildEngine {
                        set { parentCollection = value; }
                }
                
-               internal Project Project {
+               internal Project ParentProject {
                        get { return parentProject; }
                }
 
@@ -221,6 +276,12 @@ namespace Microsoft.Build.BuildEngine {
                                return itemGroupElement != null;
                        }
                }
+
+               internal XmlElement XmlElement {
+                       get {
+                               return itemGroupElement;
+                       }       
+               }
        }
 }