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 EventSource eventSource;
41 BuildPropertyGroup globalProperties;
42 IDictionary importedProjects;
44 bool onlyLogCriticalEvents;
47 static Engine globalEngine;
48 static Version version;
52 version = new Version ("0.1");
60 // engine should be invoked with path where binary files are
61 // to find microsoft.build.tasks
62 public Engine (string binPath)
64 this.binPath = binPath;
65 this.projects = new Hashtable ();
66 this.eventSource = new EventSource ();
67 this.loggers = new ArrayList ();
68 this.buildStarted = false;
69 this.globalProperties = new BuildPropertyGroup ();
73 public bool BuildProject (Project project)
75 return project.Build ();
79 public bool BuildProject (Project project, string targetName)
81 return BuildProject (project, new string[] { targetName}, new Hashtable (), BuildSettings.None);
85 public bool BuildProject (Project project, string[] targetNames)
87 return BuildProject (project, targetNames, new Hashtable (), BuildSettings.None);
91 public bool BuildProject (Project project,
93 IDictionary targetOutputs)
95 return BuildProject (project, targetNames, targetOutputs, BuildSettings.None);
98 [MonoTODO ("use buildFlags")]
99 public bool BuildProject (Project project,
100 string[] targetNames,
101 IDictionary targetOutputs,
102 BuildSettings buildFlags)
106 LogProjectStarted (project, targetNames);
108 result = project.Build (targetNames, targetOutputs);
110 LogProjectFinished (project, result);
116 public bool BuildProjectFile (string projectFile)
118 throw new NotImplementedException ();
122 public bool BuildProjectFile (string projectFile,
125 throw new NotImplementedException ();
129 public bool BuildProjectFile (string projectFile,
130 string[] targetNames)
132 throw new NotImplementedException ();
136 public bool BuildProjectFile (string projectFile,
137 string[] targetNames,
138 BuildPropertyGroup globalProperties)
140 return BuildProjectFile (projectFile, targetNames, globalProperties, new Hashtable (), BuildSettings.None);
144 public bool BuildProjectFile (string projectFile,
145 string[] targetNames,
146 BuildPropertyGroup globalProperties,
147 IDictionary targetOutputs)
149 return BuildProjectFile (projectFile, targetNames, globalProperties, targetOutputs, BuildSettings.None);
152 [MonoTODO ("use buildFlags")]
153 public bool BuildProjectFile (string projectFile,
154 string[] targetNames,
155 BuildPropertyGroup globalProperties,
156 IDictionary targetOutputs,
157 BuildSettings buildFlags)
162 if (projects.Contains (projectFile)) {
163 project = (Project) projects [projectFile];
164 LogProjectStarted (project, targetNames);
165 result = project.Build (targetNames, targetOutputs);
170 LogProjectFinished (project, result);
175 internal void CheckBinPath ()
177 if (BinPath == null) {
178 throw new InvalidOperationException("Before a project can be instantiated, " +
179 "Engine.BinPath must be set to the location on disk where MSBuild " +
180 "is installed. This is used to evaluate $(MSBuildBinPath).");
184 public Project CreateNewProject ()
187 return new Project (this);
190 public Project GetLoadedProject (string projectFullFileName)
192 if (projectFullFileName == null) {
193 throw new ArgumentNullException ("projectFullFileName");
195 return (Project) projects [projectFullFileName];
198 internal void RemoveLoadedProject (Project p)
200 if (p.FullFileName != "") {
201 projects.Remove (p.FullFileName);
205 internal void AddLoadedProject (Project p)
207 if (p.FullFileName != "") {
208 projects.Add (p.FullFileName, p);
212 public void UnloadProject (Project project)
214 if (project.ParentEngine != this) {
215 throw new InvalidOperationException("This project is not loaded in this engine");
217 project.CheckUnloaded ();
218 if (project.FullFileName != "") {
219 projects.Remove (project.FullFileName);
224 public void UnloadAllProjects ()
226 foreach (DictionaryEntry e in projects) {
227 UnloadProject ((Project) e.Value);
231 public void RegisterLogger (ILogger logger)
234 throw new ArgumentNullException ("logger");
235 logger.Initialize (eventSource);
236 loggers.Add (logger);
239 public void UnregisterAllLoggers ()
241 // FIXME: check if build succeeded
242 LogBuildFinished (true);
243 foreach (ILogger i in loggers) {
249 private void LogProjectStarted (Project project, string[] targetNames)
251 ProjectStartedEventArgs psea;
252 if (targetNames.Length == 0) {
253 if (project.DefaultTargets != String.Empty)
254 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
255 project.DefaultTargets, null, null);
257 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default", null, null);
259 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
260 targetNames), null, null);
261 eventSource.FireProjectStarted (this, psea);
264 private void LogProjectFinished (Project project, bool succeeded)
266 ProjectFinishedEventArgs pfea;
267 pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
268 eventSource.FireProjectFinished (this, pfea);
271 private void LogBuildStarted ()
273 BuildStartedEventArgs bsea;
274 bsea = new BuildStartedEventArgs ("Build started.", null);
275 eventSource.FireBuildStarted (this, bsea);
278 private void LogBuildFinished (bool succeeded)
280 BuildFinishedEventArgs bfea;
281 bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
282 eventSource.FireBuildFinished (this, bfea);
285 public string BinPath {
286 get { return binPath; }
287 set { binPath = value; }
290 public bool BuildEnabled {
291 get { return buildEnabled; }
292 set { buildEnabled = value; }
295 public static Version Version {
296 get { return version; }
299 public static Engine GlobalEngine {
301 if (globalEngine == null) {
302 globalEngine = new Engine ();
308 public BuildPropertyGroup GlobalProperties {
309 get { return globalProperties; }
310 set { globalProperties = value; }
313 public bool OnlyLogCriticalEvents {
314 get { return eventSource.OnlyLogCriticalEvents; }
315 set { eventSource.OnlyLogCriticalEvents = value; }
318 internal EventSource EventSource {
319 get { return eventSource; }