2 // BuildItemGroup.cs: Represents a group of build items.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Reflection;
32 using System.Collections;
33 using System.Collections.Generic;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace Microsoft.Build.BuildEngine {
39 public class BuildItemGroup : IEnumerable {
41 List <BuildItem> buildItems;
42 ImportedProject importedProject;
43 XmlElement itemGroupElement;
44 GroupingCollection parentCollection;
45 Project parentProject;
48 public BuildItemGroup ()
49 : this (null, null, null, false)
53 internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
55 this.buildItems = new List <BuildItem> ();
56 this.importedProject = importedProject;
57 this.itemGroupElement = xmlElement;
58 this.parentProject = project;
59 this.read_only = readOnly;
64 foreach (XmlNode xn in xmlElement.ChildNodes) {
65 if (!(xn is XmlElement))
68 XmlElement xe = (XmlElement) xn;
69 BuildItem bi = new BuildItem (xe, this);
74 public BuildItem AddNewItem (string itemName,
77 return AddNewItem (itemName, itemInclude, false);
81 public BuildItem AddNewItem (string itemName,
83 bool treatItemIncludeAsLiteral)
85 if (treatItemIncludeAsLiteral)
86 itemInclude = Utilities.Escape (itemInclude);
88 BuildItem bi = new BuildItem (itemName, itemInclude);
90 bi.Evaluate (null, true);
101 itemGroupElement.RemoveAll ();
103 buildItems = new List <BuildItem> ();
105 if (parentProject != null) {
106 parentProject.MarkProjectAsDirty ();
107 parentProject.NeedToReevaluate ();
112 public BuildItemGroup Clone (bool deepClone)
116 throw new NotImplementedException ();
118 throw new NotImplementedException ();
121 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
123 throw new NotImplementedException ();
127 public IEnumerator GetEnumerator ()
129 return buildItems.GetEnumerator ();
132 public void RemoveItem (BuildItem itemToRemove)
134 if (itemToRemove == null)
137 itemToRemove.Detach ();
139 buildItems.Remove (itemToRemove);
142 public void RemoveItemAt (int index)
144 BuildItem item = buildItems [index];
149 public BuildItem[] ToArray ()
151 return buildItems.ToArray ();
154 internal void AddItem (BuildItem buildItem)
156 buildItems.Add (buildItem);
159 internal void AddItem (string name, ITaskItem taskItem)
162 buildItem = new BuildItem (name, taskItem);
163 buildItems.Add (buildItem);
166 internal string ConvertToString (Expression transform,
167 Expression separator)
169 string separatorString;
171 if (separator == null)
172 separatorString = ";";
174 separatorString = (string) separator.ConvertTo (parentProject, typeof (string));
176 string[] items = new string [buildItems.Count];
178 foreach (BuildItem bi in buildItems)
179 items [i++] = bi.ConvertToString (transform);
180 return String.Join (separatorString, items);
183 internal ITaskItem[] ConvertToITaskItemArray (Expression transform)
185 ITaskItem[] array = new ITaskItem [buildItems.Count];
187 foreach (BuildItem item in buildItems)
188 array [i++] = item.ConvertToITaskItem (transform);
192 internal void Detach ()
195 throw new InvalidOperationException ();
197 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
200 internal void Evaluate ()
202 foreach (BuildItem bi in buildItems) {
203 if (bi.Condition == String.Empty)
204 bi.Evaluate (parentProject, true);
206 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
207 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
212 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
214 int index = buildItems.IndexOf (item);
215 buildItems.RemoveAt (index);
216 buildItems.InsertRange (index, list);
219 public string Condition {
222 return itemGroupElement.GetAttribute ("Condition");
228 itemGroupElement.SetAttribute ("Condition", value);
230 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
235 get { return buildItems.Count; }
238 public bool IsImported {
239 get { return importedProject != null; }
244 public BuildItem this [int index] {
246 return buildItems [index];
250 internal GroupingCollection GroupingCollection {
251 get { return parentCollection; }
252 set { parentCollection = value; }
255 internal Project Project {
256 get { return parentProject; }
259 internal bool FromXml {
261 return itemGroupElement != null;
265 internal XmlElement XmlElement {
267 return itemGroupElement;