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 (Project project)
54 : this (null, project, null, false)
58 internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
60 this.buildItems = new List <BuildItem> ();
61 this.importedProject = importedProject;
62 this.itemGroupElement = xmlElement;
63 this.parentProject = project;
64 this.read_only = readOnly;
69 foreach (XmlNode xn in xmlElement.ChildNodes) {
70 if (!(xn is XmlElement))
73 XmlElement xe = (XmlElement) xn;
74 BuildItem bi = new BuildItem (xe, this);
76 project.LastItemGroupContaining [bi.Name] = this;
80 public BuildItem AddNewItem (string itemName,
83 return AddNewItem (itemName, itemInclude, false);
87 public BuildItem AddNewItem (string itemName,
89 bool treatItemIncludeAsLiteral)
93 if (treatItemIncludeAsLiteral)
94 itemInclude = Utilities.Escape (itemInclude);
97 XmlElement element = itemGroupElement.OwnerDocument.CreateElement (itemName, Project.XmlNamespace);
98 itemGroupElement.AppendChild (element);
99 element.SetAttribute ("Include", itemInclude);
100 item = new BuildItem (element, this);
102 item = new BuildItem (itemName, itemInclude);
103 item.ParentItemGroup = this;
106 item.Evaluate (null, true);
109 buildItems.Add (item);
111 if (parentProject != null) {
112 parentProject.MarkProjectAsDirty ();
113 parentProject.NeedToReevaluate ();
122 itemGroupElement.RemoveAll ();
124 buildItems = new List <BuildItem> ();
126 if (parentProject != null) {
127 parentProject.MarkProjectAsDirty ();
128 parentProject.NeedToReevaluate ();
133 public BuildItemGroup Clone (bool deepClone)
137 throw new NotImplementedException ();
139 throw new NotImplementedException ();
142 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
144 throw new NotImplementedException ();
148 public IEnumerator GetEnumerator ()
150 return buildItems.GetEnumerator ();
153 public void RemoveItem (BuildItem itemToRemove)
155 if (itemToRemove == null)
158 itemToRemove.Detach ();
160 buildItems.Remove (itemToRemove);
163 public void RemoveItemAt (int index)
165 BuildItem item = buildItems [index];
170 public BuildItem[] ToArray ()
172 return buildItems.ToArray ();
175 internal void AddItem (BuildItem buildItem)
177 buildItems.Add (buildItem);
180 internal void AddItem (string name, ITaskItem taskItem)
183 buildItem = new BuildItem (name, taskItem);
184 buildItem.ParentItemGroup = this;
185 buildItems.Add (buildItem);
188 internal string ConvertToString (Expression transform,
189 Expression separator)
191 string separatorString;
193 if (separator == null)
194 separatorString = ";";
196 separatorString = (string) separator.ConvertTo (parentProject, typeof (string));
198 string[] items = new string [buildItems.Count];
200 foreach (BuildItem bi in buildItems)
201 items [i++] = bi.ConvertToString (transform);
202 return String.Join (separatorString, items);
205 internal ITaskItem[] ConvertToITaskItemArray (Expression transform)
207 ITaskItem[] array = new ITaskItem [buildItems.Count];
209 foreach (BuildItem item in buildItems)
210 array [i++] = item.ConvertToITaskItem (transform);
214 internal void Detach ()
217 throw new InvalidOperationException ();
219 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
222 internal void Evaluate ()
224 foreach (BuildItem bi in buildItems) {
225 if (bi.Condition == String.Empty)
226 bi.Evaluate (parentProject, true);
228 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
229 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
234 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
236 int index = buildItems.IndexOf (item);
237 buildItems.RemoveAt (index);
238 buildItems.InsertRange (index, list);
241 public string Condition {
244 return itemGroupElement.GetAttribute ("Condition");
250 itemGroupElement.SetAttribute ("Condition", value);
252 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
257 get { return buildItems.Count; }
260 public bool IsImported {
261 get { return importedProject != null; }
266 public BuildItem this [int index] {
268 return buildItems [index];
272 internal GroupingCollection GroupingCollection {
273 get { return parentCollection; }
274 set { parentCollection = value; }
277 internal Project ParentProject {
278 get { return parentProject; }
280 if (parentProject != null)
281 throw new InvalidOperationException ("parentProject is already set");
282 parentProject = value;
286 internal bool FromXml {
288 return itemGroupElement != null;
292 internal XmlElement XmlElement {
294 return itemGroupElement;