5 // Leszek Ciesielski (skolima@gmail.com)
6 // Rolf Bjarne Kvinge (rolf@xamarin.com)
7 // Atsushi Enomoto (atsushi@xamarin.com)
9 // (C) 2011 Leszek Ciesielski
10 // Copyright (C) 2011,2013 Xamarin Inc.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections.Generic;
34 using System.Diagnostics;
36 using Microsoft.Build.Construction;
38 using Microsoft.Build.Framework;
40 namespace Microsoft.Build.Evaluation
42 [DebuggerDisplay ("{ItemType}={EvaluatedInclude} [{UnevaluatedInclude}] #DirectMetadata={DirectMetadataCount}")]
43 public class ProjectItem
45 internal ProjectItem (Project project, ProjectItemElement xml, string evaluatedInclude)
47 this.project = project;
49 if (project.ItemDefinitions.ContainsKey (ItemType))
50 foreach (var md in project.ItemDefinitions [ItemType].Metadata)
52 foreach (var md in xml.Metadata)
53 metadata.Add (new ProjectMetadata (project, ItemType, metadata, m => metadata.Remove (m), md));
54 this.evaluated_include = evaluatedInclude;
55 is_imported = project.ProjectCollection.OngoingImports.Any ();
58 readonly Project project;
59 readonly ProjectItemElement xml;
60 readonly List<ProjectMetadata> metadata = new List<ProjectMetadata> ();
61 readonly bool is_imported;
62 readonly string evaluated_include;
64 internal string RecursiveDir { get; set; }
66 public ProjectMetadata GetMetadata (string name)
68 return metadata.FirstOrDefault (m => m.Name == name);
71 public string GetMetadataValue (string name)
74 throw new ArgumentNullException ("name");
75 var wk = ProjectCollection.GetWellKnownMetadata (name, EvaluatedInclude, project.GetFullPath, RecursiveDir);
78 var m = GetMetadata (name);
79 return m != null ? m.EvaluatedValue : string.Empty;
82 public bool HasMetadata (string name)
84 return GetMetadata (name) != null;
87 public bool RemoveMetadata (string name)
89 var m = GetMetadata (name);
92 return metadata.Remove (m);
95 public void Rename (string name)
97 throw new NotImplementedException ();
100 public ProjectMetadata SetMetadataValue (string name, string unevaluatedValue)
102 // This has to do several tasks:
103 // - it cannot directly change Xml.Metadata because the ProjectItemElement might be shared
104 // among multiple ProjectItems.
105 // - hence it has to create another ProjectItemElement instance and add it to the project
106 // XML construction, with specific Include value that is assigned to this instance, and
107 // metadata values that are assigned to this instance.
108 throw new NotImplementedException ();
111 public IEnumerable<ProjectMetadata> DirectMetadata {
113 var list = new List<ProjectMetadata> ();
114 foreach (var xm in xml.Metadata)
115 yield return new ProjectMetadata (project, ItemType, list, p => list.Remove (p), xm);
119 public int DirectMetadataCount {
120 get { return xml.Metadata.Count; }
123 public string EvaluatedInclude {
124 get { return evaluated_include; }
127 public bool IsImported {
128 get { return is_imported; }
131 public string ItemType {
132 get { return Xml.ItemType; }
133 set { Xml.ItemType = value; }
136 public ICollection<ProjectMetadata> Metadata {
137 get { return metadata; }
140 public int MetadataCount {
141 get { return metadata.Count; }
144 public Project Project {
145 get { return project; }
148 public string UnevaluatedInclude {
149 get { return Xml.Include; }
150 set { Xml.Include = value; }
153 public ProjectItemElement Xml {