Added some comments on loaders in the two project object models.
[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 = Path.IsPathRooted (import.Project) ? import.Project : dir != null ? Path.Combine (dir, import.Project) : Path.GetFullPath (import.Project);
333                         if (ProjectCollection.OngoingImports.Contains (path)) {
334                                 switch (load_settings) {
335                                 case ProjectLoadSettings.RejectCircularImports:
336                                         throw new InvalidProjectFileException (import.Location, null, string.Format ("Circular imports was detected: {0} is already on \"importing\" stack", path));
337                                 }
338                                 return new ProjectElement [0]; // do not import circular references
339                         }
340                         ProjectCollection.OngoingImports.Push (path);
341                         try {
342                                 using (var reader = XmlReader.Create (path)) {
343                                         var root = ProjectRootElement.Create (reader, ProjectCollection);
344                                         raw_imports.Add (new ResolvedImport (import, root, true));
345                                         return this.EvaluatePropertiesAndImports (root.Children).ToArray ();
346                                 }
347                         } finally {
348                                 ProjectCollection.OngoingImports.Pop ();
349                         }
350                 }
351
352                 public ICollection<ProjectItem> GetItemsIgnoringCondition (string itemType)
353                 {
354                         return new CollectionFromEnumerable<ProjectItem> (raw_items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
355                 }
356
357                 public void RemoveItems (IEnumerable<ProjectItem> items)
358                 {
359                         var removal = new List<ProjectItem> (items);
360                         foreach (var item in removal) {
361                                 var parent = item.Xml.Parent;
362                                 parent.RemoveChild (item.Xml);
363                                 if (parent.Count == 0)
364                                         parent.Parent.RemoveChild (parent);
365                         }
366                 }
367
368                 static readonly Dictionary<string, string> empty_metadata = new Dictionary<string, string> ();
369
370                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude)
371                 {
372                         return AddItem (itemType, unevaluatedInclude, empty_metadata);
373                 }
374
375                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude,
376                                 IEnumerable<KeyValuePair<string, string>> metadata)
377                 {
378                         // FIXME: needs several check that AddItemFast() does not process (see MSDN for details).
379
380                         return AddItemFast (itemType, unevaluatedInclude, metadata);
381                 }
382
383                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude)
384                 {
385                         return AddItemFast (itemType, unevaluatedInclude, empty_metadata);
386                 }
387
388                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude,
389                                                                      IEnumerable<KeyValuePair<string, string>> metadata)
390                 {
391                         throw new NotImplementedException ();
392                 }
393
394                 public bool Build ()
395                 {
396                         return Build (Xml.DefaultTargets.Split (';'));
397                 }
398
399                 public bool Build (IEnumerable<ILogger> loggers)
400                 {
401                         return Build (Xml.DefaultTargets.Split (';'), loggers);
402                 }
403
404                 public bool Build (string target)
405                 {
406                         return string.IsNullOrWhiteSpace (target) ? Build () : Build (new string [] {target});
407                 }
408
409                 public bool Build (string[] targets)
410                 {
411                         return Build (targets, new ILogger [0]);
412                 }
413
414                 public bool Build (ILogger logger)
415                 {
416                         return Build (Xml.DefaultTargets.Split (';'), new ILogger [] {logger});
417                 }
418
419                 public bool Build (string[] targets, IEnumerable<ILogger> loggers)
420                 {
421                         return Build (targets, loggers, new ForwardingLoggerRecord [0]);
422                 }
423
424                 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
425                 {
426                         return Build (Xml.DefaultTargets.Split (';'), loggers, remoteLoggers);
427                 }
428
429                 public bool Build (string target, IEnumerable<ILogger> loggers)
430                 {
431                         return Build (new string [] { target }, loggers);
432                 }
433
434                 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
435                 {
436                         // Unlike ProjectInstance.Build(), there is no place to fill outputs by targets, so ignore them
437                         // (i.e. we don't use the overload with output).
438                         //
439                         // This does not check FullPath, so don't call GetProjectInstanceForBuild() directly.
440                         return new BuildManager ().GetProjectInstanceForBuildInternal (this).Build (targets, loggers, remoteLoggers);
441                 }
442
443                 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
444                 {
445                         return Build (new string [] { target }, loggers, remoteLoggers);
446                 }
447
448                 public ProjectInstance CreateProjectInstance ()
449                 {
450                         var ret = new ProjectInstance (Xml, GlobalProperties, ToolsVersion, ProjectCollection);
451                         // FIXME: maybe fill other properties to the result.
452                         return ret;
453                 }
454                 
455                 bool Evaluate (string unexpandedValue)
456                 {
457                         return string.IsNullOrWhiteSpace (unexpandedValue) || new ExpressionEvaluator (this, null).EvaluateAsBoolean (unexpandedValue);
458                 }
459
460                 public string ExpandString (string unexpandedValue)
461                 {
462                         return ExpandString (unexpandedValue, null);
463                 }
464                 
465                 string ExpandString (string unexpandedValue, string replacementForMissingStuff)
466                 {
467                         return new ExpressionEvaluator (this, replacementForMissingStuff).Evaluate (unexpandedValue);
468                 }
469
470                 public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
471                 {
472                         return ProjectCollection.Escape (item.EvaluatedInclude);
473                 }
474
475                 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinition item)
476                 {
477                         // ?? ItemDefinition does not have Include attribute. What's the point here?
478                         throw new NotImplementedException ();
479                 }
480
481                 public ICollection<ProjectItem> GetItems (string itemType)
482                 {
483                         return new CollectionFromEnumerable<ProjectItem> (Items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
484                 }
485
486                 public ICollection<ProjectItem> GetItemsByEvaluatedInclude (string evaluatedInclude)
487                 {
488                         return new CollectionFromEnumerable<ProjectItem> (Items.Where (p => p.EvaluatedInclude.Equals (evaluatedInclude, StringComparison.OrdinalIgnoreCase)));
489                 }
490
491                 public IEnumerable<ProjectElement> GetLogicalProject ()
492                 {
493                         throw new NotImplementedException ();
494                 }
495
496                 public static string GetMetadataValueEscaped (ProjectMetadata metadatum)
497                 {
498                         return ProjectCollection.Escape (metadatum.EvaluatedValue);
499                 }
500
501                 public static string GetMetadataValueEscaped (ProjectItem item, string name)
502                 {
503                         var md = item.GetMetadata (name);
504                         return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
505                 }
506
507                 public static string GetMetadataValueEscaped (ProjectItemDefinition item, string name)
508                 {
509                         var md = item.Metadata.FirstOrDefault (m => m.Name == name);
510                         return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
511                 }
512
513                 public string GetPropertyValue (string name)
514                 {
515                         var prop = GetProperty (name);
516                         return prop != null ? prop.EvaluatedValue : string.Empty;
517                 }
518
519                 public static string GetPropertyValueEscaped (ProjectProperty property)
520                 {
521                         // WTF happens here.
522                         return property.EvaluatedValue;
523                 }
524
525                 public ProjectProperty GetProperty (string name)
526                 {
527                         return properties.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
528                 }
529
530                 public void MarkDirty ()
531                 {
532                         if (!DisableMarkDirty)
533                                 is_dirty = true;
534                 }
535
536                 public void ReevaluateIfNecessary ()
537                 {
538                         throw new NotImplementedException ();
539                 }
540
541                 public bool RemoveGlobalProperty (string name)
542                 {
543                         throw new NotImplementedException ();
544                 }
545
546                 public bool RemoveItem (ProjectItem item)
547                 {
548                         throw new NotImplementedException ();
549                 }
550
551                 public bool RemoveProperty (ProjectProperty property)
552                 {
553                         var removed = properties.FirstOrDefault (p => p.Name.Equals (property.Name, StringComparison.OrdinalIgnoreCase));
554                         if (removed == null)
555                                 return false;
556                         properties.Remove (removed);
557                         return true;
558                 }
559
560                 public void Save ()
561                 {
562                         Xml.Save ();
563                 }
564
565                 public void Save (TextWriter writer)
566                 {
567                         Xml.Save (writer);
568                 }
569
570                 public void Save (string path)
571                 {
572                         Save (path, Encoding.Default);
573                 }
574
575                 public void Save (Encoding encoding)
576                 {
577                         Save (FullPath, encoding);
578                 }
579
580                 public void Save (string path, Encoding encoding)
581                 {
582                         using (var writer = new StreamWriter (path, false, encoding))
583                                 Save (writer);
584                 }
585
586                 public void SaveLogicalProject (TextWriter writer)
587                 {
588                         throw new NotImplementedException ();
589                 }
590
591                 public bool SetGlobalProperty (string name, string escapedValue)
592                 {
593                         throw new NotImplementedException ();
594                 }
595
596                 public ProjectProperty SetProperty (string name, string unevaluatedValue)
597                 {
598                         var p = new ManuallyAddedProjectProperty (this, name, unevaluatedValue);
599                         properties.Add (p);
600                         return p;
601                 }
602
603                 public ICollection<ProjectMetadata> AllEvaluatedItemDefinitionMetadata {
604                         get { throw new NotImplementedException (); }
605                 }
606
607                 public ICollection<ProjectItem> AllEvaluatedItems {
608                         get { return all_evaluated_items; }
609                 }
610
611                 public ICollection<ProjectProperty> AllEvaluatedProperties {
612                         get { return properties; }
613                 }
614
615                 public IDictionary<string, List<string>> ConditionedProperties {
616                         get {
617                                 // this property returns different instances every time.
618                                 var dic = new Dictionary<string, List<string>> ();
619                                 
620                                 // but I dunno HOW this evaluates
621                                 
622                                 throw new NotImplementedException ();
623                         }
624                 }
625
626                 public string DirectoryPath {
627                         get { return dir_path; }
628                 }
629
630                 public bool DisableMarkDirty { get; set; }
631
632                 public int EvaluationCounter {
633                         get { throw new NotImplementedException (); }
634                 }
635
636                 public string FullPath {
637                         get { return Xml.FullPath; }
638                         set { Xml.FullPath = value; }
639                 }
640                 
641                 class ResolvedImportComparer : IEqualityComparer<ResolvedImport>
642                 {
643                         public static ResolvedImportComparer Instance = new ResolvedImportComparer ();
644                         
645                         public bool Equals (ResolvedImport x, ResolvedImport y)
646                         {
647                                 return x.ImportedProject.FullPath.Equals (y.ImportedProject.FullPath);
648                         }
649                         public int GetHashCode (ResolvedImport obj)
650                         {
651                                 return obj.ImportedProject.FullPath.GetHashCode ();
652                         }
653                 }
654
655                 public IList<ResolvedImport> Imports {
656                         get { return raw_imports.Distinct (ResolvedImportComparer.Instance).ToList (); }
657                 }
658
659                 public IList<ResolvedImport> ImportsIncludingDuplicates {
660                         get { return raw_imports; }
661                 }
662
663                 public bool IsBuildEnabled {
664                         get { return ProjectCollection.IsBuildEnabled; }
665                 }
666
667                 bool is_dirty;
668                 public bool IsDirty {
669                         get { return is_dirty; }
670                 }
671
672                 public IDictionary<string, ProjectItemDefinition> ItemDefinitions {
673                         get { return item_definitions; }
674                 }
675
676                 [MonoTODO ("should be different from AllEvaluatedItems")]
677                 public ICollection<ProjectItem> Items {
678                         get { return AllEvaluatedItems; }
679                 }
680
681                 public ICollection<ProjectItem> ItemsIgnoringCondition {
682                         get { return raw_items; }
683                 }
684
685                 public ICollection<string> ItemTypes {
686                         get { return new CollectionFromEnumerable<string> (raw_items.Select (i => i.ItemType).Distinct ()); }
687                 }
688
689                 [MonoTODO ("should be different from AllEvaluatedProperties")]
690                 public ICollection<ProjectProperty> Properties {
691                         get { return AllEvaluatedProperties; }
692                 }
693
694                 public bool SkipEvaluation { get; set; }
695
696                 #if NET_4_5
697                 public
698                 #else
699                 internal
700                 #endif
701                 IDictionary<string, ProjectTargetInstance> Targets {
702                         get { return targets; }
703                 }
704                 
705                 // These are required for reserved property, represents dynamically changing property values.
706                 // This should resolve to either the project file path or that of the imported file.
707                 internal string GetEvaluationTimeThisFileDirectory ()
708                 {
709                         var file = GetEvaluationTimeThisFile ();
710                         var dir = Path.IsPathRooted (file) ? Path.GetDirectoryName (file) : Directory.GetCurrentDirectory ();
711                         return dir + Path.DirectorySeparatorChar;
712                 }
713
714                 internal string GetEvaluationTimeThisFile ()
715                 {
716                         return ProjectCollection.OngoingImports.Count > 0 ? ProjectCollection.OngoingImports.Peek () : FullPath ?? string.Empty;
717                 }
718                 
719                 internal string GetFullPath (string pathRelativeToProject)
720                 {
721                         if (Path.IsPathRooted (pathRelativeToProject))
722                                 return pathRelativeToProject;
723                         return Path.GetFullPath (Path.Combine (DirectoryPath, pathRelativeToProject));
724                 }
725         }
726 }