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, PropertyType.Reserved);
77 public bool BuildProject (Project project,
79 IDictionary targetOutputs)
83 LogProjectStarted (project, targetNames);
85 result = project.Build (targetNames, targetOutputs);
87 LogProjectFinished (project, result);
92 public bool BuildProjectFile (string projectFileName,
94 BuildPropertyGroup globalPropertiesToUse,
95 IDictionary targetOutputs)
100 if (projects.Contains (projectFileName)) {
101 project = (Project) projects [projectFileName];
102 LogProjectStarted (project, targetNames);
103 result = project.Build (targetNames, targetOutputs);
108 LogProjectFinished (project, result);
113 public Project CreateNewProject ()
115 if (buildStarted == false) {
119 Project p = new Project (this);
120 p.EnvironmentProperties = this.environmentProperties;
121 p.ReservedProperties = this.reservedProperties;
122 if (globalProperties != null) {
123 BuildPropertyGroup bpg = new BuildPropertyGroup ();
124 foreach (BuildProperty bp in globalProperties)
125 bpg.AddNewProperty (bp.Name, bp.Value, PropertyType.CommandLine);
126 p.GlobalProperties = bpg;
131 public Project GetLoadedProject (string projectFullFileName)
133 return (Project) projects [projectFullFileName];
136 public void RegisterLogger (ILogger logger)
139 throw new ArgumentNullException ("logger");
140 logger.Initialize (eventSource);
141 loggers.Add (logger);
145 public void UnloadAllProjects ()
150 public void UnloadProject (Project project)
154 public void UnregisterAllLoggers ()
156 // FIXME: check if build succeeded
157 LogBuildFinished (true);
158 foreach (ILogger i in loggers) {
164 private void LoadEnvironmentProperties ()
166 environmentProperties = new BuildPropertyGroup ();
167 IDictionary environment = Environment.GetEnvironmentVariables ();
168 foreach (DictionaryEntry de in environment) {
169 environmentProperties.AddNewProperty ((string) de.Key, (string) de.Value, PropertyType.Environment);
173 private void LogProjectStarted (Project project, string[] targetNames)
175 ProjectStartedEventArgs psea;
176 if (targetNames.Length == 0) {
177 if (project.DefaultTargets != String.Empty)
178 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
179 project.DefaultTargets, null, null);
181 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default", null, null);
183 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
184 targetNames), null, null);
185 eventSource.FireProjectStarted (this, psea);
188 private void LogProjectFinished (Project project, bool succeeded)
190 ProjectFinishedEventArgs pfea;
191 pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
192 eventSource.FireProjectFinished (this, pfea);
195 private void LogBuildStarted ()
197 BuildStartedEventArgs bsea;
198 bsea = new BuildStartedEventArgs ("Build started.", null);
199 eventSource.FireBuildStarted (this, bsea);
202 private void LogBuildFinished (bool succeeded)
204 BuildFinishedEventArgs bfea;
205 bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
206 eventSource.FireBuildFinished (this, bfea);
209 public string BinPath {
210 get { return binPath; }
211 set { binPath = value; }
214 public bool BuildEnabled {
215 get { return buildEnabled; }
216 set { buildEnabled = value; }
219 public static Version Version {
220 get { return version; }
223 public static Engine GlobalEngine {
224 get { return globalEngine; }
227 public BuildPropertyGroup GlobalProperties {
228 get { return globalProperties; }
229 set { globalProperties = value; }
232 public bool OnlyLogCriticalEvents {
233 get { return eventSource.OnlyLogCriticalEvents; }
234 set { eventSource.OnlyLogCriticalEvents = value; }
237 internal EventSource EventSource {
238 get { return eventSource; }