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