implement ProjectItem and co.
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Evaluation / ProjectCollection.cs
1 //
2 // ProjectCollection.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.
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 Microsoft.Build.Construction;
33 using Microsoft.Build.Execution;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Logging;
36 using Microsoft.Build.Utilities;
37 using System;
38 using System.Collections.Generic;
39 using System.Collections.ObjectModel;
40 using System.IO;
41 using System.Linq;
42
43 namespace Microsoft.Build.Evaluation
44 {
45         public class ProjectCollection : IDisposable
46         {
47                 public delegate void ProjectAddedEventHandler (object target, ProjectAddedToProjectCollectionEventArgs args);
48                 
49                 public class ProjectAddedToProjectCollectionEventArgs : EventArgs
50                 {
51                         public ProjectAddedToProjectCollectionEventArgs (ProjectRootElement project)
52                         {
53                                 if (project == null)
54                                         throw new ArgumentNullException ("project");
55                                 ProjectRootElement = project;
56                         }
57                         
58                         public ProjectRootElement ProjectRootElement { get; private set; }
59                 }
60
61                 // static members
62
63                 static readonly ProjectCollection global_project_collection;
64
65                 static ProjectCollection ()
66                 {
67                         #if NET_4_5
68                         global_project_collection = new ProjectCollection (new ReadOnlyDictionary<string, string> (new Dictionary<string, string> ()));
69                         #else
70                         global_project_collection = new ProjectCollection (new Dictionary<string, string> ());
71                         #endif
72                 }
73
74                 public static string Escape (string unescapedString)
75                 {
76                         return Mono.XBuild.Utilities.MSBuildUtils.Escape (unescapedString);
77                 }
78
79                 public static string Unescape (string scapedString)
80                 {
81                         throw new NotImplementedException ();
82                 }
83
84                 public static ProjectCollection GlobalProjectCollection {
85                         get { return global_project_collection; }
86                 }
87
88                 // semantic model part
89
90                 public ProjectCollection ()
91                         : this (null)
92                 {
93                 }
94
95                 public ProjectCollection (IDictionary<string, string> globalProperties)
96                 : this (globalProperties, null, ToolsetDefinitionLocations.Registry | ToolsetDefinitionLocations.ConfigurationFile)
97                 {
98                 }
99
100                 public ProjectCollection (ToolsetDefinitionLocations toolsetDefinitionLocations)
101                 : this (null, null, toolsetDefinitionLocations)
102                 {
103                 }
104
105                 public ProjectCollection (IDictionary<string, string> globalProperties, IEnumerable<ILogger> loggers,
106                                 ToolsetDefinitionLocations toolsetDefinitionLocations)
107                         : this (globalProperties, loggers, null, toolsetDefinitionLocations, int.MaxValue, false)
108                 {
109                 }
110
111                 public ProjectCollection (IDictionary<string, string> globalProperties,
112                                 IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers,
113                                 ToolsetDefinitionLocations toolsetDefinitionLocations,
114                                 int maxNodeCount, bool onlyLogCriticalEvents)
115                 {
116                         global_properties = globalProperties ?? new Dictionary<string, string> ();
117                         this.loggers = loggers != null ? loggers.ToList () : new List<ILogger> ();
118                         toolset_locations = toolsetDefinitionLocations;
119                         max_node_count = maxNodeCount;
120                         OnlyLogCriticalEvents = onlyLogCriticalEvents;
121
122                         LoadDefaultToolsets ();
123                 }
124                 
125                 [MonoTODO ("not fired yet")]
126                 public event ProjectAddedEventHandler ProjectAdded;
127                 [MonoTODO ("not fired yet")]
128                 public event EventHandler<ProjectChangedEventArgs> ProjectChanged;
129                 [MonoTODO ("not fired yet")]
130                 public event EventHandler<ProjectCollectionChangedEventArgs> ProjectCollectionChanged;
131                 [MonoTODO ("not fired yet")]
132                 public event EventHandler<ProjectXmlChangedEventArgs> ProjectXmlChanged;
133
134                 readonly int max_node_count;
135
136                 [MonoTODO]
137                 public int Count {
138                         get { return loaded_projects.Count; }
139                 }
140
141                 [MonoTODO]
142                 public string DefaultToolsVersion {
143                         get { return Toolsets.Any () ? Toolsets.First ().ToolsVersion : null; }
144                 }
145
146                 public void Dispose ()
147                 {
148                         Dispose (true);
149                         GC.SuppressFinalize (this);
150                 }
151
152                 protected virtual void Dispose (bool disposing)
153                 {
154                         if (disposing) {
155                         }
156                 }
157
158                 public ICollection<Project> GetLoadedProjects (string fullPath)
159                 {
160                         return LoadedProjects.Where (p => Path.GetFullPath (p.FullPath) == Path.GetFullPath (fullPath)).ToList ();
161                 }
162
163                 readonly IDictionary<string, string> global_properties;
164
165                 public IDictionary<string, string> GlobalProperties {
166                         get { return global_properties; }
167                 }
168
169                 readonly List<Project> loaded_projects = new List<Project> ();
170
171                 [MonoTODO]
172                 public ICollection<Project> LoadedProjects {
173                         get { return loaded_projects; }
174                 }
175
176                 readonly List<ILogger> loggers = new List<ILogger> ();
177                 [MonoTODO]
178                 public ICollection<ILogger> Loggers {
179                         get { return loggers; }
180                 }
181
182                 [MonoTODO]
183                 public bool OnlyLogCriticalEvents { get; set; }
184
185                 [MonoTODO]
186                 public bool SkipEvaluation { get; set; }
187
188                 readonly ToolsetDefinitionLocations toolset_locations;
189                 public ToolsetDefinitionLocations ToolsetLocations {
190                         get { return toolset_locations; }
191                 }
192
193                 readonly List<Toolset> toolsets = new List<Toolset> ();
194                 [MonoTODO ("unused")]
195                 // so what should we do without ToolLocationHelper in Microsoft.Build.Utilities.dll? There is no reference to it in this dll.
196                 public ICollection<Toolset> Toolsets {
197                         // For ConfigurationFile and None, they cannot be added externally.
198                         get { return (ToolsetLocations & ToolsetDefinitionLocations.Registry) != 0 ? toolsets : toolsets.ToList (); }
199                 }
200
201                 //FIXME: should also support config file, depending on ToolsetLocations
202                 void LoadDefaultToolsets ()
203                 {
204                         AddToolset (new Toolset ("2.0",
205                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20), this, null));
206                         AddToolset (new Toolset ("3.0",
207                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version30), this, null));
208                         AddToolset (new Toolset ("3.5",
209                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version35), this, null));
210 #if NET_4_0
211                         AddToolset (new Toolset ("4.0",
212                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version40), this, null));
213 #endif
214 #if NET_4_5
215                         AddToolset (new Toolset ("4.5",
216                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version45), this, null));
217 #endif
218                 }
219                 
220                 [MonoTODO ("not verified at all")]
221                 public void AddToolset (Toolset toolset)
222                 {
223                         toolsets.Add (toolset);
224                 }
225                 
226                 [MonoTODO ("not verified at all")]
227                 public void RemoveAllToolsets ()
228                 {
229                         toolsets.Clear ();
230                 }
231                 
232                 [MonoTODO ("not verified at all")]
233                 public void RegisterLogger (ILogger logger)
234                 {
235                         loggers.Add (logger);
236                 }
237                 
238                 [MonoTODO ("not verified at all")]
239                 public void RegisterLoggers (IEnumerable<ILogger> loggers)
240                 {
241                         foreach (var logger in loggers)
242                                 this.loggers.Add (logger);
243                 }
244
245                 public void UnloadAllProjects ()
246                 {
247                         throw new NotImplementedException ();
248                 }
249
250                 public void UnloadProject (Project project)
251                 {
252                         throw new NotImplementedException ();
253                 }
254
255                 public void UnloadProject (ProjectRootElement projectRootElement)
256                 {
257                         throw new NotImplementedException ();
258                 }
259
260                 public static Version Version {
261                         get { throw new NotImplementedException (); }
262                 }
263
264                 // Execution part
265
266                 [MonoTODO]
267                 public bool DisableMarkDirty { get; set; }
268
269                 [MonoTODO]
270                 public HostServices HostServices { get; set; }
271
272                 [MonoTODO]
273                 public bool IsBuildEnabled { get; set; }
274         }
275 }