5 // Leszek Ciesielski (skolima@gmail.com)
6 // Rolf Bjarne Kvinge (rolf@xamarin.com)
7 // Atsushi Enomoto (atsushi@xamarin.com)
9 // (C) 2011 Leszek Ciesielski
10 // Copyright (C) 2011,2013 Xamarin Inc. (http://www.xamarin.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections.Generic;
34 using System.Diagnostics;
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;
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").
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.
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).
60 // And both API holds different set of descendant types for each and cannot really share the
61 // loader code. That is lame.
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.
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.
73 namespace Microsoft.Build.Evaluation
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}")]
81 public Project (XmlReader xml)
82 : this (ProjectRootElement.Create (xml))
86 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
88 : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
92 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
93 string toolsVersion, ProjectCollection projectCollection)
94 : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
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)
105 public Project (ProjectRootElement xml) : this (xml, null, null)
109 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
111 : this (xml, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
115 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
116 string toolsVersion, ProjectCollection projectCollection)
117 : this (xml, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
121 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
122 string toolsVersion, ProjectCollection projectCollection,
123 ProjectLoadSettings loadSettings)
125 if (projectCollection == null)
126 throw new ArgumentNullException ("projectCollection");
128 this.GlobalProperties = globalProperties ?? new Dictionary<string, string> ();
129 this.ToolsVersion = toolsVersion;
130 this.ProjectCollection = projectCollection;
131 this.load_settings = loadSettings;
136 Project (ProjectRootElement imported, Project parent)
139 this.GlobalProperties = parent.GlobalProperties;
140 this.ToolsVersion = parent.ToolsVersion;
141 this.ProjectCollection = parent.ProjectCollection;
142 this.load_settings = parent.load_settings;
147 public Project (string projectFile)
148 : this (projectFile, null, null)
152 public Project (string projectFile, IDictionary<string, string> globalProperties,
154 : this (projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection, ProjectLoadSettings.Default)
158 public Project (string projectFile, IDictionary<string, string> globalProperties,
159 string toolsVersion, ProjectCollection projectCollection)
160 : this (projectFile, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
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)
171 ProjectLoadSettings load_settings;
173 public IDictionary<string, string> GlobalProperties { get; private set; }
175 public ProjectCollection ProjectCollection { get; private set; }
177 public string ToolsVersion { get; private set; }
179 public ProjectRootElement Xml { get; private set; }
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;
189 void Initialize (Project parent)
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> ();
197 // FIXME: this is likely hack. Test ImportedProject.Properties to see what exactly should happen.
198 if (parent != null) {
199 properties = parent.properties;
201 properties = new List<ProjectProperty> ();
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);
216 ProjectCollection.AddProject (this);
219 static readonly char [] item_sep = {';'};
221 void ProcessXml (Project parent)
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> ();
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.
233 // next, evaluate items
234 EvaluateItems (elements);
236 // finally, evaluate targets and tasks
237 EvaluateTasks (elements);
240 IEnumerable<ProjectElement> EvaluatePropertiesAndImports (IEnumerable<ProjectElement> elements)
242 // First step: evaluate Properties
243 foreach (var child in elements) {
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 ()));
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))
260 var inc = child as ProjectImportElement;
261 if (inc != null && Evaluate (inc.Condition))
262 foreach (var e in Import (inc))
267 void EvaluateItems (IEnumerable<ProjectElement> elements)
269 foreach (var child in elements) {
270 var ige = child as ProjectItemGroupElement;
272 foreach (var p in ige.Items) {
273 if (!Evaluate (ige.Condition) || !Evaluate (p.Condition))
275 var includes = ExpandString (p.Include).Split (item_sep, StringSplitOptions.RemoveEmptyEntries);
276 var excludes = ExpandString (p.Exclude).Split (item_sep, StringSplitOptions.RemoveEmptyEntries);
278 if (includes.Length == 0)
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);
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 (),
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);
305 var def = child as ProjectItemDefinitionGroupElement;
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);
317 all_evaluated_items.Sort ((p1, p2) => string.Compare (p1.ItemType, p2.ItemType, StringComparison.OrdinalIgnoreCase));
320 void EvaluateTasks (IEnumerable<ProjectElement> elements)
322 foreach (var child in elements) {
323 var te = child as ProjectTargetElement;
325 this.targets.Add (te.Name, new ProjectTargetInstance (te));
329 IEnumerable<ProjectElement> Import (ProjectImportElement import)
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));
338 return new ProjectElement [0]; // do not import circular references
340 ProjectCollection.OngoingImports.Push (path);
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 ();
348 ProjectCollection.OngoingImports.Pop ();
352 public ICollection<ProjectItem> GetItemsIgnoringCondition (string itemType)
354 return new CollectionFromEnumerable<ProjectItem> (raw_items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
357 public void RemoveItems (IEnumerable<ProjectItem> items)
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);
368 static readonly Dictionary<string, string> empty_metadata = new Dictionary<string, string> ();
370 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude)
372 return AddItem (itemType, unevaluatedInclude, empty_metadata);
375 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude,
376 IEnumerable<KeyValuePair<string, string>> metadata)
378 // FIXME: needs several check that AddItemFast() does not process (see MSDN for details).
380 return AddItemFast (itemType, unevaluatedInclude, metadata);
383 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude)
385 return AddItemFast (itemType, unevaluatedInclude, empty_metadata);
388 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude,
389 IEnumerable<KeyValuePair<string, string>> metadata)
391 throw new NotImplementedException ();
396 return Build (Xml.DefaultTargets.Split (';'));
399 public bool Build (IEnumerable<ILogger> loggers)
401 return Build (Xml.DefaultTargets.Split (';'), loggers);
404 public bool Build (string target)
406 return string.IsNullOrWhiteSpace (target) ? Build () : Build (new string [] {target});
409 public bool Build (string[] targets)
411 return Build (targets, new ILogger [0]);
414 public bool Build (ILogger logger)
416 return Build (Xml.DefaultTargets.Split (';'), new ILogger [] {logger});
419 public bool Build (string[] targets, IEnumerable<ILogger> loggers)
421 return Build (targets, loggers, new ForwardingLoggerRecord [0]);
424 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
426 return Build (Xml.DefaultTargets.Split (';'), loggers, remoteLoggers);
429 public bool Build (string target, IEnumerable<ILogger> loggers)
431 return Build (new string [] { target }, loggers);
434 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
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).
439 // This does not check FullPath, so don't call GetProjectInstanceForBuild() directly.
440 return new BuildManager ().GetProjectInstanceForBuildInternal (this).Build (targets, loggers, remoteLoggers);
443 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
445 return Build (new string [] { target }, loggers, remoteLoggers);
448 public ProjectInstance CreateProjectInstance ()
450 var ret = new ProjectInstance (Xml, GlobalProperties, ToolsVersion, ProjectCollection);
451 // FIXME: maybe fill other properties to the result.
455 bool Evaluate (string unexpandedValue)
457 return string.IsNullOrWhiteSpace (unexpandedValue) || new ExpressionEvaluator (this, null).EvaluateAsBoolean (unexpandedValue);
460 public string ExpandString (string unexpandedValue)
462 return ExpandString (unexpandedValue, null);
465 string ExpandString (string unexpandedValue, string replacementForMissingStuff)
467 return new ExpressionEvaluator (this, replacementForMissingStuff).Evaluate (unexpandedValue);
470 public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
472 return ProjectCollection.Escape (item.EvaluatedInclude);
475 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinition item)
477 // ?? ItemDefinition does not have Include attribute. What's the point here?
478 throw new NotImplementedException ();
481 public ICollection<ProjectItem> GetItems (string itemType)
483 return new CollectionFromEnumerable<ProjectItem> (Items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
486 public ICollection<ProjectItem> GetItemsByEvaluatedInclude (string evaluatedInclude)
488 return new CollectionFromEnumerable<ProjectItem> (Items.Where (p => p.EvaluatedInclude.Equals (evaluatedInclude, StringComparison.OrdinalIgnoreCase)));
491 public IEnumerable<ProjectElement> GetLogicalProject ()
493 throw new NotImplementedException ();
496 public static string GetMetadataValueEscaped (ProjectMetadata metadatum)
498 return ProjectCollection.Escape (metadatum.EvaluatedValue);
501 public static string GetMetadataValueEscaped (ProjectItem item, string name)
503 var md = item.GetMetadata (name);
504 return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
507 public static string GetMetadataValueEscaped (ProjectItemDefinition item, string name)
509 var md = item.Metadata.FirstOrDefault (m => m.Name == name);
510 return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
513 public string GetPropertyValue (string name)
515 var prop = GetProperty (name);
516 return prop != null ? prop.EvaluatedValue : string.Empty;
519 public static string GetPropertyValueEscaped (ProjectProperty property)
522 return property.EvaluatedValue;
525 public ProjectProperty GetProperty (string name)
527 return properties.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
530 public void MarkDirty ()
532 if (!DisableMarkDirty)
536 public void ReevaluateIfNecessary ()
538 throw new NotImplementedException ();
541 public bool RemoveGlobalProperty (string name)
543 throw new NotImplementedException ();
546 public bool RemoveItem (ProjectItem item)
548 throw new NotImplementedException ();
551 public bool RemoveProperty (ProjectProperty property)
553 var removed = properties.FirstOrDefault (p => p.Name.Equals (property.Name, StringComparison.OrdinalIgnoreCase));
556 properties.Remove (removed);
565 public void Save (TextWriter writer)
570 public void Save (string path)
572 Save (path, Encoding.Default);
575 public void Save (Encoding encoding)
577 Save (FullPath, encoding);
580 public void Save (string path, Encoding encoding)
582 using (var writer = new StreamWriter (path, false, encoding))
586 public void SaveLogicalProject (TextWriter writer)
588 throw new NotImplementedException ();
591 public bool SetGlobalProperty (string name, string escapedValue)
593 throw new NotImplementedException ();
596 public ProjectProperty SetProperty (string name, string unevaluatedValue)
598 var p = new ManuallyAddedProjectProperty (this, name, unevaluatedValue);
603 public ICollection<ProjectMetadata> AllEvaluatedItemDefinitionMetadata {
604 get { throw new NotImplementedException (); }
607 public ICollection<ProjectItem> AllEvaluatedItems {
608 get { return all_evaluated_items; }
611 public ICollection<ProjectProperty> AllEvaluatedProperties {
612 get { return properties; }
615 public IDictionary<string, List<string>> ConditionedProperties {
617 // this property returns different instances every time.
618 var dic = new Dictionary<string, List<string>> ();
620 // but I dunno HOW this evaluates
622 throw new NotImplementedException ();
626 public string DirectoryPath {
627 get { return dir_path; }
630 public bool DisableMarkDirty { get; set; }
632 public int EvaluationCounter {
633 get { throw new NotImplementedException (); }
636 public string FullPath {
637 get { return Xml.FullPath; }
638 set { Xml.FullPath = value; }
641 class ResolvedImportComparer : IEqualityComparer<ResolvedImport>
643 public static ResolvedImportComparer Instance = new ResolvedImportComparer ();
645 public bool Equals (ResolvedImport x, ResolvedImport y)
647 return x.ImportedProject.FullPath.Equals (y.ImportedProject.FullPath);
649 public int GetHashCode (ResolvedImport obj)
651 return obj.ImportedProject.FullPath.GetHashCode ();
655 public IList<ResolvedImport> Imports {
656 get { return raw_imports.Distinct (ResolvedImportComparer.Instance).ToList (); }
659 public IList<ResolvedImport> ImportsIncludingDuplicates {
660 get { return raw_imports; }
663 public bool IsBuildEnabled {
664 get { return ProjectCollection.IsBuildEnabled; }
668 public bool IsDirty {
669 get { return is_dirty; }
672 public IDictionary<string, ProjectItemDefinition> ItemDefinitions {
673 get { return item_definitions; }
676 [MonoTODO ("should be different from AllEvaluatedItems")]
677 public ICollection<ProjectItem> Items {
678 get { return AllEvaluatedItems; }
681 public ICollection<ProjectItem> ItemsIgnoringCondition {
682 get { return raw_items; }
685 public ICollection<string> ItemTypes {
686 get { return new CollectionFromEnumerable<string> (raw_items.Select (i => i.ItemType).Distinct ()); }
689 [MonoTODO ("should be different from AllEvaluatedProperties")]
690 public ICollection<ProjectProperty> Properties {
691 get { return AllEvaluatedProperties; }
694 public bool SkipEvaluation { get; set; }
701 IDictionary<string, ProjectTargetInstance> Targets {
702 get { return targets; }
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 ()
709 var file = GetEvaluationTimeThisFile ();
710 var dir = Path.IsPathRooted (file) ? Path.GetDirectoryName (file) : Directory.GetCurrentDirectory ();
711 return dir + Path.DirectorySeparatorChar;
714 internal string GetEvaluationTimeThisFile ()
716 return ProjectCollection.OngoingImports.Count > 0 ? ProjectCollection.OngoingImports.Peek () : FullPath ?? string.Empty;
719 internal string GetFullPath (string pathRelativeToProject)
721 if (Path.IsPathRooted (pathRelativeToProject))
722 return pathRelativeToProject;
723 return Path.GetFullPath (Path.Combine (DirectoryPath, pathRelativeToProject));