5 // Leszek Ciesielski (skolima@gmail.com)
6 // Rolf Bjarne Kvinge (rolf@xamarin.com)
8 // (C) 2011 Leszek Ciesielski
9 // Copyright (C) 2011 Xamarin Inc. (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
33 using System.Diagnostics;
38 using Microsoft.Build.Construction;
39 using Microsoft.Build.Internal;
40 using Microsoft.Build.Execution;
41 using Microsoft.Build.Framework;
42 using Microsoft.Build.Logging;
44 namespace Microsoft.Build.Evaluation
46 [DebuggerDisplay ("{FullPath} EffectiveToolsVersion={ToolsVersion} #GlobalProperties="
47 + "{data.globalProperties.Count} #Properties={data.Properties.Count} #ItemTypes="
48 + "{data.ItemTypes.Count} #ItemDefinitions={data.ItemDefinitions.Count} #Items="
49 + "{data.Items.Count} #Targets={data.Targets.Count}")]
52 public Project (XmlReader xml)
53 : this (ProjectRootElement.Create (xml))
57 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
59 : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
63 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
64 string toolsVersion, ProjectCollection projectCollection)
65 : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
69 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
70 string toolsVersion, ProjectCollection projectCollection,
71 ProjectLoadSettings loadSettings)
72 : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection, loadSettings)
76 public Project (ProjectRootElement xml) : this (xml, null, null)
80 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
82 : this (xml, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
86 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
87 string toolsVersion, ProjectCollection projectCollection)
88 : this (xml, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
92 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
93 string toolsVersion, ProjectCollection projectCollection,
94 ProjectLoadSettings loadSettings)
96 if (projectCollection == null)
97 throw new ArgumentNullException ("projectCollection");
98 this.GlobalProperties = globalProperties ?? new Dictionary<string, string> ();
99 this.ToolsVersion = toolsVersion;
100 this.ProjectCollection = projectCollection;
101 this.load_settings = loadSettings;
106 public Project (string projectFile)
107 : this (projectFile, null, null)
111 public Project (string projectFile, IDictionary<string, string> globalProperties,
113 : this (projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection, ProjectLoadSettings.Default)
117 public Project (string projectFile, IDictionary<string, string> globalProperties,
118 string toolsVersion, ProjectCollection projectCollection)
119 : this (projectFile, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
123 public Project (string projectFile, IDictionary<string, string> globalProperties,
124 string toolsVersion, ProjectCollection projectCollection,
125 ProjectLoadSettings loadSettings)
126 : this (ProjectRootElement.Create (projectFile), globalProperties, toolsVersion, projectCollection, loadSettings)
130 ProjectLoadSettings load_settings;
132 public IDictionary<string, string> GlobalProperties { get; private set; }
134 public ProjectCollection ProjectCollection { get; private set; }
136 public string ToolsVersion { get; private set; }
138 public ProjectRootElement Xml { get; private set; }
140 string dir_path, full_path;
141 Dictionary<string, ProjectItemDefinition> item_definitions;
142 List<ResolvedImport> raw_imports;
143 List<ProjectItem> raw_items;
144 List<string> item_types;
145 List<ProjectProperty> properties;
146 Dictionary<string, ProjectTargetInstance> targets;
150 ConditionedProperties = new Dictionary<string, List<string>> ();
151 dir_path = Directory.GetCurrentDirectory ();
152 raw_imports = new List<ResolvedImport> ();
153 item_definitions = new Dictionary<string, ProjectItemDefinition> ();
154 item_types = new List<string> ();
155 properties = new List<ProjectProperty> ();
156 targets = new Dictionary<string, ProjectTargetInstance> ();
159 public ICollection<ProjectItem> GetItemsIgnoringCondition (string itemType)
161 return new CollectionFromEnumerable<ProjectItem> (
162 new FilteredEnumerable<ProjectItemElement> (Xml.Items).
163 Where (p => p.ItemType.Equals (itemType, StringComparison.OrdinalIgnoreCase)).
164 Select (p => new ProjectItem (p)));
167 public void RemoveItems (IEnumerable<ProjectItem> items)
169 var removal = new List<ProjectItem> (items);
170 foreach (var item in removal) {
171 var parent = item.Xml.Parent;
172 parent.RemoveChild (item.Xml);
173 if (parent.Count == 0)
174 parent.Parent.RemoveChild (parent);
178 static readonly Dictionary<string, string> empty_metadata = new Dictionary<string, string> ();
180 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude)
182 return AddItem (itemType, unevaluatedInclude, empty_metadata);
185 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude,
186 IEnumerable<KeyValuePair<string, string>> metadata)
188 // FIXME: needs several check that AddItemFast() does not process (see MSDN for details).
190 return AddItemFast (itemType, unevaluatedInclude, metadata);
193 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude)
195 return AddItemFast (itemType, unevaluatedInclude, empty_metadata);
198 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude,
199 IEnumerable<KeyValuePair<string, string>> metadata)
201 throw new NotImplementedException ();
206 return Build (Xml.DefaultTargets.Split (';'));
209 public bool Build (IEnumerable<ILogger> loggers)
211 return Build (Xml.DefaultTargets.Split (';'), loggers);
214 public bool Build (string target)
216 return string.IsNullOrWhiteSpace (target) ? Build () : Build (new string [] {target});
219 public bool Build (string[] targets)
221 return Build (targets, new ILogger [0]);
224 public bool Build (ILogger logger)
226 return Build (Xml.DefaultTargets.Split (';'), new ILogger [] {logger});
229 public bool Build (string[] targets, IEnumerable<ILogger> loggers)
231 return Build (targets, loggers, new ForwardingLoggerRecord [0]);
234 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
236 return Build (Xml.DefaultTargets.Split (';'), loggers, remoteLoggers);
239 public bool Build (string target, IEnumerable<ILogger> loggers)
241 return Build (new string [] { target }, loggers);
244 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
246 var manager = new BuildManager ();
247 var pi = manager.GetProjectInstanceForBuild (this);
248 var parameters = new BuildParameters (this.ProjectCollection) {
249 ForwardingLoggers = remoteLoggers,
252 var requestData = new BuildRequestData (pi, targets);
253 var result = manager.Build (parameters, requestData);
254 return result.OverallResult == BuildResultCode.Success;
257 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
259 return Build (new string [] { target }, loggers, remoteLoggers);
262 public ProjectInstance CreateProjectInstance ()
264 throw new NotImplementedException ();
267 public string ExpandString (string unexpandedValue)
269 throw new NotImplementedException ();
272 public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
274 throw new NotImplementedException ();
277 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinition item)
279 throw new NotImplementedException ();
282 public ICollection<ProjectItem> GetItems (string itemType)
284 throw new NotImplementedException ();
287 public ICollection<ProjectItem> GetItemsByEvaluatedInclude (string evaluatedInclude)
289 throw new NotImplementedException ();
292 public IEnumerable<ProjectElement> GetLogicalProject ()
294 throw new NotImplementedException ();
297 public static string GetMetadataValueEscaped (ProjectMetadata metadatum)
299 throw new NotImplementedException ();
302 public static string GetMetadataValueEscaped (ProjectItem item, string name)
304 throw new NotImplementedException ();
307 public static string GetMetadataValueEscaped (ProjectItemDefinition item, string name)
309 throw new NotImplementedException ();
312 public string GetPropertyValue (string name)
314 throw new NotImplementedException ();
317 public static string GetPropertyValueEscaped (ProjectProperty property)
319 throw new NotImplementedException ();
322 public ProjectProperty GetProperty (string name)
324 throw new NotImplementedException ();
327 public void MarkDirty ()
329 throw new NotImplementedException ();
332 public void ReevaluateIfNecessary ()
334 throw new NotImplementedException ();
337 public bool RemoveGlobalProperty (string name)
339 throw new NotImplementedException ();
342 public bool RemoveItem (ProjectItem item)
344 throw new NotImplementedException ();
347 public bool RemoveProperty (ProjectProperty property)
349 throw new NotImplementedException ();
354 using (var writer = new StreamWriter (FullPath))
358 public void Save (TextWriter writer)
360 throw new NotImplementedException ();
363 public void Save (string path)
365 Save (path, Encoding.Default);
368 public void Save (Encoding encoding)
370 Save (FullPath, encoding);
373 public void Save (string path, Encoding encoding)
375 using (var writer = new StreamWriter (path, false, encoding))
379 public void SaveLogicalProject (TextWriter writer)
381 throw new NotImplementedException ();
384 public bool SetGlobalProperty (string name, string escapedValue)
386 throw new NotImplementedException ();
389 public ProjectProperty SetProperty (string name, string unevaluatedValue)
391 throw new NotImplementedException ();
394 public ICollection<ProjectMetadata> AllEvaluatedItemDefinitionMetadata { get; private set; }
396 public ICollection<ProjectItem> AllEvaluatedItems { get; private set; }
398 public ICollection<ProjectProperty> AllEvaluatedProperties { get; private set; }
400 public IDictionary<string, List<string>> ConditionedProperties { get; private set; }
402 public string DirectoryPath {
403 get { return dir_path; }
406 public bool DisableMarkDirty { get; set; }
408 public int EvaluationCounter {
409 get { throw new NotImplementedException (); }
412 public string FullPath {
413 get { return full_path; }
415 // FIXME: check validity? mark IsDirty?
420 public IList<ResolvedImport> Imports {
421 get { throw new NotImplementedException (); }
424 public IList<ResolvedImport> ImportsIncludingDuplicates {
425 get { return raw_imports; }
428 public bool IsBuildEnabled {
429 get { throw new NotImplementedException (); }
432 public bool IsDirty {
433 get { throw new NotImplementedException (); }
436 public IDictionary<string, ProjectItemDefinition> ItemDefinitions {
437 get { return item_definitions; }
440 public ICollection<ProjectItem> Items {
441 get { throw new NotImplementedException (); }
444 public ICollection<ProjectItem> ItemsIgnoringCondition {
445 get { return raw_items; }
448 public ICollection<string> ItemTypes {
449 get { return item_types; }
452 public ICollection<ProjectProperty> Properties {
453 get { return properties; }
456 public bool SkipEvaluation { get; set; }
458 public IDictionary<string, ProjectTargetInstance> Targets {
459 get { return targets; }