2 // Engine.cs: Main engine of XBuild.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using Microsoft.Build.Framework;
34 namespace Microsoft.Build.BuildEngine {
39 BuildPropertyGroup environmentProperties;
40 EventSource eventSource;
42 BuildPropertyGroup globalProperties;
43 IDictionary importedProjects;
45 bool onlyLogCriticalEvents;
47 BuildPropertyGroup reservedProperties;
49 // FIXME: GlobalEngine static property uses this but what about GlobalEngineAccessor?
50 static Engine globalEngine;
51 static Version version;
55 version = new Version("0.1");
63 // engine should be invoked with path where binary files are
64 // to find microsoft.build.tasks
65 public Engine (string binPath)
67 this.binPath = binPath;
68 this.projects = new Hashtable ();
69 this.eventSource = new EventSource ();
70 this.loggers = new ArrayList ();
71 this.buildStarted = false;
72 this.LoadEnvironmentProperties ();
73 this.reservedProperties = new BuildPropertyGroup ();
74 this.reservedProperties.AddNewProperty ("MSBuildBinPath", binPath, false, PropertyType.Reserved);
78 public bool BuildProject (Project project)
80 return project.Build ();
84 public bool BuildProject (Project project, string targetName)
86 return BuildProject (project, new string[] { targetName}, new Hashtable (), BuildSettings.None);
90 public bool BuildProject (Project project, string[] targetNames)
92 return BuildProject (project, targetNames, new Hashtable (), BuildSettings.None);
96 public bool BuildProject (Project project,
98 IDictionary targetOutputs)
100 return BuildProject (project, targetNames, targetOutputs, BuildSettings.None);
103 [MonoTODO ("use buildFlags")]
104 public bool BuildProject (Project project,
105 string[] targetNames,
106 IDictionary targetOutputs,
107 BuildSettings buildFlags)
111 LogProjectStarted (project, targetNames);
113 result = project.Build (targetNames, targetOutputs);
115 LogProjectFinished (project, result);
121 public bool BuildProjectFile (string projectFile)
123 throw new NotImplementedException ();
127 public bool BuildProjectFile (string projectFile,
130 throw new NotImplementedException ();
134 public bool BuildProjectFile (string projectFile,
135 string[] targetNames)
137 throw new NotImplementedException ();
141 public bool BuildProjectFile (string projectFile,
142 string[] targetNames,
143 BuildPropertyGroup globalProperties)
145 return BuildProjectFile (projectFile, targetNames, globalProperties, new Hashtable (), BuildSettings.None);
149 public bool BuildProjectFile (string projectFile,
150 string[] targetNames,
151 BuildPropertyGroup globalProperties,
152 IDictionary targetOutputs)
154 return BuildProjectFile (projectFile, targetNames, globalProperties, targetOutputs, BuildSettings.None);
157 [MonoTODO ("use buildFlags")]
158 public bool BuildProjectFile (string projectFile,
159 string[] targetNames,
160 BuildPropertyGroup globalProperties,
161 IDictionary targetOutputs,
162 BuildSettings buildFlags)
167 if (projects.Contains (projectFile)) {
168 project = (Project) projects [projectFile];
169 LogProjectStarted (project, targetNames);
170 result = project.Build (targetNames, targetOutputs);
175 LogProjectFinished (project, result);
180 public Project CreateNewProject ()
182 if (buildStarted == false) {
186 Project p = new Project (this);
187 p.EnvironmentProperties = this.environmentProperties;
188 p.ReservedProperties = this.reservedProperties;
189 if (globalProperties != null) {
190 BuildPropertyGroup bpg = new BuildPropertyGroup ();
191 foreach (BuildProperty bp in globalProperties)
192 bpg.AddNewProperty (bp.Name, bp.Value, false, PropertyType.CommandLine);
193 p.GlobalProperties = bpg;
198 public Project GetLoadedProject (string projectFullFileName)
200 return (Project) projects [projectFullFileName];
203 public void RegisterLogger (ILogger logger)
206 throw new ArgumentNullException ("logger");
207 logger.Initialize (eventSource);
208 loggers.Add (logger);
212 public void UnloadAllProjects ()
217 public void UnloadProject (Project project)
221 public void UnregisterAllLoggers ()
223 // FIXME: check if build succeeded
224 LogBuildFinished (true);
225 foreach (ILogger i in loggers) {
231 private void LoadEnvironmentProperties ()
233 environmentProperties = new BuildPropertyGroup ();
234 IDictionary environment = Environment.GetEnvironmentVariables ();
235 foreach (DictionaryEntry de in environment) {
236 environmentProperties.AddNewProperty ((string) de.Key, (string) de.Value, false,
237 PropertyType.Environment);
241 private void LogProjectStarted (Project project, string[] targetNames)
243 ProjectStartedEventArgs psea;
244 if (targetNames.Length == 0) {
245 if (project.DefaultTargets != String.Empty)
246 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
247 project.DefaultTargets, null, null);
249 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default", null, null);
251 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
252 targetNames), null, null);
253 eventSource.FireProjectStarted (this, psea);
256 private void LogProjectFinished (Project project, bool succeeded)
258 ProjectFinishedEventArgs pfea;
259 pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
260 eventSource.FireProjectFinished (this, pfea);
263 private void LogBuildStarted ()
265 BuildStartedEventArgs bsea;
266 bsea = new BuildStartedEventArgs ("Build started.", null);
267 eventSource.FireBuildStarted (this, bsea);
270 private void LogBuildFinished (bool succeeded)
272 BuildFinishedEventArgs bfea;
273 bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
274 eventSource.FireBuildFinished (this, bfea);
277 public string BinPath {
278 get { return binPath; }
279 set { binPath = value; }
282 public bool BuildEnabled {
283 get { return buildEnabled; }
284 set { buildEnabled = value; }
287 public static Version Version {
288 get { return version; }
291 public static Engine GlobalEngine {
292 get { return globalEngine; }
295 public BuildPropertyGroup GlobalProperties {
296 get { return globalProperties; }
297 set { globalProperties = value; }
300 public bool OnlyLogCriticalEvents {
301 get { return eventSource.OnlyLogCriticalEvents; }
302 set { eventSource.OnlyLogCriticalEvents = value; }
305 internal EventSource EventSource {
306 get { return eventSource; }