Add missing built-in properties. Resolve properties on import paths. Resolve \ with...
[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 //   Atsushi Enomoto (atsushi@xamarin.com)
8 //
9 // (C) 2011 Leszek Ciesielski
10 // Copyright (C) 2011,2013 Xamarin Inc. (http://www.xamarin.com)
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.IO;
36 using System.Linq;
37 using System.Text;
38 using System.Xml;
39 using Microsoft.Build.Construction;
40 using Microsoft.Build.Exceptions;
41 using Microsoft.Build.Execution;
42 using Microsoft.Build.Framework;
43 using Microsoft.Build.Internal;
44 using Microsoft.Build.Logging;
45 using System.Collections;
46
47 // Basically there are two semantic Project object models and their relationship is not obvious
48 // (apart from Microsoft.Build.Construction.ProjectRootElement which is a "construction rule").
49 //
50 // Microsoft.Build.Evaluation.Project holds some "editable" project model, and it supports
51 // detailed loader API (such as Items and AllEvaluatedItems).
52 // ProjectPoperty holds UnevaluatedValue and gives EvaluatedValue too.
53 //
54 // Microsoft.Build.Execution.ProjectInstance holds "snapshot" of a project, and it lacks
55 // detailed loader API. It does not give us Unevaluated property value.
56 // On the other hand, it supports Targets object model. What Microsoft.Build.Evaluation.Project
57 // offers there is actually a list of Microsoft.Build.Execution.ProjectInstance objects.
58 // It should be also noted that only ProjectInstance has Evaluate() method (Project doesn't).
59 //
60 // And both API holds different set of descendant types for each and cannot really share the
61 // loader code. That is lame.
62 //
63 // So, can either of them be used to construct the other model? Both API models share the same
64 // "governor", which is Microsoft.Build.Evaluation.ProjectCollection/ Project is added to
65 // its LoadedProjects list, while ProjectInstance isn't. Project cannot be loaded to load
66 // a ProjectInstance, at least within the same ProjectCollection.
67 //
68 // On the other hand, can ProjectInstance be used to load a Project? Maybe. Since Project and
69 // its descendants need Microsoft.Build.Construction.ProjectElement family as its API model
70 // is part of the public API. Then I still have to understand how those AllEvaluatedItems/
71 // AllEvaluatedProperties members make sense. EvaluationCounter is another propery in question.
72
73 namespace Microsoft.Build.Evaluation
74 {
75         [DebuggerDisplay ("{FullPath} EffectiveToolsVersion={ToolsVersion} #GlobalProperties="
76         + "{data.globalProperties.Count} #Properties={data.Properties.Count} #ItemTypes="
77         + "{data.ItemTypes.Count} #ItemDefinitions={data.ItemDefinitions.Count} #Items="
78         + "{data.Items.Count} #Targets={data.Targets.Count}")]
79         public class Project
80         {
81                 public Project (XmlReader xml)
82                         : this (ProjectRootElement.Create (xml))
83                 {
84                 }
85
86                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
87                                               string toolsVersion)
88                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
89                 {
90                 }
91
92                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
93                                               string toolsVersion, ProjectCollection projectCollection)
94                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
95                 {
96                 }
97
98                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
99                                               string toolsVersion, ProjectCollection projectCollection,
100                                               ProjectLoadSettings loadSettings)
101                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection, loadSettings)
102                 {
103                 }
104
105                 public Project (ProjectRootElement xml) : this (xml, null, null)
106                 {
107                 }
108
109                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
110                                               string toolsVersion)
111                         : this (xml, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
112                 {
113                 }
114
115                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
116                                               string toolsVersion, ProjectCollection projectCollection)
117                         : this (xml, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
118                 {
119                 }
120
121                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
122                                               string toolsVersion, ProjectCollection projectCollection,
123                                               ProjectLoadSettings loadSettings)
124                 {
125                         if (projectCollection == null)
126                                 throw new ArgumentNullException ("projectCollection");
127                         this.Xml = xml;
128                         this.GlobalProperties = globalProperties ?? new Dictionary<string, string> ();
129                         this.ToolsVersion = toolsVersion;
130                         this.ProjectCollection = projectCollection;
131                         this.load_settings = loadSettings;
132
133                         Initialize (null);
134                 }
135                 
136                 Project (ProjectRootElement imported, Project parent)
137                 {
138                         this.Xml = imported;
139                         this.GlobalProperties = parent.GlobalProperties;
140                         this.ToolsVersion = parent.ToolsVersion;
141                         this.ProjectCollection = parent.ProjectCollection;
142                         this.load_settings = parent.load_settings;
143
144                         Initialize (parent);
145                 }
146
147                 public Project (string projectFile)
148                         : this (projectFile, null, null)
149                 {
150                 }
151
152                 public Project (string projectFile, IDictionary<string, string> globalProperties,
153                                 string toolsVersion)
154                 : this (projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection, ProjectLoadSettings.Default)
155                 {
156                 }
157
158                 public Project (string projectFile, IDictionary<string, string> globalProperties,
159                                 string toolsVersion, ProjectCollection projectCollection)
160                 : this (projectFile, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
161                 {
162                 }
163
164                 public Project (string projectFile, IDictionary<string, string> globalProperties,
165                                 string toolsVersion, ProjectCollection projectCollection,
166                                 ProjectLoadSettings loadSettings)
167                         : this (ProjectRootElement.Create (projectFile), globalProperties, toolsVersion, projectCollection, loadSettings)
168                 {
169                 }
170
171                 ProjectLoadSettings load_settings;
172
173                 public IDictionary<string, string> GlobalProperties { get; private set; }
174
175                 public ProjectCollection ProjectCollection { get; private set; }
176
177                 public string ToolsVersion { get; private set; }
178
179                 public ProjectRootElement Xml { get; private set; }
180
181                 string dir_path;
182                 Dictionary<string, ProjectItemDefinition> item_definitions;
183                 List<ResolvedImport> raw_imports;
184                 List<ProjectItem> raw_items;
185                 List<ProjectItem> all_evaluated_items;
186                 List<ProjectProperty> properties;
187                 Dictionary<string, ProjectTargetInstance> targets;
188
189                 void Initialize (Project parent)
190                 {
191                         dir_path = Directory.GetCurrentDirectory ();
192                         raw_imports = new List<ResolvedImport> ();
193                         item_definitions = new Dictionary<string, ProjectItemDefinition> ();
194                         targets = new Dictionary<string, ProjectTargetInstance> ();
195                         raw_items = new List<ProjectItem> ();
196                         
197                         // FIXME: this is likely hack. Test ImportedProject.Properties to see what exactly should happen.
198                         if (parent != null) {
199                                 properties = parent.properties;
200                         } else {
201                                 properties = new List<ProjectProperty> ();
202                         
203                                 foreach (DictionaryEntry p in Environment.GetEnvironmentVariables ())
204                                         this.properties.Add (new EnvironmentProjectProperty (this, (string)p.Key, (string)p.Value));
205                                 foreach (var p in GlobalProperties)
206                                         this.properties.Add (new GlobalProjectProperty (this, p.Key, p.Value));
207                                 var tools = ProjectCollection.GetToolset (this.ToolsVersion) ?? ProjectCollection.GetToolset (this.ProjectCollection.DefaultToolsVersion);
208                                 foreach (var p in ProjectCollection.GetReservedProperties (tools, this))
209                                         this.properties.Add (p);
210                                 foreach (var p in ProjectCollection.GetWellKnownProperties (this))
211                                         this.properties.Add (p);
212                         }
213
214                         ProcessXml (parent);
215                         
216                         ProjectCollection.AddProject (this);
217                 }
218                 
219                 static readonly char [] item_sep = {';'};
220                 
221                 void ProcessXml (Project parent)
222                 {
223                         // this needs to be initialized here (regardless of that items won't be evaluated at property evaluation;
224                         // Conditions could incorrectly reference items and lack of this list causes NRE.
225                         all_evaluated_items = new List<ProjectItem> ();
226
227                         // property evaluation happens couple of times.
228                         // At first step, all non-imported properties are evaluated TOO, WHILE those properties are being evaluated.
229                         // This means, Include and IncludeGroup elements with Condition attribute MAY contain references to
230                         // properties and they will be expanded.
231                         var elements = EvaluatePropertiesAndImports (Xml.Children).ToArray (); // ToArray(): to not lazily evaluate elements.
232                         
233                         // next, evaluate items
234                         EvaluateItems (elements);
235                         
236                         // finally, evaluate targets and tasks
237                         EvaluateTasks (elements);
238                 }
239                 
240                 IEnumerable<ProjectElement> EvaluatePropertiesAndImports (IEnumerable<ProjectElement> elements)
241                 {
242                         // First step: evaluate Properties
243                         foreach (var child in elements) {
244                                 yield return child;
245                                 var pge = child as ProjectPropertyGroupElement;
246                                 if (pge != null && Evaluate (pge.Condition))
247                                         foreach (var p in pge.Properties)
248                                                 // do not allow overwriting reserved or well-known properties by user
249                                                 if (!this.properties.Any (_ => (_.IsReservedProperty || _.IsWellKnownProperty) && _.Name.Equals (p.Name, StringComparison.InvariantCultureIgnoreCase)))
250                                                         if (Evaluate (p.Condition))
251                                                                 this.properties.Add (new XmlProjectProperty (this, p, PropertyType.Normal, ProjectCollection.OngoingImports.Any ()));
252
253                                 var ige = child as ProjectImportGroupElement;
254                                 if (ige != null && Evaluate (ige.Condition)) {
255                                         foreach (var incc in ige.Imports) {
256                                                 foreach (var e in Import (incc))
257                                                         yield return e;
258                                         }
259                                 }
260                                 var inc = child as ProjectImportElement;
261                                 if (inc != null && Evaluate (inc.Condition))
262                                         foreach (var e in Import (inc))
263                                                 yield return e;
264                         }
265                 }
266
267                 void EvaluateItems (IEnumerable<ProjectElement> elements)
268                 {
269                         foreach (var child in elements) {
270                                 var ige = child as ProjectItemGroupElement;
271                                 if (ige != null) {
272                                         foreach (var p in ige.Items) {
273                                                 if (!Evaluate (ige.Condition) || !Evaluate (p.Condition))
274                                                         continue;
275                                                 var includes = ExpandString (p.Include).Split (item_sep, StringSplitOptions.RemoveEmptyEntries);
276                                                 var excludes = ExpandString (p.Exclude).Split (item_sep, StringSplitOptions.RemoveEmptyEntries);
277                                                 
278                                                 if (includes.Length == 0)
279                                                         continue;                                               
280                                                 if (includes.Length == 1 && includes [0].IndexOf ('*') < 0 && excludes.Length == 0) {
281                                                         // for most case - shortcut.
282                                                         var item = new ProjectItem (this, p, includes [0]);
283                                                         this.raw_items.Add (item);
284                                                         all_evaluated_items.Add (item);
285                                                 } else {
286                                                         var ds = new Microsoft.Build.BuildEngine.DirectoryScanner () {
287                                                                 BaseDirectory = new DirectoryInfo (DirectoryPath),
288                                                                 Includes = includes.Select (i => new ProjectTaskItem (p, i)).ToArray (),
289                                                                 Excludes = excludes.Select (e => new ProjectTaskItem (p, e)).ToArray (),
290                                                         };
291                                                         ds.Scan ();
292                                                         foreach (var taskItem in ds.MatchedItems) {
293                                                                 if (all_evaluated_items.Any (i => i.EvaluatedInclude == taskItem.ItemSpec && i.ItemType == p.ItemType))
294                                                                         continue; // skip duplicate
295                                                                 var item = new ProjectItem (this, p, taskItem.ItemSpec);
296                                                                 string recurse = taskItem.GetMetadata ("RecursiveDir");
297                                                                 if (!string.IsNullOrEmpty (recurse))
298                                                                         item.RecursiveDir = recurse;
299                                                                 this.raw_items.Add (item);
300                                                                 all_evaluated_items.Add (item);
301                                                         }
302                                                 }
303                                         }
304                                 }
305                                 var def = child as ProjectItemDefinitionGroupElement;
306                                 if (def != null) {
307                                         foreach (var p in def.ItemDefinitions) {
308                                                 if (Evaluate (p.Condition)) {
309                                                         ProjectItemDefinition existing;
310                                                         if (!item_definitions.TryGetValue (p.ItemType, out existing))
311                                                                 item_definitions.Add (p.ItemType, (existing = new ProjectItemDefinition (this, p.ItemType)));
312                                                         existing.AddItems (p);
313                                                 }
314                                         }
315                                 }
316                         }
317                         all_evaluated_items.Sort ((p1, p2) => string.Compare (p1.ItemType, p2.ItemType, StringComparison.OrdinalIgnoreCase));
318                 }
319                 
320                 void EvaluateTasks (IEnumerable<ProjectElement> elements)
321                 {
322                         foreach (var child in elements) {
323                                 var te = child as ProjectTargetElement;
324                                 if (te != null)
325                                         this.targets.Add (te.Name, new ProjectTargetInstance (te));
326                         }
327                 }
328                 
329                 IEnumerable<ProjectElement> Import (ProjectImportElement import)
330                 {
331                         string dir = ProjectCollection.GetEvaluationTimeThisFileDirectory (() => FullPath);
332                         string path = ExpandString (import.Project);
333                         path = Path.IsPathRooted (path) ? path : dir != null ? Path.Combine (dir, path) : Path.GetFullPath (path);
334                         if (ProjectCollection.OngoingImports.Contains (path)) {
335                                 switch (load_settings) {
336                                 case ProjectLoadSettings.RejectCircularImports:
337                                         throw new InvalidProjectFileException (import.Location, null, string.Format ("Circular imports was detected: {0} (resolved as \"{1}\") is already on \"importing\" stack", import.Project, path));
338                                 }
339                                 return new ProjectElement [0]; // do not import circular references
340                         }
341                         ProjectCollection.OngoingImports.Push (path);
342                         try {
343                                 using (var reader = XmlReader.Create (WindowsCompatibilityExtensions.NormalizeFilePath (path))) {
344                                         var root = ProjectRootElement.Create (reader, ProjectCollection);
345                                         raw_imports.Add (new ResolvedImport (import, root, true));
346                                         return this.EvaluatePropertiesAndImports (root.Children).ToArray ();
347                                 }
348                         } finally {
349                                 ProjectCollection.OngoingImports.Pop ();
350                         }
351                 }
352
353                 public ICollection<ProjectItem> GetItemsIgnoringCondition (string itemType)
354                 {
355                         return new CollectionFromEnumerable<ProjectItem> (raw_items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
356                 }
357
358                 public void RemoveItems (IEnumerable<ProjectItem> items)
359                 {
360                         var removal = new List<ProjectItem> (items);
361                         foreach (var item in removal) {
362                                 var parent = item.Xml.Parent;
363                                 parent.RemoveChild (item.Xml);
364                                 if (parent.Count == 0)
365                                         parent.Parent.RemoveChild (parent);
366                         }
367                 }
368
369                 static readonly Dictionary<string, string> empty_metadata = new Dictionary<string, string> ();
370
371                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude)
372                 {
373                         return AddItem (itemType, unevaluatedInclude, empty_metadata);
374                 }
375
376                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude,
377                                 IEnumerable<KeyValuePair<string, string>> metadata)
378                 {
379                         // FIXME: needs several check that AddItemFast() does not process (see MSDN for details).
380
381                         return AddItemFast (itemType, unevaluatedInclude, metadata);
382                 }
383
384                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude)
385                 {
386                         return AddItemFast (itemType, unevaluatedInclude, empty_metadata);
387                 }
388
389                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude,
390                                                                      IEnumerable<KeyValuePair<string, string>> metadata)
391                 {
392                         throw new NotImplementedException ();
393                 }
394                 
395                 static readonly char [] target_sep = new char[] {';'};
396
397                 public bool Build ()
398                 {
399                         return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries));
400                 }
401
402                 public bool Build (IEnumerable<ILogger> loggers)
403                 {
404                         return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), loggers);
405                 }
406
407                 public bool Build (string target)
408                 {
409                         return string.IsNullOrWhiteSpace (target) ? Build () : Build (new string [] {target});
410                 }
411
412                 public bool Build (string[] targets)
413                 {
414                         return Build (targets, new ILogger [0]);
415                 }
416
417                 public bool Build (ILogger logger)
418                 {
419                         return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), new ILogger [] {logger});
420                 }
421
422                 public bool Build (string[] targets, IEnumerable<ILogger> loggers)
423                 {
424                         return Build (targets, loggers, new ForwardingLoggerRecord [0]);
425                 }
426
427                 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
428                 {
429                         return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), loggers, remoteLoggers);
430                 }
431
432                 public bool Build (string target, IEnumerable<ILogger> loggers)
433                 {
434                         return Build (new string [] { target }, loggers);
435                 }
436
437                 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
438                 {
439                         // Unlike ProjectInstance.Build(), there is no place to fill outputs by targets, so ignore them
440                         // (i.e. we don't use the overload with output).
441                         //
442                         // This does not check FullPath, so don't call GetProjectInstanceForBuild() directly.
443                         return new BuildManager ().GetProjectInstanceForBuildInternal (this).Build (targets, loggers, remoteLoggers);
444                 }
445
446                 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
447                 {
448                         return Build (new string [] { target }, loggers, remoteLoggers);
449                 }
450
451                 public ProjectInstance CreateProjectInstance ()
452                 {
453                         var ret = new ProjectInstance (Xml, GlobalProperties, ToolsVersion, ProjectCollection);
454                         // FIXME: maybe fill other properties to the result.
455                         return ret;
456                 }
457                 
458                 bool Evaluate (string unexpandedValue)
459                 {
460                         return string.IsNullOrWhiteSpace (unexpandedValue) || new ExpressionEvaluator (this, null).EvaluateAsBoolean (unexpandedValue);
461                 }
462
463                 public string ExpandString (string unexpandedValue)
464                 {
465                         return ExpandString (unexpandedValue, null);
466                 }
467                 
468                 string ExpandString (string unexpandedValue, string replacementForMissingStuff)
469                 {
470                         return new ExpressionEvaluator (this, replacementForMissingStuff).Evaluate (unexpandedValue);
471                 }
472
473                 public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
474                 {
475                         return ProjectCollection.Escape (item.EvaluatedInclude);
476                 }
477
478                 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinition item)
479                 {
480                         // ?? ItemDefinition does not have Include attribute. What's the point here?
481                         throw new NotImplementedException ();
482                 }
483
484                 public ICollection<ProjectItem> GetItems (string itemType)
485                 {
486                         return new CollectionFromEnumerable<ProjectItem> (Items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
487                 }
488
489                 public ICollection<ProjectItem> GetItemsByEvaluatedInclude (string evaluatedInclude)
490                 {
491                         return new CollectionFromEnumerable<ProjectItem> (Items.Where (p => p.EvaluatedInclude.Equals (evaluatedInclude, StringComparison.OrdinalIgnoreCase)));
492                 }
493
494                 public IEnumerable<ProjectElement> GetLogicalProject ()
495                 {
496                         throw new NotImplementedException ();
497                 }
498
499                 public static string GetMetadataValueEscaped (ProjectMetadata metadatum)
500                 {
501                         return ProjectCollection.Escape (metadatum.EvaluatedValue);
502                 }
503
504                 public static string GetMetadataValueEscaped (ProjectItem item, string name)
505                 {
506                         var md = item.GetMetadata (name);
507                         return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
508                 }
509
510                 public static string GetMetadataValueEscaped (ProjectItemDefinition item, string name)
511                 {
512                         var md = item.Metadata.FirstOrDefault (m => m.Name == name);
513                         return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
514                 }
515
516                 public string GetPropertyValue (string name)
517                 {
518                         var prop = GetProperty (name);
519                         return prop != null ? prop.EvaluatedValue : string.Empty;
520                 }
521
522                 public static string GetPropertyValueEscaped (ProjectProperty property)
523                 {
524                         // WTF happens here.
525                         return property.EvaluatedValue;
526                 }
527
528                 public ProjectProperty GetProperty (string name)
529                 {
530                         return properties.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
531                 }
532
533                 public void MarkDirty ()
534                 {
535                         if (!DisableMarkDirty)
536                                 is_dirty = true;
537                 }
538
539                 public void ReevaluateIfNecessary ()
540                 {
541                         throw new NotImplementedException ();
542                 }
543
544                 public bool RemoveGlobalProperty (string name)
545                 {
546                         throw new NotImplementedException ();
547                 }
548
549                 public bool RemoveItem (ProjectItem item)
550                 {
551                         throw new NotImplementedException ();
552                 }
553
554                 public bool RemoveProperty (ProjectProperty property)
555                 {
556                         var removed = properties.FirstOrDefault (p => p.Name.Equals (property.Name, StringComparison.OrdinalIgnoreCase));
557                         if (removed == null)
558                                 return false;
559                         properties.Remove (removed);
560                         return true;
561                 }
562
563                 public void Save ()
564                 {
565                         Xml.Save ();
566                 }
567
568                 public void Save (TextWriter writer)
569                 {
570                         Xml.Save (writer);
571                 }
572
573                 public void Save (string path)
574                 {
575                         Save (path, Encoding.Default);
576                 }
577
578                 public void Save (Encoding encoding)
579                 {
580                         Save (FullPath, encoding);
581                 }
582
583                 public void Save (string path, Encoding encoding)
584                 {
585                         using (var writer = new StreamWriter (path, false, encoding))
586                                 Save (writer);
587                 }
588
589                 public void SaveLogicalProject (TextWriter writer)
590                 {
591                         throw new NotImplementedException ();
592                 }
593
594                 public bool SetGlobalProperty (string name, string escapedValue)
595                 {
596                         throw new NotImplementedException ();
597                 }
598
599                 public ProjectProperty SetProperty (string name, string unevaluatedValue)
600                 {
601                         var p = new ManuallyAddedProjectProperty (this, name, unevaluatedValue);
602                         properties.Add (p);
603                         return p;
604                 }
605
606                 public ICollection<ProjectMetadata> AllEvaluatedItemDefinitionMetadata {
607                         get { throw new NotImplementedException (); }
608                 }
609
610                 public ICollection<ProjectItem> AllEvaluatedItems {
611                         get { return all_evaluated_items; }
612                 }
613
614                 public ICollection<ProjectProperty> AllEvaluatedProperties {
615                         get { return properties; }
616                 }
617
618                 public IDictionary<string, List<string>> ConditionedProperties {
619                         get {
620                                 // this property returns different instances every time.
621                                 var dic = new Dictionary<string, List<string>> ();
622                                 
623                                 // but I dunno HOW this evaluates
624                                 
625                                 throw new NotImplementedException ();
626                         }
627                 }
628
629                 public string DirectoryPath {
630                         get { return dir_path; }
631                 }
632
633                 public bool DisableMarkDirty { get; set; }
634
635                 public int EvaluationCounter {
636                         get { throw new NotImplementedException (); }
637                 }
638
639                 public string FullPath {
640                         get { return Xml.FullPath; }
641                         set { Xml.FullPath = value; }
642                 }
643                 
644                 class ResolvedImportComparer : IEqualityComparer<ResolvedImport>
645                 {
646                         public static ResolvedImportComparer Instance = new ResolvedImportComparer ();
647                         
648                         public bool Equals (ResolvedImport x, ResolvedImport y)
649                         {
650                                 return x.ImportedProject.FullPath.Equals (y.ImportedProject.FullPath);
651                         }
652                         public int GetHashCode (ResolvedImport obj)
653                         {
654                                 return obj.ImportedProject.FullPath.GetHashCode ();
655                         }
656                 }
657
658                 public IList<ResolvedImport> Imports {
659                         get { return raw_imports.Distinct (ResolvedImportComparer.Instance).ToList (); }
660                 }
661
662                 public IList<ResolvedImport> ImportsIncludingDuplicates {
663                         get { return raw_imports; }
664                 }
665
666                 public bool IsBuildEnabled {
667                         get { return ProjectCollection.IsBuildEnabled; }
668                 }
669
670                 bool is_dirty;
671                 public bool IsDirty {
672                         get { return is_dirty; }
673                 }
674
675                 public IDictionary<string, ProjectItemDefinition> ItemDefinitions {
676                         get { return item_definitions; }
677                 }
678
679                 [MonoTODO ("should be different from AllEvaluatedItems")]
680                 public ICollection<ProjectItem> Items {
681                         get { return AllEvaluatedItems; }
682                 }
683
684                 public ICollection<ProjectItem> ItemsIgnoringCondition {
685                         get { return raw_items; }
686                 }
687
688                 public ICollection<string> ItemTypes {
689                         get { return new CollectionFromEnumerable<string> (raw_items.Select (i => i.ItemType).Distinct ()); }
690                 }
691
692                 [MonoTODO ("should be different from AllEvaluatedProperties")]
693                 public ICollection<ProjectProperty> Properties {
694                         get { return AllEvaluatedProperties; }
695                 }
696
697                 public bool SkipEvaluation { get; set; }
698
699                 #if NET_4_5
700                 public
701                 #else
702                 internal
703                 #endif
704                 IDictionary<string, ProjectTargetInstance> Targets {
705                         get { return targets; }
706                 }
707                 
708                 // These are required for reserved property, represents dynamically changing property values.
709                 // This should resolve to either the project file path or that of the imported file.
710                 internal string GetEvaluationTimeThisFileDirectory ()
711                 {
712                         var file = GetEvaluationTimeThisFile ();
713                         var dir = Path.IsPathRooted (file) ? Path.GetDirectoryName (file) : Directory.GetCurrentDirectory ();
714                         return dir + Path.DirectorySeparatorChar;
715                 }
716
717                 internal string GetEvaluationTimeThisFile ()
718                 {
719                         return ProjectCollection.OngoingImports.Count > 0 ? ProjectCollection.OngoingImports.Peek () : FullPath ?? string.Empty;
720                 }
721                 
722                 internal string GetFullPath (string pathRelativeToProject)
723                 {
724                         if (Path.IsPathRooted (pathRelativeToProject))
725                                 return pathRelativeToProject;
726                         return Path.GetFullPath (Path.Combine (DirectoryPath, pathRelativeToProject));
727                 }
728         }
729 }