5 // Rolf Bjarne Kvinge (rolf@xamarin.com)
6 // Atsushi Enomoto (atsushi@xamarin.com)
8 // Copyright (C) 2011,2013 Xamarin Inc.
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
35 using Microsoft.Build.Construction;
36 using Microsoft.Build.Evaluation;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Internal.Expressions;
39 using Microsoft.Build.Logging;
42 // It is not always consistent to reuse Project and its evaluation stuff mostly because
43 // both BuildParameters.ctor() and Project.ctor() takes arbitrary ProjectCollection, which are not very likely eqivalent
44 // (as BuildParameters.ctor(), unlike Project.ctor(...), is known to create a new ProjectCollection instance).
46 // However, that inconsistency could happen even if you only use ProjectInstance and BuildParameters.
47 // They both have constructors that take ProjectCollection and there is no guarantee that the arguments are the same.
48 // BuildManager.Build() does not fail because of inconsistent ProjectCollection instance on .NET.
50 // Anyhow, I'm not going to instantiate Project within ProjectInstance code for another reason:
51 // ProjectCollection.GetLoadedProject() does not return any Project instnace for corresponding ProjectInstance
52 // (or I should say, ProjectRootElement for both).
53 using Microsoft.Build.Internal;
55 using Microsoft.Build.Exceptions;
59 namespace Microsoft.Build.Execution
61 public class ProjectInstance
65 public ProjectInstance (ProjectRootElement xml)
66 : this (xml, null, null, ProjectCollection.GlobalProjectCollection)
70 public ProjectInstance (string projectFile)
71 : this (projectFile, null, null, ProjectCollection.GlobalProjectCollection)
75 public ProjectInstance (string projectFile, IDictionary<string, string> globalProperties,
77 : this (projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
81 public ProjectInstance (ProjectRootElement xml, IDictionary<string, string> globalProperties,
82 string toolsVersion, ProjectCollection projectCollection)
84 projects = projectCollection;
85 global_properties = globalProperties ?? new Dictionary<string, string> ();
86 tools_version = !string.IsNullOrEmpty (toolsVersion) ? toolsVersion :
87 !string.IsNullOrEmpty (xml.ToolsVersion) ? xml.ToolsVersion :
88 projects.DefaultToolsVersion;
89 InitializeProperties (xml);
92 public ProjectInstance (string projectFile, IDictionary<string, string> globalProperties,
93 string toolsVersion, ProjectCollection projectCollection)
94 : this (ProjectRootElement.Create (projectFile), globalProperties, toolsVersion, projectCollection)
98 ProjectCollection projects;
99 IDictionary<string, string> global_properties;
101 string full_path, directory;
103 ElementLocation location;
106 Dictionary<string, ProjectItemDefinitionInstance> item_definitions;
107 List<ResolvedImport> raw_imports; // maybe we don't need this...
108 List<ProjectItemInstance> all_evaluated_items;
109 List<ProjectItemInstance> raw_items;
110 Dictionary<string,ProjectPropertyInstance> properties;
111 Dictionary<string, ProjectTargetInstance> targets;
112 string tools_version;
114 // FIXME: this is a duplicate code between Project and ProjectInstance
115 string [] GetDefaultTargets (ProjectRootElement xml)
117 var ret = GetDefaultTargets (xml, true, true);
118 return ret.Any () ? ret : GetDefaultTargets (xml, false, true);
121 string [] GetDefaultTargets (ProjectRootElement xml, bool fromAttribute, bool checkImports)
124 var ret = xml.DefaultTargets.Split (item_target_sep, StringSplitOptions.RemoveEmptyEntries).Select (s => s.Trim ()).ToArray ();
125 if (checkImports && ret.Length == 0) {
126 foreach (var imp in this.raw_imports) {
127 ret = GetDefaultTargets (imp.ImportedProject, true, false);
134 if (xml.Targets.Any ())
135 return new String [] { xml.Targets.First ().Name };
137 foreach (var imp in this.raw_imports) {
138 var ret = GetDefaultTargets (imp.ImportedProject, false, false);
143 return new string [0];
147 void InitializeProperties (ProjectRootElement xml)
150 location = xml.Location;
152 full_path = xml.FullPath;
153 directory = string.IsNullOrWhiteSpace (xml.DirectoryPath) ? System.IO.Directory.GetCurrentDirectory () : xml.DirectoryPath;
154 InitialTargets = xml.InitialTargets.Split (item_target_sep, StringSplitOptions.RemoveEmptyEntries).Select (s => s.Trim ()).ToList ();
156 raw_imports = new List<ResolvedImport> ();
157 item_definitions = new Dictionary<string, ProjectItemDefinitionInstance> ();
158 targets = new Dictionary<string, ProjectTargetInstance> ();
159 raw_items = new List<ProjectItemInstance> ();
161 properties = new Dictionary<string,ProjectPropertyInstance> ();
163 foreach (DictionaryEntry p in Environment.GetEnvironmentVariables ())
164 // FIXME: this is kind of workaround for unavoidable issue that PLATFORM=* is actually given
165 // on some platforms and that prevents setting default "PLATFORM=AnyCPU" property.
166 if (!string.Equals ("PLATFORM", (string) p.Key, StringComparison.OrdinalIgnoreCase))
167 this.properties [(string) p.Key] = new ProjectPropertyInstance ((string) p.Key, true, (string) p.Value);
168 foreach (var p in global_properties)
169 this.properties [p.Key] = new ProjectPropertyInstance (p.Key, false, p.Value);
170 var tools = projects.GetToolset (tools_version) ?? projects.GetToolset (projects.DefaultToolsVersion);
171 foreach (var p in projects.GetReservedProperties (tools, this, xml))
172 this.properties [p.Name] = p;
173 foreach (var p in ProjectCollection.GetWellKnownProperties (this))
174 this.properties [p.Name] = p;
178 DefaultTargets = GetDefaultTargets (xml).ToList ();
181 static readonly char [] item_target_sep = {';'};
183 void ProcessXml (ProjectRootElement xml)
185 UsingTasks = new List<ProjectUsingTaskElement> ();
187 // this needs to be initialized here (regardless of that items won't be evaluated at property evaluation;
188 // Conditions could incorrectly reference items and lack of this list causes NRE.
189 all_evaluated_items = new List<ProjectItemInstance> ();
191 // property evaluation happens couple of times.
192 // At first step, all non-imported properties are evaluated TOO, WHILE those properties are being evaluated.
193 // This means, Include and IncludeGroup elements with Condition attribute MAY contain references to
194 // properties and they will be expanded.
195 var elements = EvaluatePropertiesAndUsingTasksAndImportsAndChooses (xml.Children).ToArray (); // ToArray(): to not lazily evaluate elements.
197 // next, evaluate items
198 EvaluateItems (xml, elements);
200 // finally, evaluate targets and tasks
201 EvaluateTargets (elements);
204 IEnumerable<ProjectElement> EvaluatePropertiesAndUsingTasksAndImportsAndChooses (IEnumerable<ProjectElement> elements)
206 foreach (var child in elements) {
209 var ute = child as ProjectUsingTaskElement;
210 if (ute != null && EvaluateCondition (ute.Condition))
211 UsingTasks.Add (ute);
213 var pge = child as ProjectPropertyGroupElement;
214 if (pge != null && EvaluateCondition (pge.Condition))
215 foreach (var p in pge.Properties)
216 // do not allow overwriting reserved or well-known properties by user
217 if (!this.properties.Any (_ => (_.Value.IsImmutable) && _.Key.Equals (p.Name, StringComparison.InvariantCultureIgnoreCase)))
218 if (EvaluateCondition (p.Condition))
219 this.properties [p.Name] = new ProjectPropertyInstance (p.Name, false, ExpandString (p.Value));
221 var ige = child as ProjectImportGroupElement;
222 if (ige != null && EvaluateCondition (ige.Condition)) {
223 foreach (var incc in ige.Imports) {
224 if (EvaluateCondition (incc.Condition))
225 foreach (var e in Import (incc))
230 var inc = child as ProjectImportElement;
231 if (inc != null && EvaluateCondition (inc.Condition))
232 foreach (var e in Import (inc))
234 var choose = child as ProjectChooseElement;
235 if (choose != null && EvaluateCondition (choose.Condition)) {
237 foreach (ProjectWhenElement when in choose.WhenElements)
238 if (EvaluateCondition (when.Condition)) {
239 foreach (var e in EvaluatePropertiesAndUsingTasksAndImportsAndChooses (when.Children))
244 if (!done && choose.OtherwiseElement != null)
245 foreach (var e in EvaluatePropertiesAndUsingTasksAndImportsAndChooses (choose.OtherwiseElement.Children))
251 internal IEnumerable<T> GetAllItems<T> (string include, string exclude, Func<string,T> creator, Func<string,ITaskItem> taskItemCreator, Func<string,bool> itemTypeCheck, Action<T,string> assignRecurse)
253 return ProjectCollection.GetAllItems<T> (ExpandString, include, exclude, creator, taskItemCreator, Directory, assignRecurse,
254 t => all_evaluated_items.Any (i => i.EvaluatedInclude == t.ItemSpec && itemTypeCheck (i.ItemType)));
257 void EvaluateItems (ProjectRootElement xml, IEnumerable<ProjectElement> elements)
259 foreach (var child in elements.Reverse ()) {
260 var ige = child as ProjectItemGroupElement;
262 foreach (var p in ige.Items) {
263 if (!EvaluateCondition (ige.Condition) || !EvaluateCondition (p.Condition))
265 Func<string,ProjectItemInstance> creator = s => new ProjectItemInstance (this, p.ItemType, p.Metadata.Select (m => new KeyValuePair<string,string> (m.Name, m.Value)).ToList (), s);
266 foreach (var item in GetAllItems (p.Include, p.Exclude, creator, s => new ProjectTaskItem (p, s), it => string.Equals (it, p.ItemType, StringComparison.OrdinalIgnoreCase), (t, s) => t.RecursiveDir = s)) {
267 raw_items.Add (item);
268 all_evaluated_items.Add (item);
272 var def = child as ProjectItemDefinitionGroupElement;
274 foreach (var p in def.ItemDefinitions) {
275 if (EvaluateCondition (p.Condition)) {
276 ProjectItemDefinitionInstance existing;
277 if (!item_definitions.TryGetValue (p.ItemType, out existing))
278 item_definitions.Add (p.ItemType, (existing = new ProjectItemDefinitionInstance (p)));
279 existing.AddItems (p);
284 all_evaluated_items.Sort ((p1, p2) => string.Compare (p1.ItemType, p2.ItemType, StringComparison.OrdinalIgnoreCase));
287 void EvaluateTargets (IEnumerable<ProjectElement> elements)
289 foreach (var child in elements) {
290 var te = child as ProjectTargetElement;
292 // It overwrites same name target.
293 this.targets [te.Name] = new ProjectTargetInstance (te);
297 IEnumerable<ProjectElement> Import (ProjectImportElement import)
299 string dir = projects.GetEvaluationTimeThisFileDirectory (() => FullPath);
300 // FIXME: use appropriate logger (but cannot be instantiated here...?)
301 string path = ProjectCollection.FindFileInSeveralExtensionsPath (ref extensions_path_override, ExpandString, import.Project, TextWriter.Null.WriteLine);
302 path = Path.IsPathRooted (path) ? path : dir != null ? Path.Combine (dir, path) : Path.GetFullPath (path);
303 if (projects.OngoingImports.Contains (path))
304 throw new InvalidProjectFileException (import.Location, null, string.Format ("Circular imports was detected: {0} is already on \"importing\" stack", path));
305 projects.OngoingImports.Push (path);
307 using (var reader = XmlReader.Create (path)) {
308 var root = ProjectRootElement.Create (reader, projects);
309 raw_imports.Add (new ResolvedImport (import, root, true));
310 return this.EvaluatePropertiesAndUsingTasksAndImportsAndChooses (root.Children).ToArray ();
313 projects.OngoingImports.Pop ();
317 internal IEnumerable<ProjectItemInstance> AllEvaluatedItems {
318 get { return all_evaluated_items; }
321 public List<string> DefaultTargets { get; private set; }
323 public string Directory {
324 get { return directory; }
327 public string FullPath {
328 get { return full_path; }
331 public IDictionary<string, string> GlobalProperties {
332 get { return global_properties; }
335 public List<string> InitialTargets { get; private set; }
338 public bool IsImmutable {
339 get { throw new NotImplementedException (); }
343 public IDictionary<string, ProjectItemDefinitionInstance> ItemDefinitions {
344 get { return item_definitions; }
347 public ICollection<ProjectItemInstance> Items {
348 get { return all_evaluated_items; }
351 public ICollection<string> ItemTypes {
352 get { return all_evaluated_items.Select (i => i.ItemType).Distinct ().ToArray (); }
356 public ElementLocation ProjectFileLocation {
357 get { return location; }
361 public ICollection<ProjectPropertyInstance> Properties {
362 get { return properties.Values; }
370 IDictionary<string, ProjectTargetInstance> Targets {
371 get { return targets; }
374 public string ToolsVersion {
375 get { return tools_version; }
378 public ProjectItemInstance AddItem (string itemType, string evaluatedInclude)
380 return AddItem (itemType, evaluatedInclude, new KeyValuePair<string, string> [0]);
383 public ProjectItemInstance AddItem (string itemType, string evaluatedInclude, IEnumerable<KeyValuePair<string, string>> metadata)
385 var item = new ProjectItemInstance (this, itemType, metadata, evaluatedInclude);
386 raw_items.Add (item);
387 all_evaluated_items.Add (item);
393 return Build (new ILogger [0]);
396 public bool Build (IEnumerable<ILogger> loggers)
398 return Build (loggers, new ForwardingLoggerRecord [0]);
401 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
403 return Build (DefaultTargets.ToArray (), loggers, remoteLoggers);
406 public bool Build (string target, IEnumerable<ILogger> loggers)
408 return Build (target, loggers, new ForwardingLoggerRecord [0]);
411 public bool Build (string [] targets, IEnumerable<ILogger> loggers)
413 return Build (targets, loggers, new ForwardingLoggerRecord [0]);
416 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
418 return Build (new string [] {target}, loggers, remoteLoggers);
421 public bool Build (string [] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
423 IDictionary<string, TargetResult> outputs;
424 return Build (targets, loggers, remoteLoggers, out outputs);
427 public bool Build (string[] targets, IEnumerable<ILogger> loggers, out IDictionary<string, TargetResult> targetOutputs)
429 return Build (targets, loggers, new ForwardingLoggerRecord [0], out targetOutputs);
432 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers, out IDictionary<string, TargetResult> targetOutputs)
434 var manager = new BuildManager ();
435 var parameters = new BuildParameters (projects) {
436 ForwardingLoggers = remoteLoggers,
438 DefaultToolsVersion = projects.DefaultToolsVersion,
440 var requestData = new BuildRequestData (this, targets ?? DefaultTargets.ToArray ());
441 var result = manager.Build (parameters, requestData);
442 targetOutputs = result.ResultsByTarget;
443 return result.OverallResult == BuildResultCode.Success;
446 public ProjectInstance DeepCopy ()
448 return DeepCopy (false);
451 public ProjectInstance DeepCopy (bool isImmutable)
453 throw new NotImplementedException ();
456 public bool EvaluateCondition (string condition)
458 return string.IsNullOrWhiteSpace (condition) || new ExpressionEvaluator (this).EvaluateAsBoolean (condition);
461 public string ExpandString (string unexpandedValue)
463 return WindowsCompatibilityExtensions.NormalizeFilePath (new ExpressionEvaluator (this).Evaluate (unexpandedValue));
466 internal string ExpandString (ExpressionEvaluator evaluator, string unexpandedValue)
468 return WindowsCompatibilityExtensions.NormalizeFilePath (evaluator.Evaluate (unexpandedValue));
471 public ICollection<ProjectItemInstance> GetItems (string itemType)
473 return new CollectionFromEnumerable<ProjectItemInstance> (Items.Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)));
476 public IEnumerable<ProjectItemInstance> GetItemsByItemTypeAndEvaluatedInclude (string itemType, string evaluatedInclude)
478 throw new NotImplementedException ();
481 string extensions_path_override;
483 public ProjectPropertyInstance GetProperty (string name)
485 if (extensions_path_override != null && (name.Equals ("MSBuildExtensionsPath") || name.Equals ("MSBuildExtensionsPath32") || name.Equals ("MSBuildExtensionsPath64")))
486 return new ProjectPropertyInstance (name, true, extensions_path_override);
487 return properties.Values.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
490 public string GetPropertyValue (string name)
492 var prop = GetProperty (name);
493 return prop != null ? prop.EvaluatedValue : string.Empty;
496 public bool RemoveItem (ProjectItemInstance item)
498 // yeah, this raw_items should vanish...
499 raw_items.Remove (item);
500 return all_evaluated_items.Remove (item);
503 public bool RemoveProperty (string name)
505 var removed = properties.Values.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
508 properties.Remove (name);
512 public ProjectPropertyInstance SetProperty (string name, string evaluatedValue)
514 var p = new ProjectPropertyInstance (name, false, evaluatedValue);
515 properties [p.Name] = p;
519 public ProjectRootElement ToProjectRootElement ()
521 throw new NotImplementedException ();
525 public void UpdateStateFrom (ProjectInstance projectState)
527 throw new NotImplementedException ();
533 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinitionInstance item)
535 // ?? ItemDefinition does not have Include attribute. What's the point here?
536 throw new NotImplementedException ();
539 public static string GetEvaluatedItemIncludeEscaped (ProjectItemInstance item)
541 return ProjectCollection.Escape (item.EvaluatedInclude);
544 public static string GetMetadataValueEscaped (ProjectMetadataInstance metadatum)
546 return ProjectCollection.Escape (metadatum.EvaluatedValue);
549 public static string GetMetadataValueEscaped (ProjectItemDefinitionInstance item, string name)
551 var md = item.Metadata.FirstOrDefault (m => m.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
552 return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
555 public static string GetMetadataValueEscaped (ProjectItemInstance item, string name)
557 var md = item.Metadata.FirstOrDefault (m => m.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
558 return md != null ? ProjectCollection.Escape (md.EvaluatedValue) : null;
561 public static string GetPropertyValueEscaped (ProjectPropertyInstance property)
564 //return ProjectCollection.Escape (property.EvaluatedValue);
565 return property.EvaluatedValue;
568 internal List<ProjectUsingTaskElement> UsingTasks { get; private set; }
570 internal string GetFullPath (string pathRelativeToProject)
572 if (Path.IsPathRooted (pathRelativeToProject))
573 return pathRelativeToProject;
574 return Path.GetFullPath (Path.Combine (Directory, pathRelativeToProject));