MS.Build.dll: Add more missing members and implementation for some members.
[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 //
8 // (C) 2011 Leszek Ciesielski
9 // Copyright (C) 2011 Xamarin Inc. (http://www.xamarin.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Collections.Generic;
33 using System.Diagnostics;
34 using System.IO;
35 using System.Linq;
36 using System.Text;
37 using System.Xml;
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;
43
44 namespace Microsoft.Build.Evaluation
45 {
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}")]
50         public class Project
51         {
52                 public Project (XmlReader xml)
53                         : this (ProjectRootElement.Create (xml))
54                 {
55                 }
56
57                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
58                                               string toolsVersion)
59                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
60                 {
61                 }
62
63                 public Project (XmlReader xml, IDictionary<string, string> globalProperties,
64                                               string toolsVersion, ProjectCollection projectCollection)
65                         : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
66                 {
67                 }
68
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)
73                 {
74                 }
75
76                 public Project (ProjectRootElement xml) : this (xml, null, null)
77                 {
78                 }
79
80                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
81                                               string toolsVersion)
82                         : this (xml, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection)
83                 {
84                 }
85
86                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
87                                               string toolsVersion, ProjectCollection projectCollection)
88                         : this (xml, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
89                 {
90                 }
91
92                 public Project (ProjectRootElement xml, IDictionary<string, string> globalProperties,
93                                               string toolsVersion, ProjectCollection projectCollection,
94                                               ProjectLoadSettings loadSettings)
95                 {
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;
102
103                         Initialize ();
104                 }
105
106                 public Project (string projectFile)
107                         : this (projectFile, null, null)
108                 {
109                 }
110
111                 public Project (string projectFile, IDictionary<string, string> globalProperties,
112                                 string toolsVersion)
113                 : this (projectFile, globalProperties, toolsVersion, ProjectCollection.GlobalProjectCollection, ProjectLoadSettings.Default)
114                 {
115                 }
116
117                 public Project (string projectFile, IDictionary<string, string> globalProperties,
118                                 string toolsVersion, ProjectCollection projectCollection)
119                 : this (projectFile, globalProperties, toolsVersion, projectCollection, ProjectLoadSettings.Default)
120                 {
121                 }
122
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)
127                 {
128                 }
129
130                 ProjectLoadSettings load_settings;
131
132                 public IDictionary<string, string> GlobalProperties { get; private set; }
133
134                 public ProjectCollection ProjectCollection { get; private set; }
135
136                 public string ToolsVersion { get; private set; }
137
138                 public ProjectRootElement Xml { get; private set; }
139
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;
147
148                 void Initialize ()
149                 {
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> ();
157                 }
158
159                 public ICollection<ProjectItem> GetItemsIgnoringCondition (string itemType)
160                 {
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)));
165                 }
166
167                 public void RemoveItems (IEnumerable<ProjectItem> items)
168                 {
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);
175                         }
176                 }
177
178                 static readonly Dictionary<string, string> empty_metadata = new Dictionary<string, string> ();
179
180                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude)
181                 {
182                         return AddItem (itemType, unevaluatedInclude, empty_metadata);
183                 }
184
185                 public IList<ProjectItem> AddItem (string itemType, string unevaluatedInclude,
186                                 IEnumerable<KeyValuePair<string, string>> metadata)
187                 {
188                         // FIXME: needs several check that AddItemFast() does not process (see MSDN for details).
189
190                         return AddItemFast (itemType, unevaluatedInclude, metadata);
191                 }
192
193                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude)
194                 {
195                         return AddItemFast (itemType, unevaluatedInclude, empty_metadata);
196                 }
197
198                 public IList<ProjectItem> AddItemFast (string itemType, string unevaluatedInclude,
199                                                                      IEnumerable<KeyValuePair<string, string>> metadata)
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 public bool Build ()
205                 {
206                         return Build (Xml.DefaultTargets.Split (';'));
207                 }
208
209                 public bool Build (IEnumerable<ILogger> loggers)
210                 {
211                         return Build (Xml.DefaultTargets.Split (';'), loggers);
212                 }
213
214                 public bool Build (string target)
215                 {
216                         return string.IsNullOrWhiteSpace (target) ? Build () : Build (new string [] {target});
217                 }
218
219                 public bool Build (string[] targets)
220                 {
221                         return Build (targets, new ILogger [0]);
222                 }
223
224                 public bool Build (ILogger logger)
225                 {
226                         return Build (Xml.DefaultTargets.Split (';'), new ILogger [] {logger});
227                 }
228
229                 public bool Build (string[] targets, IEnumerable<ILogger> loggers)
230                 {
231                         return Build (targets, loggers, new ForwardingLoggerRecord [0]);
232                 }
233
234                 public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
235                 {
236                         return Build (Xml.DefaultTargets.Split (';'), loggers, remoteLoggers);
237                 }
238
239                 public bool Build (string target, IEnumerable<ILogger> loggers)
240                 {
241                         return Build (new string [] { target }, loggers);
242                 }
243
244                 public bool Build (string[] targets, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
245                 {
246                         var manager = new BuildManager ();
247                         var pi = manager.GetProjectInstanceForBuild (this);
248                         var parameters = new BuildParameters (this.ProjectCollection) {
249                                 ForwardingLoggers = remoteLoggers,
250                                 Loggers = loggers
251                         };
252                         var requestData = new BuildRequestData (pi, targets);
253                         var result = manager.Build (parameters, requestData);
254                         return result.OverallResult == BuildResultCode.Success;
255                 }
256
257                 public bool Build (string target, IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
258                 {
259                         return Build (new string [] { target }, loggers, remoteLoggers);
260                 }
261
262                 public ProjectInstance CreateProjectInstance ()
263                 {
264                         throw new NotImplementedException ();
265                 }
266
267                 public string ExpandString (string unexpandedValue)
268                 {
269                         throw new NotImplementedException ();
270                 }
271
272                 public static string GetEvaluatedItemIncludeEscaped (ProjectItem item)
273                 {
274                         throw new NotImplementedException ();
275                 }
276
277                 public static string GetEvaluatedItemIncludeEscaped (ProjectItemDefinition item)
278                 {
279                         throw new NotImplementedException ();
280                 }
281
282                 public ICollection<ProjectItem> GetItems (string itemType)
283                 {
284                         throw new NotImplementedException ();
285                 }
286
287                 public ICollection<ProjectItem> GetItemsByEvaluatedInclude (string evaluatedInclude)
288                 {
289                         throw new NotImplementedException ();
290                 }
291
292                 public IEnumerable<ProjectElement> GetLogicalProject ()
293                 {
294                         throw new NotImplementedException ();
295                 }
296
297                 public static string GetMetadataValueEscaped (ProjectMetadata metadatum)
298                 {
299                         throw new NotImplementedException ();
300                 }
301
302                 public static string GetMetadataValueEscaped (ProjectItem item, string name)
303                 {
304                         throw new NotImplementedException ();
305                 }
306
307                 public static string GetMetadataValueEscaped (ProjectItemDefinition item, string name)
308                 {
309                         throw new NotImplementedException ();
310                 }
311
312                 public string GetPropertyValue (string name)
313                 {
314                         throw new NotImplementedException ();
315                 }
316
317                 public static string GetPropertyValueEscaped (ProjectProperty property)
318                 {
319                         throw new NotImplementedException ();
320                 }
321
322                 public ProjectProperty GetProperty (string name)
323                 {
324                         throw new NotImplementedException ();
325                 }
326
327                 public void MarkDirty ()
328                 {
329                         throw new NotImplementedException ();
330                 }
331
332                 public void ReevaluateIfNecessary ()
333                 {
334                         throw new NotImplementedException ();
335                 }
336
337                 public bool RemoveGlobalProperty (string name)
338                 {
339                         throw new NotImplementedException ();
340                 }
341
342                 public bool RemoveItem (ProjectItem item)
343                 {
344                         throw new NotImplementedException ();
345                 }
346
347                 public bool RemoveProperty (ProjectProperty property)
348                 {
349                         throw new NotImplementedException ();
350                 }
351
352                 public void Save ()
353                 {
354                         using (var writer = new StreamWriter (FullPath))
355                                 Save (writer);
356                 }
357
358                 public void Save (TextWriter writer)
359                 {
360                         throw new NotImplementedException ();
361                 }
362
363                 public void Save (string path)
364                 {
365                         Save (path, Encoding.Default);
366                 }
367
368                 public void Save (Encoding encoding)
369                 {
370                         Save (FullPath, encoding);
371                 }
372
373                 public void Save (string path, Encoding encoding)
374                 {
375                         using (var writer = new StreamWriter (path, false, encoding))
376                                 Save (writer);
377                 }
378
379                 public void SaveLogicalProject (TextWriter writer)
380                 {
381                         throw new NotImplementedException ();
382                 }
383
384                 public bool SetGlobalProperty (string name, string escapedValue)
385                 {
386                         throw new NotImplementedException ();
387                 }
388
389                 public ProjectProperty SetProperty (string name, string unevaluatedValue)
390                 {
391                         throw new NotImplementedException ();
392                 }
393
394                 public ICollection<ProjectMetadata> AllEvaluatedItemDefinitionMetadata { get; private set; }
395
396                 public ICollection<ProjectItem> AllEvaluatedItems { get; private set; }
397
398                 public ICollection<ProjectProperty> AllEvaluatedProperties { get; private set; }
399
400                 public IDictionary<string, List<string>> ConditionedProperties { get; private set; }
401
402                 public string DirectoryPath {
403                         get { return dir_path; }
404                 }
405
406                 public bool DisableMarkDirty { get; set; }
407
408                 public int EvaluationCounter {
409                         get { throw new NotImplementedException (); }
410                 }
411
412                 public string FullPath {
413                         get { return full_path; }
414                         set {
415                                 // FIXME: check validity? mark IsDirty?
416                                 full_path = value;
417                         }
418                 }
419
420                 public IList<ResolvedImport> Imports {
421                         get { throw new NotImplementedException (); }
422                 }
423
424                 public IList<ResolvedImport> ImportsIncludingDuplicates {
425                         get { return raw_imports; }
426                 }
427
428                 public bool IsBuildEnabled {
429                         get { throw new NotImplementedException (); }
430                 }
431
432                 public bool IsDirty {
433                         get { throw new NotImplementedException (); }
434                 }
435
436                 public IDictionary<string, ProjectItemDefinition> ItemDefinitions {
437                         get { return item_definitions; }
438                 }
439
440                 public ICollection<ProjectItem> Items {
441                         get { throw new NotImplementedException (); }
442                 }
443
444                 public ICollection<ProjectItem> ItemsIgnoringCondition {
445                         get { return raw_items; }
446                 }
447
448                 public ICollection<string> ItemTypes {
449                         get { return item_types; }
450                 }
451
452                 public ICollection<ProjectProperty> Properties {
453                         get { return properties; }
454                 }
455
456                 public bool SkipEvaluation { get; set; }
457
458                 public IDictionary<string, ProjectTargetInstance> Targets {
459                         get { return targets; }
460                 }
461         }
462 }