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