implement some MS.Build members as well as adding missing members.
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Evaluation / Project.cs
1 //
2 // Project.cs
3 //
4 // Author:
5 //   Leszek Ciesielski (skolima@gmail.com)
6 //   Rolf Bjarne Kvinge (rolf@xamarin.com)
7 //
8 // (C) 2011 Leszek Ciesielski
9 // Copyright (C) 2011 Xamarin Inc. (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections.Generic;
33 using System.Diagnostics;
34 using System.IO;
35 using System.Linq;
36 using System.Text;
37 using System.Xml;
38 using Microsoft.Build.Construction;
39 using Microsoft.Build.Internal;
40 using Microsoft.Build.Execution;
41 using Microsoft.Build.Framework;
42 using Microsoft.Build.Logging;
43
44 namespace Microsoft.Build.Evaluation
45 {
46         [DebuggerDisplay ("{FullPath} EffectiveToolsVersion={ToolsVersion} #GlobalProperties="
47         + "{data.globalProperties.Count} #Properties={data.Properties.Count} #ItemTypes="
48         + "{data.ItemTypes.Count} #ItemDefinitions={data.ItemDefinitions.Count} #Items="
49         + "{data.Items.Count} #Targets={data.Targets.Count}")]
50         public class Project
51         {
52                 public Project (XmlReader xml)
53                         : this (ProjectRootElement.Create (xml))
54                 {
55                 }
56
57                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
58                                               string toolsVersion)
59                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
60                 {
61                 }
62
63                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
64                                               string toolsVersion, ProjectCollection projectCollection)
65                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
66                 {
67                 }
68
69                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
70                                               string toolsVersion, ProjectCollection projectCollection,
71                                               ProjectLoadSettings loadSettings)
72                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection, loadSettings)
73                 {
74                 }
75
76                 public Project (ProjectRootElement xml) : this (xml, null, null)
77                 {
78                 }
79
80                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
81                                               string toolsVersion)
82                         : this (xml, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
83                 {
84                 }
85
86                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
87                                               string toolsVersion, ProjectCollection projectCollection)
88                         : this (xml, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
89                 {
90                 }
91
92                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
93                                               string toolsVersion, ProjectCollection projectCollection,
94                                               ProjectLoadSettings loadSettings)
95                 {
96                         if (projectCollection == null)
97                                 throw new ArgumentNullException ("projectCollection");
98                         this.GlobalProperties = globalProperties ?? new Dictionary<string, string> ();
99                         this.ToolsVersion = toolsVersion;
100                         this.ProjectCollection = projectCollection;
101                         this.load_settings = loadSettings;
102
103                         Initialize ();
104                 }
105
106                 public Project (string projectFile)
107                         : this (projectFile, null, null)
108                 {
109                 }
110
111                 public Project (string projectFile, IDictionary<string, string> globalProperties,
112                                 string toolsVersion)
113                 : this (projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection, ProjectLoadSettings.Default)
114                 {
115                 }
116
117                 public Project (string projectFile, IDictionary<string, string> globalProperties,
118                                 string toolsVersion, ProjectCollection projectCollection)
119                 : this (projectFile, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
120                 {
121                 }
122
123                 public Project (string projectFile, IDictionary<string, string> globalProperties,
124                                 string toolsVersion, ProjectCollection projectCollection,
125                                 ProjectLoadSettings loadSettings)
126                         : this (ProjectRootElement.Create (projectFile), globalProperties, toolsVersion, projectCollection, loadSettings)
127                 {
128                 }
129
130                 ProjectLoadSettings load_settings;
131
132                 public IDictionary<string, string> GlobalProperties { get; private set; }
133
134                 public ProjectCollection ProjectCollection { get; private set; }
135
136                 public string ToolsVersion { get; private set; }
137
138                 public ProjectRootElement Xml { get; private set; }
139
140                 string dir_path, full_path;
141                 Dictionary<string, ProjectItemDefinition> item_definitions;
142                 List<ResolvedImport> raw_imports;
143                 List<ProjectItem> raw_items;
144                 List<string> item_types;
145                 List<ProjectProperty> properties;
146                 Dictionary<string, ProjectTargetInstance> targets;
147
148                 void Initialize ()
149                 {
150                         ConditionedProperties = new Dictionary<string, List<string>> ();
151                         dir_path = Directory.GetCurrentDirectory ();
152                         raw_imports = new List<ResolvedImport> ();
153                         item_definitions = new Dictionary<string, ProjectItemDefinition> ();
154                         item_types = new List<string> ();
155                         properties = new List<ProjectProperty> ();
156                         targets = new Dictionary<string, ProjectTargetInstance> ();
157                 }
158
159                 public ICollection<ProjectItem> GetItemsIgnoringCondition (string itemType)
160                 {
161                         return new CollectionFromEnumerable<ProjectItem> (
162                                 new FilteredEnumerable<ProjectItemElement> (Xml.Items).
163                                 Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)).
164                                 Select (p => new ProjectItem (p)));
165                 }
166
167                 public void RemoveItems (IEnumerable<ProjectItem> items)
168                 {
169                         var removal = new List<ProjectItem> (items);
170                         foreach (var item in removal) {
171                                 var parent = item.Xml.Parent;
172                                 parent.RemoveChild (item.Xml);
173                                 if (parent.Count == 0)
174                                         parent.Parent.RemoveChild (parent);
175                         }
176                 }
177
178                 static readonly Dictionary<string, string> empty_metadata = new Dictionary<string, string> ();
179
180                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude)
181                 {
182                         return AddItem (itemType, unevaluatedInclude, empty_metadata);
183                 }
184
185                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude,
186                                 IEnumerable<KeyValuePair<string, string>> metadata)
187                 {
188                         // FIXME: needs several check that AddItemFast() does not process (see MSDN for details).
189
190                         return AddItemFast (itemType, unevaluatedInclude, metadata);
191                 }
192
193                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude)
194                 {
195                         return AddItemFast (itemType, unevaluatedInclude, empty_metadata);
196                 }
197
198                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude,
199                                                                      IEnumerable<KeyValuePair<string, string>> metadata)
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 public bool Build ()
205                 {
206                         return Build (Xml.DefaultTargets.Split (';'));
207                 }
208
209                 public bool Build (IEnumerable<ILogger> loggers)
210                 {
211                         return Build (Xml.DefaultTargets.Split (';'), loggers);
212                 }
213
214                 public bool Build (string target)
215                 {
216                         return string.IsNullOrWhiteSpace (target) ? Build () : Build (new string [] {target});
217                 }
218
219                 public bool Build (string[] targets)
220                 {
221                         return Build (targets, new ILogger [0]);
222                 }
223
224                 public bool Build (ILogger logger)
225                 {
226                         return Build (Xml.DefaultTargets.Split (';'), new ILogger [] {logger});
227                 }
228
229                 public bool Build (string[] targets, IEnumerable<ILogger> loggers)
230                 {
231                         return Build (targets, loggers, new ForwardingLoggerRecord [0]);
232                 }
233
234                 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
235                 {
236                         return Build (Xml.DefaultTargets.Split (';'), loggers, remoteLoggers);
237                 }
238
239                 public bool Build (string target, IEnumerable<ILogger> loggers)
240                 {
241                         return Build (new string [] { target }, loggers);
242                 }
243
244                 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
245                 {
246                         throw new NotImplementedException ();
247                 }
248
249                 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
250                 {
251                         return Build (new string [] { target }, loggers, remoteLoggers);
252                 }
253
254                 public ProjectInstance CreateProjectInstance ()
255                 {
256                         throw new NotImplementedException ();
257                 }
258
259                 public string ExpandString (string unexpandedValue)
260                 {
261                         throw new NotImplementedException ();
262                 }
263
264                 public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
265                 {
266                         throw new NotImplementedException ();
267                 }
268
269                 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinition item)
270                 {
271                         throw new NotImplementedException ();
272                 }
273
274                 public ICollection<ProjectItem> GetItems (string itemType)
275                 {
276                         throw new NotImplementedException ();
277                 }
278
279                 public ICollection<ProjectItem> GetItemsByEvaluatedInclude (string evaluatedInclude)
280                 {
281                         throw new NotImplementedException ();
282                 }
283
284                 public IEnumerable<ProjectElement> GetLogicalProject ()
285                 {
286                         throw new NotImplementedException ();
287                 }
288
289                 public static string GetMetadataValueEscaped (ProjectMetadata metadatum)
290                 {
291                         throw new NotImplementedException ();
292                 }
293
294                 public static string GetMetadataValueEscaped (ProjectItem item, string name)
295                 {
296                         throw new NotImplementedException ();
297                 }
298
299                 public static string GetMetadataValueEscaped (ProjectItemDefinition item, string name)
300                 {
301                         throw new NotImplementedException ();
302                 }
303
304                 public string GetPropertyValue (string name)
305                 {
306                         throw new NotImplementedException ();
307                 }
308
309                 public static string GetPropertyValueEscaped (ProjectProperty property)
310                 {
311                         throw new NotImplementedException ();
312                 }
313
314                 public ProjectProperty GetProperty (string name)
315                 {
316                         throw new NotImplementedException ();
317                 }
318
319                 public void MarkDirty ()
320                 {
321                         throw new NotImplementedException ();
322                 }
323
324                 public void ReevaluateIfNecessary ()
325                 {
326                         throw new NotImplementedException ();
327                 }
328
329                 public bool RemoveGlobalProperty (string name)
330                 {
331                         throw new NotImplementedException ();
332                 }
333
334                 public bool RemoveItem (ProjectItem item)
335                 {
336                         throw new NotImplementedException ();
337                 }
338
339                 public bool RemoveProperty (ProjectProperty property)
340                 {
341                         throw new NotImplementedException ();
342                 }
343
344                 public void Save ()
345                 {
346                         using (var writer = new StreamWriter (FullPath))
347                                 Save (writer);
348                 }
349
350                 public void Save (TextWriter writer)
351                 {
352                         throw new NotImplementedException ();
353                 }
354
355                 public void Save (string path)
356                 {
357                         Save (path, Encoding.Default);
358                 }
359
360                 public void Save (Encoding encoding)
361                 {
362                         Save (FullPath, encoding);
363                 }
364
365                 public void Save (string path, Encoding encoding)
366                 {
367                         using (var writer = new StreamWriter (path, false, encoding))
368                                 Save (writer);
369                 }
370
371                 public void SaveLogicalProject (TextWriter writer)
372                 {
373                         throw new NotImplementedException ();
374                 }
375
376                 public bool SetGlobalProperty (string name, string escapedValue)
377                 {
378                         throw new NotImplementedException ();
379                 }
380
381                 public ProjectProperty SetProperty (string name, string unevaluatedValue)
382                 {
383                         throw new NotImplementedException ();
384                 }
385
386                 public ICollection<ProjectMetadata> AllEvaluatedItemDefinitionMetadata { get; private set; }
387
388                 public ICollection<ProjectItem> AllEvaluatedItems { get; private set; }
389
390                 public ICollection<ProjectProperty> AllEvaluatedProperties { get; private set; }
391
392                 public IDictionary<string, List<string>> ConditionedProperties { get; private set; }
393
394                 public string DirectoryPath {
395                         get { return dir_path; }
396                 }
397
398                 public bool DisableMarkDirty { get; set; }
399
400                 public int EvaluationCounter {
401                         get { throw new NotImplementedException (); }
402                 }
403
404                 public string FullPath {
405                         get { return full_path; }
406                         set {
407                                 // FIXME: check validity? mark IsDirty?
408                                 full_path = value;
409                         }
410                 }
411
412                 public IList<ResolvedImport> Imports {
413                         get { throw new NotImplementedException (); }
414                 }
415
416                 public IList<ResolvedImport> ImportsIncludingDuplicates {
417                         get { return raw_imports; }
418                 }
419
420                 public bool IsBuildEnabled {
421                         get { throw new NotImplementedException (); }
422                 }
423
424                 public bool IsDirty {
425                         get { throw new NotImplementedException (); }
426                 }
427
428                 public IDictionary<string, ProjectItemDefinition> ItemDefinitions {
429                         get { return item_definitions; }
430                 }
431
432                 public ICollection<ProjectItem> Items {
433                         get { throw new NotImplementedException (); }
434                 }
435
436                 public ICollection<ProjectItem> ItemsIgnoringCondition {
437                         get { return raw_items; }
438                 }
439
440                 public ICollection<string> ItemTypes {
441                         get { return item_types; }
442                 }
443
444                 public ICollection<ProjectProperty> Properties {
445                         get { return properties; }
446                 }
447
448                 public bool SkipEvaluation { get; set; }
449
450                 public IDictionary<string, ProjectTargetInstance> Targets {
451                         get { return targets; }
452                 }
453         }
454 }