ProjectItems are created after splitting Include paths by ';'.
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Evaluation / ProjectItem.cs
1 //
2 // ProjectItem.cs
3 //
4 // Author:
5 //   Leszek Ciesielski (skolima@gmail.com)
6 //   Rolf Bjarne Kvinge (rolf@xamarin.com)
7 //   Atsushi Enomoto (atsushi@xamarin.com)
8 //
9 // (C) 2011 Leszek Ciesielski
10 // Copyright (C) 2011,2013 Xamarin Inc.
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.Collections.Generic;
34 using System.Diagnostics;
35 using System.Linq;
36 using Microsoft.Build.Construction;
37
38 namespace Microsoft.Build.Evaluation
39 {
40         [DebuggerDisplay ("{ItemType}={EvaluatedInclude} [{UnevaluatedInclude}] #DirectMetadata={DirectMetadataCount}")]
41         public class ProjectItem
42         {
43                 internal ProjectItem (Project project, ProjectItemElement xml, string evaluatedInclude)
44                 {
45                         this.project = project;
46                         this.xml = xml;
47                         if (project.ItemDefinitions.ContainsKey (ItemType))
48                                 foreach (var md in project.ItemDefinitions [ItemType].Metadata)
49                                         metadata.Add (md);
50                         foreach (var item in xml.Metadata)
51                                 metadata.Add (new ProjectMetadata (project, ItemType, metadata, m => metadata.Remove (m), item));
52                         evaluated_include = evaluatedInclude;
53                 }
54                 
55                 Project project;
56                 ProjectItemElement xml;
57                 List<ProjectMetadata> metadata = new List<ProjectMetadata> ();
58                 string evaluated_include;
59
60                 public ProjectMetadata GetMetadata (string name)
61                 {
62                         return metadata.FirstOrDefault (m => m.Name == name);
63                 }
64
65                 public string GetMetadataValue (string name)
66                 {
67                         var m = GetMetadata (name);
68                         return m != null ? m.EvaluatedValue : null;
69                 }
70
71                 public bool HasMetadata (string name)
72                 {
73                         return GetMetadata (name) != null;
74                 }
75
76                 public bool RemoveMetadata (string name)
77                 {
78                         var m = GetMetadata (name);
79                         if (m == null)
80                                 return false;
81                         return metadata.Remove (m);
82                 }
83
84                 public void Rename (string name)
85                 {
86                         throw new NotImplementedException ();
87                 }
88
89                 public ProjectMetadata SetMetadataValue (string name, string unevaluatedValue)
90                 {
91                         throw new NotImplementedException ();
92                 }
93
94                 public IEnumerable<ProjectMetadata> DirectMetadata {
95                         get {
96                                 var list = new List<ProjectMetadata> ();
97                                 foreach (var xm in xml.Metadata)
98                                         yield return new ProjectMetadata (project, ItemType, list, p => list.Remove (p), xm);
99                         }
100                 }
101
102                 public int DirectMetadataCount {
103                         get { return xml.Metadata.Count; }
104                 }
105
106                 public string EvaluatedInclude {
107                         get { return evaluated_include; }
108                 }
109
110                 public bool IsImported {
111                         get { throw new NotImplementedException (); }
112                 }
113
114                 public string ItemType {
115                         get { return Xml.ItemType; }
116                         set { Xml.ItemType = value; }
117                 }
118
119                 public ICollection<ProjectMetadata> Metadata {
120                         get { return metadata; }
121                 }
122
123                 public int MetadataCount {
124                         get { return metadata.Count; }
125                 }
126
127                 public Project Project {
128                         get { return project; }
129                 }
130
131                 public string UnevaluatedInclude {
132                         get { return Xml.Include; }
133                         set { Xml.Include = value; }
134                 }
135
136                 public ProjectItemElement Xml {
137                         get { return xml; }
138                 }
139         }
140 }