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 // In eval phase, any ref'ed item would've already been expanded
189 // or it doesnt exist, so dont expand again
190 // In non-eval, items have _already_ been expanded, so dont expand again
191 // So, ignore @options
192 internal string ConvertToString (Expression transform,
193 Expression separator, ExpressionOptions options)
195 string separatorString;
197 // Item refs are not expanded for separator or transform
198 if (separator == null)
199 separatorString = ";";
201 separatorString = (string) separator.ConvertTo (parentProject, typeof (string),
202 ExpressionOptions.DoNotExpandItemRefs);
204 string[] items = new string [buildItems.Count];
206 foreach (BuildItem bi in buildItems)
207 items [i++] = bi.ConvertToString (transform, ExpressionOptions.DoNotExpandItemRefs);
208 return String.Join (separatorString, items);
211 // In eval phase, any ref'ed item would've already been expanded
212 // or it doesnt exist, so dont expand again
213 // In non-eval, items have _already_ been expanded, so dont expand again
214 // So, ignore @options
215 internal ITaskItem[] ConvertToITaskItemArray (Expression transform, ExpressionOptions options)
217 ITaskItem[] array = new ITaskItem [buildItems.Count];
219 foreach (BuildItem item in buildItems)
220 array [i++] = item.ConvertToITaskItem (transform, ExpressionOptions.DoNotExpandItemRefs);
224 internal void Detach ()
227 throw new InvalidOperationException ();
229 itemGroupElement.ParentNode.RemoveChild (itemGroupElement);
232 internal void Evaluate ()
234 foreach (BuildItem bi in buildItems) {
235 if (bi.Condition == String.Empty)
236 bi.Evaluate (parentProject, true);
238 ConditionExpression ce = ConditionParser.ParseCondition (bi.Condition);
239 bi.Evaluate (parentProject, ce.BoolEvaluate (parentProject));
244 internal void ReplaceWith (BuildItem item, List <BuildItem> list)
246 int index = buildItems.IndexOf (item);
247 buildItems.RemoveAt (index);
248 buildItems.InsertRange (index, list);
251 public string Condition {
254 return itemGroupElement.GetAttribute ("Condition");
260 itemGroupElement.SetAttribute ("Condition", value);
262 throw new InvalidOperationException ("Cannot set a condition on an object not represented by an XML element in the project file.");
267 get { return buildItems.Count; }
270 public bool IsImported {
271 get { return importedProject != null; }
276 public BuildItem this [int index] {
278 return buildItems [index];
282 internal GroupingCollection GroupingCollection {
283 get { return parentCollection; }
284 set { parentCollection = value; }
287 internal Project ParentProject {
288 get { return parentProject; }
290 if (parentProject != null)
291 throw new InvalidOperationException ("parentProject is already set");
292 parentProject = value;
296 internal bool FromXml {
298 return itemGroupElement != null;
302 internal XmlElement XmlElement {
304 return itemGroupElement;