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)
87 if (treatItemIncludeAsLiteral)
88 itemInclude = Utilities.Escape (itemInclude);
91 XmlElement element = itemGroupElement.OwnerDocument.CreateElement (itemName, Project.XmlNamespace);
92 itemGroupElement.AppendChild (element);
93 element.SetAttribute ("Include", itemInclude);
94 item = new BuildItem (element, this);
96 item = new BuildItem (itemName, itemInclude);
99 item.Evaluate (null, true);
102 buildItems.Add (item);
104 if (parentProject != null) {
105 parentProject.MarkProjectAsDirty ();
106 parentProject.NeedToReevaluate ();
115 itemGroupElement.RemoveAll ();
117 buildItems = new List <BuildItem> ();
119 if (parentProject != null) {
120 parentProject.MarkProjectAsDirty ();
121 parentProject.NeedToReevaluate ();
126 public BuildItemGroup Clone (bool deepClone)
130 throw new NotImplementedException ();
132 throw new NotImplementedException ();
135 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
137 throw new NotImplementedException ();
141 public IEnumerator GetEnumerator ()
143 return buildItems.GetEnumerator ();
146 public void RemoveItem (BuildItem itemToRemove)
148 if (itemToRemove == null)
151 itemToRemove.Detach ();
153 buildItems.Remove (itemToRemove);
156 public void RemoveItemAt (int index)
158 BuildItem item = buildItems [index];
163 public BuildItem[] ToArray ()
165 return buildItems.ToArray ();
168 internal void AddItem (BuildItem buildItem)
170 buildItems.Add (buildItem);
173 internal void AddItem (string name, ITaskItem taskItem)
176 buildItem = new BuildItem (name, taskItem);
177 buildItems.Add (buildItem);
180 internal string ConvertToString (Expression transform,
181 Expression separator)
183 string separatorString;
185 if (separator == null)
186 separatorString = ";";
188 separatorString = (string) separator.ConvertTo (parentProject, typeof (string));
190 string[] items = new string [buildItems.Count];
192 foreach (BuildItem bi in buildItems)
193 items [i++] = bi.ConvertToString (transform);
194 return String.Join (separatorString, items);
197 internal ITaskItem[] ConvertToITaskItemArray (Expression transform)
199 ITaskItem[] array = new ITaskItem [buildItems.Count];
201 foreach (BuildItem item in buildItems)
202 array [i++] = item.ConvertToITaskItem (transform);
206 internal void Detach ()
209 throw new InvalidOperationException ();
211 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
214 internal void Evaluate ()
216 foreach (BuildItem bi in buildItems) {
217 if (bi.Condition == String.Empty)
218 bi.Evaluate (parentProject, true);
220 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
221 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
226 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
228 int index = buildItems.IndexOf (item);
229 buildItems.RemoveAt (index);
230 buildItems.InsertRange (index, list);
233 public string Condition {
236 return itemGroupElement.GetAttribute ("Condition");
242 itemGroupElement.SetAttribute ("Condition", value);
244 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
249 get { return buildItems.Count; }
252 public bool IsImported {
253 get { return importedProject != null; }
258 public BuildItem this [int index] {
260 return buildItems [index];
264 internal GroupingCollection GroupingCollection {
265 get { return parentCollection; }
266 set { parentCollection = value; }
269 internal Project ParentProject {
270 get { return parentProject; }
273 internal bool FromXml {
275 return itemGroupElement != null;
279 internal XmlElement XmlElement {
281 return itemGroupElement;