PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/tools/msbuild/Main.cs

https://github.com/ancailliau/mono
C# | 360 lines | 269 code | 55 blank | 36 comment | 37 complexity | 25874b6a07fde8ace02558eb0cd38999 MD5 | raw file
  1. //
  2. // Main.cs: Main program file of command line utility.
  3. //
  4. // Author:
  5. // Marek Sieradzki (marek.sieradzki@gmail.com)
  6. // Miguel de Icaza (miguel@ximian.com)
  7. // Marek Safar (marek.safar@seznam.cz)
  8. //
  9. // (C) 2005 Marek Sieradzki
  10. // Copyright 2009 Novell, Inc (http://www.novell.com)
  11. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. using Microsoft.Build.Utilities;
  32. using Microsoft.Build.Exceptions;
  33. using Microsoft.Build.Construction;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Linq;
  37. using System.Collections.Generic;
  38. using System;
  39. using System.Collections;
  40. using System.IO;
  41. using System.Reflection;
  42. using System.Text;
  43. using Microsoft.Build.Evaluation;
  44. using Microsoft.Build.Execution;
  45. using Microsoft.Build.Framework;
  46. using Microsoft.Build.Logging;
  47. using Mono.XBuild.Framework;
  48. class MonoTODOAttribute : Attribute
  49. {
  50. }
  51. namespace Mono.XBuild.CommandLine {
  52. public class MainClass {
  53. Parameters parameters;
  54. string[] args;
  55. string defaultSchema;
  56. ProjectCollection project_collection;
  57. ProjectRootElement project;
  58. ConsoleReportPrinter printer;
  59. public static void Main (string[] args)
  60. {
  61. MainClass mc = new MainClass ();
  62. mc.args = args;
  63. mc.Execute ();
  64. }
  65. public MainClass ()
  66. {
  67. string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
  68. defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
  69. parameters = new Parameters ();
  70. }
  71. public void Execute ()
  72. {
  73. bool result = false;
  74. bool show_stacktrace = false;
  75. try {
  76. parameters.ParseArguments (args);
  77. show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
  78. parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
  79. if (!parameters.NoLogo)
  80. ErrorUtilities.ShowVersion (false);
  81. project_collection = new ProjectCollection ();
  82. if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
  83. if (project_collection.GetToolset (parameters.ToolsVersion) == null)
  84. ErrorUtilities.ReportError (0, new InvalidToolsetDefinitionException ("Toolset " + parameters.ToolsVersion + " was not found").Message);
  85. project_collection.DefaultToolsVersion = parameters.ToolsVersion;
  86. }
  87. foreach (var p in parameters.Properties)
  88. project_collection.GlobalProperties.Add (p.Key, p.Value);
  89. if (!parameters.NoConsoleLogger) {
  90. printer = new ConsoleReportPrinter ();
  91. ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
  92. printer.Print, printer.SetForeground, printer.ResetColor);
  93. cl.Parameters = parameters.ConsoleLoggerParameters;
  94. cl.Verbosity = parameters.LoggerVerbosity;
  95. project_collection.RegisterLogger (cl);
  96. }
  97. if (parameters.FileLoggerParameters != null) {
  98. for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
  99. string fl_params = parameters.FileLoggerParameters [i];
  100. if (fl_params == null)
  101. continue;
  102. var fl = new FileLogger ();
  103. if (fl_params.Length == 0 && i > 0)
  104. fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
  105. else
  106. fl.Parameters = fl_params;
  107. project_collection.RegisterLogger (fl);
  108. }
  109. }
  110. foreach (LoggerInfo li in parameters.Loggers) {
  111. Assembly assembly;
  112. if (li.InfoType == LoadInfoType.AssemblyFilename)
  113. assembly = Assembly.LoadFrom (li.Filename);
  114. else
  115. assembly = Assembly.Load (li.AssemblyName);
  116. ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
  117. logger.Parameters = li.Parameters;
  118. project_collection.RegisterLogger (logger);
  119. }
  120. string projectFile = parameters.ProjectFile;
  121. if (!File.Exists (projectFile)) {
  122. ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
  123. return;
  124. }
  125. XmlReaderSettings settings = new XmlReaderSettings ();
  126. if (parameters.Validate) {
  127. settings.ValidationType = ValidationType.Schema;
  128. if (parameters.ValidationSchema == null)
  129. using (var xsdxml = XmlReader.Create (defaultSchema))
  130. settings.Schemas.Add (XmlSchema.Read (xsdxml, null));
  131. else
  132. using (var xsdxml = XmlReader.Create (parameters.ValidationSchema))
  133. settings.Schemas.Add (XmlSchema.Read (xsdxml, null));
  134. }
  135. var projectInstances = new List<ProjectInstance> ();
  136. if (string.Equals (Path.GetExtension (projectFile), ".sln", StringComparison.OrdinalIgnoreCase)) {
  137. var parser = new SolutionParser ();
  138. var root = ProjectRootElement.Create (project_collection);
  139. root.FullPath = projectFile;
  140. parser.ParseSolution (projectFile, project_collection, root, LogWarning);
  141. projectInstances.Add (new Project (root, parameters.Properties, parameters.ToolsVersion, project_collection).CreateProjectInstance ());
  142. } else {
  143. project = ProjectRootElement.Create (XmlReader.Create (projectFile, settings), project_collection);
  144. project.FullPath = projectFile;
  145. var pi = new ProjectInstance (project, parameters.Properties, parameters.ToolsVersion, project_collection);
  146. projectInstances.Add (pi);
  147. }
  148. foreach (var projectInstance in projectInstances) {
  149. var targets = parameters.Targets.Length > 0 ? parameters.Targets : projectInstance.DefaultTargets.ToArray ();
  150. result = projectInstance.Build (targets, parameters.Loggers.Count > 0 ? parameters.Loggers : project_collection.Loggers);
  151. if (!result)
  152. break;
  153. }
  154. }
  155. catch (InvalidProjectFileException ipfe) {
  156. ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
  157. }
  158. catch (InternalLoggerException ile) {
  159. ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
  160. }
  161. catch (CommandLineException cle) {
  162. ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
  163. }
  164. finally {
  165. //if (project_collection != null)
  166. // project_collection.UnregisterAllLoggers ();
  167. Environment.Exit (result ? 0 : 1);
  168. }
  169. }
  170. void LogWarning (int errorNumber, string message)
  171. {
  172. Console.Error.WriteLine ("Warning {0}: {1}", errorNumber, message);
  173. }
  174. }
  175. // code from mcs/report.cs
  176. class ConsoleReportPrinter
  177. {
  178. string prefix, postfix;
  179. bool color_supported;
  180. TextWriter writer;
  181. string [] colorPrefixes;
  182. public ConsoleReportPrinter ()
  183. : this (Console.Out)
  184. {
  185. }
  186. public ConsoleReportPrinter (TextWriter writer)
  187. {
  188. this.writer = writer;
  189. string term = Environment.GetEnvironmentVariable ("TERM");
  190. bool xterm_colors = false;
  191. color_supported = false;
  192. switch (term){
  193. case "xterm":
  194. case "rxvt":
  195. case "rxvt-unicode":
  196. if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
  197. xterm_colors = true;
  198. }
  199. break;
  200. case "xterm-color":
  201. case "xterm-256color":
  202. xterm_colors = true;
  203. break;
  204. }
  205. if (!xterm_colors)
  206. return;
  207. if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
  208. return;
  209. color_supported = true;
  210. PopulateColorPrefixes ();
  211. postfix = "\x001b[0m";
  212. }
  213. void PopulateColorPrefixes ()
  214. {
  215. colorPrefixes = new string [16];
  216. colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
  217. colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
  218. colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
  219. colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
  220. colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
  221. colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
  222. colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
  223. colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
  224. colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
  225. colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
  226. colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
  227. colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
  228. colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
  229. colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
  230. colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
  231. colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
  232. }
  233. public void SetForeground (ConsoleColor color)
  234. {
  235. if (color_supported)
  236. prefix = colorPrefixes [(int)color];
  237. }
  238. public void ResetColor ()
  239. {
  240. prefix = "\x001b[0m";
  241. }
  242. static int NameToCode (string s)
  243. {
  244. switch (s) {
  245. case "black":
  246. return 0;
  247. case "red":
  248. return 1;
  249. case "green":
  250. return 2;
  251. case "yellow":
  252. return 3;
  253. case "blue":
  254. return 4;
  255. case "magenta":
  256. return 5;
  257. case "cyan":
  258. return 6;
  259. case "grey":
  260. case "white":
  261. return 7;
  262. }
  263. return 7;
  264. }
  265. //
  266. // maps a color name to its xterm color code
  267. //
  268. static string GetForeground (string s)
  269. {
  270. string highcode;
  271. if (s.StartsWith ("bright")) {
  272. highcode = "1;";
  273. s = s.Substring (6);
  274. } else
  275. highcode = "";
  276. return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
  277. }
  278. static string GetBackground (string s)
  279. {
  280. return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
  281. }
  282. string FormatText (string txt)
  283. {
  284. if (prefix != null && color_supported)
  285. return prefix + txt + postfix;
  286. return txt;
  287. }
  288. public void Print (string message)
  289. {
  290. writer.WriteLine (FormatText (message));
  291. }
  292. }
  293. class UnixUtils {
  294. [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
  295. extern static int _isatty (int fd);
  296. public static bool isatty (int fd)
  297. {
  298. try {
  299. return _isatty (fd) == 1;
  300. } catch {
  301. return false;
  302. }
  303. }
  304. }
  305. }