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.
29 using System.Reflection;
30 using System.Collections;
31 using System.Collections.Generic;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
36 namespace Microsoft.Build.BuildEngine {
37 public class BuildItemGroup : IEnumerable {
39 List <BuildItem> buildItems;
40 ImportedProject importedProject;
41 XmlElement itemGroupElement;
42 GroupingCollection parentCollection;
43 Project parentProject;
47 public BuildItemGroup ()
48 : this (null, null, null, false)
52 internal BuildItemGroup (Project project)
53 : this (null, project, null, false)
57 internal BuildItemGroup (XmlElement xmlElement, Project project, ImportedProject importedProject, bool readOnly)
59 this.buildItems = new List <BuildItem> ();
60 this.importedProject = importedProject;
61 this.itemGroupElement = xmlElement;
62 this.parentProject = project;
63 this.read_only = readOnly;
68 foreach (XmlNode xn in xmlElement.ChildNodes) {
69 if (!(xn is XmlElement))
72 XmlElement xe = (XmlElement) xn;
73 BuildItem bi = new BuildItem (xe, this);
75 project.LastItemGroupContaining [bi.Name] = this;
78 DefinedInFileName = importedProject != null ? importedProject.FullFileName :
79 project != null ? project.FullFileName : null;
82 public BuildItem AddNewItem (string itemName,
85 return AddNewItem (itemName, itemInclude, false);
89 public BuildItem AddNewItem (string itemName,
91 bool treatItemIncludeAsLiteral)
95 if (treatItemIncludeAsLiteral)
96 itemInclude = Utilities.Escape (itemInclude);
99 XmlElement element = itemGroupElement.OwnerDocument.CreateElement (itemName, Project.XmlNamespace);
100 itemGroupElement.AppendChild (element);
101 element.SetAttribute ("Include", itemInclude);
102 item = new BuildItem (element, this);
104 item = new BuildItem (itemName, itemInclude);
105 item.ParentItemGroup = this;
108 item.Evaluate (null, true);
111 buildItems.Add (item);
113 if (parentProject != null) {
114 parentProject.MarkProjectAsDirty ();
115 parentProject.NeedToReevaluate ();
124 itemGroupElement.RemoveAll ();
126 buildItems = new List <BuildItem> ();
128 if (parentProject != null) {
129 parentProject.MarkProjectAsDirty ();
130 parentProject.NeedToReevaluate ();
135 public BuildItemGroup Clone (bool deepClone)
139 throw new NotImplementedException ();
141 throw new NotImplementedException ();
144 throw new InvalidOperationException ("A shallow clone of this object cannot be created.");
146 throw new NotImplementedException ();
150 public IEnumerator GetEnumerator ()
152 return buildItems.GetEnumerator ();
155 public void RemoveItem (BuildItem itemToRemove)
157 if (itemToRemove == null)
160 itemToRemove.Detach ();
162 buildItems.Remove (itemToRemove);
165 public void RemoveItemAt (int index)
167 BuildItem item = buildItems [index];
172 public BuildItem[] ToArray ()
174 return buildItems.ToArray ();
177 internal void AddItem (BuildItem buildItem)
179 buildItems.Add (buildItem);
182 internal void AddItem (string name, ITaskItem taskItem)
185 buildItem = new BuildItem (name, taskItem);
186 buildItem.ParentItemGroup = this;
187 buildItems.Add (buildItem);
190 // In eval phase, any ref'ed item would've already been expanded
191 // or it doesnt exist, so dont expand again
192 // In non-eval, items have _already_ been expanded, so dont expand again
193 // So, ignore @options
194 internal string ConvertToString (Expression transform,
195 Expression separator, ExpressionOptions options)
197 string separatorString;
199 // Item refs are not expanded for separator or transform
200 if (separator == null)
201 separatorString = ";";
203 separatorString = (string) separator.ConvertTo (parentProject, typeof (string),
204 ExpressionOptions.DoNotExpandItemRefs);
206 string[] items = new string [buildItems.Count];
208 foreach (BuildItem bi in buildItems)
209 items [i++] = bi.ConvertToString (transform, ExpressionOptions.DoNotExpandItemRefs);
210 return String.Join (separatorString, items);
213 // In eval phase, any ref'ed item would've already been expanded
214 // or it doesnt exist, so dont expand again
215 // In non-eval, items have _already_ been expanded, so dont expand again
216 // So, ignore @options
217 internal ITaskItem[] ConvertToITaskItemArray (Expression transform, Expression separator, ExpressionOptions options)
219 if (separator != null)
220 // separator present, so return as a single "join'ed" string
221 return new ITaskItem [] {
222 new TaskItem (ConvertToString (transform, separator, options))
225 ITaskItem[] array = new ITaskItem [buildItems.Count];
227 foreach (BuildItem item in buildItems)
228 array [i++] = item.ConvertToITaskItem (transform, ExpressionOptions.DoNotExpandItemRefs);
232 internal void Detach ()
235 throw new InvalidOperationException ();
237 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
240 internal void Evaluate ()
244 foreach (BuildItem bi in buildItems) {
245 if (bi.Condition == String.Empty)
246 bi.Evaluate (parentProject, true);
248 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
249 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
255 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
257 int index = buildItems.IndexOf (item);
258 buildItems.RemoveAt (index);
259 buildItems.InsertRange (index, list);
262 public string Condition {
265 return itemGroupElement.GetAttribute ("Condition");
271 itemGroupElement.SetAttribute ("Condition", value);
273 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
278 get { return buildItems.Count; }
281 public bool IsImported {
282 get { return importedProject != null; }
287 public BuildItem this [int index] {
289 return buildItems [index];
293 internal GroupingCollection GroupingCollection {
294 get { return parentCollection; }
295 set { parentCollection = value; }
298 internal Project ParentProject {
299 get { return parentProject; }
301 if (parentProject != null)
302 throw new InvalidOperationException ("parentProject is already set");
303 parentProject = value;
307 internal string DefinedInFileName { get; private set; }
309 internal bool FromXml {
311 return itemGroupElement != null;
315 internal XmlElement XmlElement {
317 return itemGroupElement;