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 {
35 public class Engine : IEngine {
39 const string engineVersion = "0.1";
40 BuildPropertyGroup environmentProperties;
41 EventSource eventSource;
43 BuildPropertyGroup globalProperties;
44 IDictionary importedProjects;
46 bool onlyLogCriticalEvents;
48 BuildPropertyGroup reservedProperties;
50 // FIXME: GlobalEngine static property uses this but what about GlobalEngineAccessor?
51 static Engine globalEngine;
59 // engine should be invoked with path where binary files are
60 // to find microsoft.build.tasks
61 public Engine (string binPath)
63 this.binPath = binPath;
64 this.projects = new Hashtable ();
65 this.eventSource = new EventSource ();
66 this.loggers = new ArrayList ();
67 this.buildStarted = false;
68 this.LoadEnvironmentProperties ();
69 this.reservedProperties = new BuildPropertyGroup ();
70 this.reservedProperties.AddNewProperty ("MSBuildBinPath", binPath, PropertyType.Reserved);
73 public bool BuildProject (Project project,
75 IDictionary targetOutputs)
79 LogProjectStarted (project, targetNames);
81 result = project.Build (targetNames, targetOutputs);
83 LogProjectFinished (project, result);
88 public bool BuildProjectFile (string projectFileName,
90 BuildPropertyGroup globalPropertiesToUse,
91 IDictionary targetOutputs)
96 if (projects.Contains (projectFileName)) {
97 project = (Project) projects [projectFileName];
98 LogProjectStarted (project, targetNames);
99 result = project.Build (targetNames, targetOutputs);
104 LogProjectFinished (project, result);
109 public void ClearAllProjects ()
114 public Project CreateNewProject ()
116 if (buildStarted == false) {
120 Project p = new Project (this);
121 p.EnvironmentProperties = this.environmentProperties;
122 p.ReservedProperties = this.reservedProperties;
123 if (globalProperties != null) {
124 BuildPropertyGroup bpg = new BuildPropertyGroup ();
125 foreach (BuildProperty bp in globalProperties)
126 bpg.AddNewProperty (bp.Name, bp.Value, PropertyType.CommandLine);
127 p.GlobalProperties = bpg;
132 public string Escape (string input)
134 // FIXME: test it, probably returns XML escaped string
138 public Project GetLoadedProject (string projectFullFileName)
140 return (Project) projects [projectFullFileName];
143 public void RegisterLogger (ILogger logger)
146 throw new ArgumentNullException ("logger");
147 logger.Initialize (eventSource);
148 loggers.Add (logger);
151 public void UnregisterAllLoggers ()
153 // FIXME: check if build succeeded
154 LogBuildFinished (true);
155 foreach (ILogger i in loggers) {
161 private void LoadEnvironmentProperties ()
163 environmentProperties = new BuildPropertyGroup ();
164 IDictionary environment = Environment.GetEnvironmentVariables ();
165 foreach (DictionaryEntry de in environment) {
166 environmentProperties.AddNewProperty ((string) de.Key, (string) de.Value, PropertyType.Environment);
170 private void LogProjectStarted (Project project, string[] targetNames)
172 ProjectStartedEventArgs psea;
173 if (targetNames.Length == 0) {
174 if (project.DefaultTargets != String.Empty)
175 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
176 project.DefaultTargets);
178 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default");
180 psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
182 eventSource.FireProjectStarted (this, psea);
185 private void LogProjectFinished (Project project, bool succeeded)
187 ProjectFinishedEventArgs pfea;
188 pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
189 eventSource.FireProjectFinished (this, pfea);
192 private void LogBuildStarted ()
194 BuildStartedEventArgs bsea;
195 bsea = new BuildStartedEventArgs ("Build started.", null);
196 eventSource.FireBuildStarted (this, bsea);
199 private void LogBuildFinished (bool succeeded)
201 BuildFinishedEventArgs bfea;
202 bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
203 eventSource.FireBuildFinished (this, bfea);
206 public string BinPath {
207 get { return binPath; }
208 set { binPath = value; }
211 public bool BuildEnabled {
212 get { return buildEnabled; }
213 set { buildEnabled = value; }
216 public static string EngineVersion {
217 get { return engineVersion; }
220 public static Engine GlobalEngine {
221 get { return globalEngine; }
224 public BuildPropertyGroup GlobalProperties {
225 get { return globalProperties; }
226 set { globalProperties = value; }
229 public bool OnlyLogCriticalEvents {
230 get { return eventSource.OnlyLogCriticalEvents; }
231 set { eventSource.OnlyLogCriticalEvents = value; }
234 internal EventSource EventSource {
235 get { return eventSource; }