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;
49 public BuildItemGroup ()
50 : this (null, null, null, false)
54 internal BuildItemGroup (Project project)
55 : this (null, project, null, false)
59 internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
61 this.buildItems = new List <BuildItem> ();
62 this.importedProject = importedProject;
63 this.itemGroupElement = xmlElement;
64 this.parentProject = project;
65 this.read_only = readOnly;
70 foreach (XmlNode xn in xmlElement.ChildNodes) {
71 if (!(xn is XmlElement))
74 XmlElement xe = (XmlElement) xn;
75 BuildItem bi = new BuildItem (xe, this);
77 project.LastItemGroupContaining [bi.Name] = this;
81 public BuildItem AddNewItem (string itemName,
84 return AddNewItem (itemName, itemInclude, false);
88 public BuildItem AddNewItem (string itemName,
90 bool treatItemIncludeAsLiteral)
94 if (treatItemIncludeAsLiteral)
95 itemInclude = Utilities.Escape (itemInclude);
98 XmlElement element = itemGroupElement.OwnerDocument.CreateElement (itemName, Project.XmlNamespace);
99 itemGroupElement.AppendChild (element);
100 element.SetAttribute ("Include", itemInclude);
101 item = new BuildItem (element, this);
103 item = new BuildItem (itemName, itemInclude);
104 item.ParentItemGroup = this;
107 item.Evaluate (null, true);
110 buildItems.Add (item);
112 if (parentProject != null) {
113 parentProject.MarkProjectAsDirty ();
114 parentProject.NeedToReevaluate ();
123 itemGroupElement.RemoveAll ();
125 buildItems = new List <BuildItem> ();
127 if (parentProject != null) {
128 parentProject.MarkProjectAsDirty ();
129 parentProject.NeedToReevaluate ();
134 public BuildItemGroup Clone (bool deepClone)
138 throw new NotImplementedException ();
140 throw new NotImplementedException ();
143 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
145 throw new NotImplementedException ();
149 public IEnumerator GetEnumerator ()
151 return buildItems.GetEnumerator ();
154 public void RemoveItem (BuildItem itemToRemove)
156 if (itemToRemove == null)
159 itemToRemove.Detach ();
161 buildItems.Remove (itemToRemove);
164 public void RemoveItemAt (int index)
166 BuildItem item = buildItems [index];
171 public BuildItem[] ToArray ()
173 return buildItems.ToArray ();
176 internal void AddItem (BuildItem buildItem)
178 buildItems.Add (buildItem);
181 internal void AddItem (string name, ITaskItem taskItem)
184 buildItem = new BuildItem (name, taskItem);
185 buildItem.ParentItemGroup = this;
186 buildItems.Add (buildItem);
189 // In eval phase, any ref'ed item would've already been expanded
190 // or it doesnt exist, so dont expand again
191 // In non-eval, items have _already_ been expanded, so dont expand again
192 // So, ignore @options
193 internal string ConvertToString (Expression transform,
194 Expression separator, ExpressionOptions options)
196 string separatorString;
198 // Item refs are not expanded for separator or transform
199 if (separator == null)
200 separatorString = ";";
202 separatorString = (string) separator.ConvertTo (parentProject, typeof (string),
203 ExpressionOptions.DoNotExpandItemRefs);
205 string[] items = new string [buildItems.Count];
207 foreach (BuildItem bi in buildItems)
208 items [i++] = bi.ConvertToString (transform, ExpressionOptions.DoNotExpandItemRefs);
209 return String.Join (separatorString, items);
212 // In eval phase, any ref'ed item would've already been expanded
213 // or it doesnt exist, so dont expand again
214 // In non-eval, items have _already_ been expanded, so dont expand again
215 // So, ignore @options
216 internal ITaskItem[] ConvertToITaskItemArray (Expression transform, Expression separator, ExpressionOptions options)
218 if (separator != null)
219 // separator present, so return as a single "join'ed" string
220 return new ITaskItem [] {
221 new TaskItem (ConvertToString (transform, separator, options))
224 ITaskItem[] array = new ITaskItem [buildItems.Count];
226 foreach (BuildItem item in buildItems)
227 array [i++] = item.ConvertToITaskItem (transform, ExpressionOptions.DoNotExpandItemRefs);
231 internal void Detach ()
234 throw new InvalidOperationException ();
236 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
239 internal void Evaluate ()
243 foreach (BuildItem bi in buildItems) {
244 if (bi.Condition == String.Empty)
245 bi.Evaluate (parentProject, true);
247 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
248 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
254 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
256 int index = buildItems.IndexOf (item);
257 buildItems.RemoveAt (index);
258 buildItems.InsertRange (index, list);
261 public string Condition {
264 return itemGroupElement.GetAttribute ("Condition");
270 itemGroupElement.SetAttribute ("Condition", value);
272 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
277 get { return buildItems.Count; }
280 public bool IsImported {
281 get { return importedProject != null; }
286 public BuildItem this [int index] {
288 return buildItems [index];
292 internal GroupingCollection GroupingCollection {
293 get { return parentCollection; }
294 set { parentCollection = value; }
297 internal Project ParentProject {
298 get { return parentProject; }
300 if (parentProject != null)
301 throw new InvalidOperationException ("parentProject is already set");
302 parentProject = value;
306 internal bool FromXml {
308 return itemGroupElement != null;
312 internal XmlElement XmlElement {
314 return itemGroupElement;