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.
30 using System.Reflection;
31 using System.Collections;
32 using System.Collections.Generic;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
37 namespace Microsoft.Build.BuildEngine {
38 public class BuildItemGroup : IEnumerable {
40 List <BuildItem> buildItems;
41 ImportedProject importedProject;
42 XmlElement itemGroupElement;
43 GroupingCollection parentCollection;
44 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)
60 : this (xmlElement, project, importedProject, readOnly, false)
64 internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly, bool dynamic)
66 this.buildItems = new List <BuildItem> ();
67 this.importedProject = importedProject;
68 this.itemGroupElement = xmlElement;
69 this.parentProject = project;
70 this.read_only = readOnly;
71 this.isDynamic = dynamic;
76 foreach (XmlNode xn in xmlElement.ChildNodes) {
77 if (!(xn is XmlElement))
80 XmlElement xe = (XmlElement) xn;
81 BuildItem bi = CreateItem (project, xe);
83 project.LastItemGroupContaining [bi.Name] = this;
86 DefinedInFileName = importedProject != null ? importedProject.FullFileName :
87 project != null ? project.FullFileName : null;
90 internal virtual BuildItem CreateItem (Project project, XmlElement xe)
92 return new BuildItem (xe, this);
95 public BuildItem AddNewItem (string itemName,
98 return AddNewItem (itemName, itemInclude, false);
102 public BuildItem AddNewItem (string itemName,
104 bool treatItemIncludeAsLiteral)
108 if (treatItemIncludeAsLiteral)
109 itemInclude = Utilities.Escape (itemInclude);
112 XmlElement element = itemGroupElement.OwnerDocument.CreateElement (itemName, Project.XmlNamespace);
113 itemGroupElement.AppendChild (element);
114 element.SetAttribute ("Include", itemInclude);
115 item = new BuildItem (element, this);
117 item = new BuildItem (itemName, itemInclude);
118 item.ParentItemGroup = this;
121 item.Evaluate (null, true);
124 buildItems.Add (item);
126 if (parentProject != null) {
127 parentProject.MarkProjectAsDirty ();
128 parentProject.NeedToReevaluate ();
137 itemGroupElement.RemoveAll ();
139 buildItems = new List <BuildItem> ();
141 if (parentProject != null) {
142 parentProject.MarkProjectAsDirty ();
143 parentProject.NeedToReevaluate ();
148 public BuildItemGroup Clone (bool deepClone)
152 throw new NotImplementedException ();
154 throw new NotImplementedException ();
157 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
159 throw new NotImplementedException ();
163 public IEnumerator GetEnumerator ()
165 return buildItems.GetEnumerator ();
168 public void RemoveItem (BuildItem itemToRemove)
170 if (itemToRemove == null)
173 itemToRemove.Detach ();
175 buildItems.Remove (itemToRemove);
178 public void RemoveItemAt (int index)
180 BuildItem item = buildItems [index];
185 internal BuildItem FindItem (ITaskItem taskItem)
187 return buildItems.FirstOrDefault (i => i.FinalItemSpec == taskItem.ItemSpec);
190 internal void RemoveItem (ITaskItem itemToRemove)
192 if (itemToRemove == null)
195 var item = FindItem (itemToRemove);
200 buildItems.Remove (item);
203 public BuildItem[] ToArray ()
205 return buildItems.ToArray ();
208 internal void AddItem (BuildItem buildItem)
210 buildItems.Add (buildItem);
213 internal void AddItem (string name, ITaskItem taskItem)
216 buildItem = new BuildItem (name, taskItem);
217 buildItem.ParentItemGroup = this;
218 buildItems.Add (buildItem);
221 // In eval phase, any ref'ed item would've already been expanded
222 // or it doesnt exist, so dont expand again
223 // In non-eval, items have _already_ been expanded, so dont expand again
224 // So, ignore @options
225 internal string ConvertToString (Expression transform,
226 Expression separator, ExpressionOptions options)
228 string separatorString;
230 // Item refs are not expanded for separator or transform
231 if (separator == null)
232 separatorString = ";";
234 separatorString = (string) separator.ConvertTo (parentProject, typeof (string),
235 ExpressionOptions.DoNotExpandItemRefs);
237 string[] items = new string [buildItems.Count];
239 foreach (BuildItem bi in buildItems)
240 items [i++] = bi.ConvertToString (transform, ExpressionOptions.DoNotExpandItemRefs);
241 return String.Join (separatorString, items);
244 // In eval phase, any ref'ed item would've already been expanded
245 // or it doesnt exist, so dont expand again
246 // In non-eval, items have _already_ been expanded, so dont expand again
247 // So, ignore @options
248 internal ITaskItem[] ConvertToITaskItemArray (Expression transform, Expression separator, ExpressionOptions options)
250 if (separator != null)
251 // separator present, so return as a single "join'ed" string
252 return new ITaskItem [] {
253 new TaskItem (ConvertToString (transform, separator, options))
256 ITaskItem[] array = new ITaskItem [buildItems.Count];
258 foreach (BuildItem item in buildItems)
259 array [i++] = item.ConvertToITaskItem (transform, ExpressionOptions.DoNotExpandItemRefs);
263 internal void Detach ()
266 throw new InvalidOperationException ();
268 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
271 internal void Evaluate ()
273 if (!isDynamic && evaluated)
275 foreach (BuildItem bi in buildItems) {
276 if (bi.Condition == String.Empty)
277 bi.Evaluate (parentProject, true);
279 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
280 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
286 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
288 int index = buildItems.IndexOf (item);
289 buildItems.RemoveAt (index);
290 buildItems.InsertRange (index, list);
293 public string Condition {
296 return itemGroupElement.GetAttribute ("Condition");
302 itemGroupElement.SetAttribute ("Condition", value);
304 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
309 get { return buildItems.Count; }
312 public bool IsImported {
313 get { return importedProject != null; }
318 public BuildItem this [int index] {
320 return buildItems [index];
324 internal GroupingCollection GroupingCollection {
325 get { return parentCollection; }
326 set { parentCollection = value; }
329 internal Project ParentProject {
330 get { return parentProject; }
332 if (parentProject != null)
333 throw new InvalidOperationException ("parentProject is already set");
334 parentProject = value;
338 internal string DefinedInFileName { get; private set; }
340 internal bool FromXml {
342 return itemGroupElement != null;
346 internal XmlElement XmlElement {
348 return itemGroupElement;
352 internal bool IsDynamic {