Engine.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Engine.cs: Main engine of XBuild.
  3. //
  4. // Author:
  5. // Marek Sieradzki ([email protected])
  6. //
  7. // (C) 2005 Marek Sieradzki
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. #if NET_2_0
  28. using System;
  29. using System.Collections;
  30. using Microsoft.Build.Framework;
  31. namespace Microsoft.Build.BuildEngine {
  32. public class Engine : IEngine {
  33. string binPath;
  34. bool buildEnabled;
  35. const string engineVersion = "0.1";
  36. BuildPropertyGroup environmentProperties;
  37. EventSource eventSource;
  38. bool buildStarted;
  39. BuildPropertyGroup globalProperties;
  40. IDictionary importedProjects;
  41. IList loggers;
  42. bool onlyLogCriticalEvents;
  43. IDictionary projects;
  44. BuildPropertyGroup reservedProperties;
  45. // FIXME: GlobalEngine static property uses this but what about GlobalEngineAccessor?
  46. static Engine globalEngine;
  47. public Engine ()
  48. : this (null)
  49. {
  50. }
  51. // engine should be invoked with path where binary files are
  52. // to find microsoft.build.tasks
  53. public Engine (string binPath)
  54. {
  55. this.binPath = binPath;
  56. this.projects = new Hashtable ();
  57. this.eventSource = new EventSource ();
  58. this.loggers = new ArrayList ();
  59. this.buildStarted = false;
  60. this.LoadEnvironmentProperties ();
  61. this.reservedProperties = new BuildPropertyGroup ();
  62. this.reservedProperties.AddNewProperty ("MSBuildBinPath", binPath, PropertyType.Reserved);
  63. }
  64. public bool BuildProject (Project project,
  65. string[] targetNames,
  66. IDictionary targetOutputs)
  67. {
  68. bool result;
  69. LogProjectStarted (project, targetNames);
  70. result = project.Build (targetNames, targetOutputs);
  71. LogProjectFinished (project, result);
  72. return result;
  73. }
  74. public bool BuildProjectFile (string projectFileName,
  75. string[] targetNames,
  76. BuildPropertyGroup globalPropertiesToUse,
  77. IDictionary targetOutputs)
  78. {
  79. bool result;
  80. Project project;
  81. if (projects.Contains (projectFileName)) {
  82. project = (Project) projects [projectFileName];
  83. LogProjectStarted (project, targetNames);
  84. result = project.Build (targetNames, targetOutputs);
  85. }
  86. else
  87. return false;
  88. LogProjectFinished (project, result);
  89. return result;
  90. }
  91. public void ClearAllProjects ()
  92. {
  93. projects.Clear ();
  94. }
  95. public Project CreateNewProject ()
  96. {
  97. if (buildStarted == false) {
  98. LogBuildStarted ();
  99. buildStarted = true;
  100. }
  101. Project p = new Project (this);
  102. p.EnvironmentProperties = this.environmentProperties;
  103. p.ReservedProperties = this.reservedProperties;
  104. if (globalProperties != null) {
  105. BuildPropertyGroup bpg = new BuildPropertyGroup ();
  106. foreach (BuildProperty bp in globalProperties)
  107. bpg.AddNewProperty (bp.Name, bp.Value, PropertyType.CommandLine);
  108. p.GlobalProperties = bpg;
  109. }
  110. return p;
  111. }
  112. public string Escape (string input)
  113. {
  114. // FIXME: test it, probably returns XML escaped string
  115. return null;
  116. }
  117. public Project GetLoadedProject (string projectFullFileName)
  118. {
  119. return (Project) projects [projectFullFileName];
  120. }
  121. public void RegisterLogger (ILogger logger)
  122. {
  123. if (logger == null)
  124. throw new ArgumentNullException ("logger");
  125. logger.Initialize (eventSource);
  126. loggers.Add (logger);
  127. }
  128. public void UnregisterAllLoggers ()
  129. {
  130. // FIXME: check if build succeeded
  131. LogBuildFinished (true);
  132. foreach (ILogger i in loggers) {
  133. i.Shutdown ();
  134. }
  135. loggers.Clear ();
  136. }
  137. private void LoadEnvironmentProperties ()
  138. {
  139. environmentProperties = new BuildPropertyGroup ();
  140. IDictionary environment = Environment.GetEnvironmentVariables ();
  141. foreach (DictionaryEntry de in environment) {
  142. environmentProperties.AddNewProperty ((string) de.Key, (string) de.Value, PropertyType.Environment);
  143. }
  144. }
  145. private void LogProjectStarted (Project project, string[] targetNames)
  146. {
  147. ProjectStartedEventArgs psea;
  148. if (targetNames.Length == 0) {
  149. if (project.DefaultTargets != String.Empty)
  150. psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
  151. project.DefaultTargets);
  152. else
  153. psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default");
  154. } else
  155. psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
  156. targetNames));
  157. eventSource.FireProjectStarted (this, psea);
  158. }
  159. private void LogProjectFinished (Project project, bool succeeded)
  160. {
  161. ProjectFinishedEventArgs pfea;
  162. pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
  163. eventSource.FireProjectFinished (this, pfea);
  164. }
  165. private void LogBuildStarted ()
  166. {
  167. BuildStartedEventArgs bsea;
  168. bsea = new BuildStartedEventArgs ("Build started.", null);
  169. eventSource.FireBuildStarted (this, bsea);
  170. }
  171. private void LogBuildFinished (bool succeeded)
  172. {
  173. BuildFinishedEventArgs bfea;
  174. bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
  175. eventSource.FireBuildFinished (this, bfea);
  176. }
  177. public string BinPath {
  178. get { return binPath; }
  179. set { binPath = value; }
  180. }
  181. public bool BuildEnabled {
  182. get { return buildEnabled; }
  183. set { buildEnabled = value; }
  184. }
  185. public static string EngineVersion {
  186. get { return engineVersion; }
  187. }
  188. public static Engine GlobalEngine {
  189. get { return globalEngine; }
  190. }
  191. public BuildPropertyGroup GlobalProperties {
  192. get { return globalProperties; }
  193. set { globalProperties = value; }
  194. }
  195. public bool OnlyLogCriticalEvents {
  196. get { return eventSource.OnlyLogCriticalEvents; }
  197. set { eventSource.OnlyLogCriticalEvents = value; }
  198. }
  199. internal EventSource EventSource {
  200. get { return eventSource; }
  201. }
  202. }
  203. }
  204. #endif