2 // Main.cs: Main program file of command line utility.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Marek Safar (marek.safar@seznam.cz)
9 // (C) 2005 Marek Sieradzki
10 // Copyright 2009 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 using System.Reflection;
38 using Microsoft.Build.BuildEngine;
39 using Microsoft.Build.Framework;
40 using Microsoft.Build.Utilities;
41 using Mono.XBuild.Framework;
43 namespace Mono.XBuild.CommandLine {
44 public class MainClass {
46 Parameters parameters;
52 ConsoleReportPrinter printer;
55 public static void Main (string[] args)
57 MainClass mc = new MainClass ();
64 string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
65 defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
66 parameters = new Parameters ();
69 public void Execute ()
72 bool show_stacktrace = false;
75 parameters.ParseArguments (args);
76 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
77 parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
79 if (!parameters.NoLogo)
80 ErrorUtilities.ShowVersion (false);
82 engine = Engine.GlobalEngine;
83 if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
84 if (engine.Toolsets [parameters.ToolsVersion] == null)
85 ErrorUtilities.ReportError (0, new UnknownToolsVersionException (parameters.ToolsVersion).Message);
87 engine.DefaultToolsVersion = parameters.ToolsVersion;
90 engine.GlobalProperties = this.parameters.Properties;
92 if (!parameters.NoConsoleLogger) {
93 printer = new ConsoleReportPrinter ();
94 ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
95 printer.Print, printer.SetForeground, printer.ResetColor);
97 cl.Parameters = parameters.ConsoleLoggerParameters;
98 cl.Verbosity = parameters.LoggerVerbosity;
99 engine.RegisterLogger (cl);
102 if (parameters.FileLoggerParameters != null) {
103 for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
104 string fl_params = parameters.FileLoggerParameters [i];
105 if (fl_params == null)
108 var fl = new FileLogger ();
109 if (fl_params.Length == 0 && i > 0)
110 fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
112 fl.Parameters = fl_params;
113 engine.RegisterLogger (fl);
117 foreach (LoggerInfo li in parameters.Loggers) {
119 if (li.InfoType == LoadInfoType.AssemblyFilename)
120 assembly = Assembly.LoadFrom (li.Filename);
122 assembly = Assembly.Load (li.AssemblyName);
123 ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
124 logger.Parameters = li.Parameters;
125 engine.RegisterLogger (logger);
128 project = engine.CreateNewProject ();
130 if (parameters.Validate) {
131 if (parameters.ValidationSchema == null)
132 project.SchemaFile = defaultSchema;
134 project.SchemaFile = parameters.ValidationSchema;
137 string projectFile = parameters.ProjectFile;
138 if (!File.Exists (projectFile)) {
139 ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
143 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
146 catch (InvalidProjectFileException ipfe) {
147 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
150 catch (InternalLoggerException ile) {
151 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
154 catch (CommandLineException cle) {
155 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
159 engine.UnregisterAllLoggers ();
161 Environment.Exit (result ? 0 : 1);
167 // code from mcs/report.cs
168 class ConsoleReportPrinter
170 string prefix, postfix;
171 bool color_supported;
173 string [] colorPrefixes;
175 public ConsoleReportPrinter ()
180 public ConsoleReportPrinter (TextWriter writer)
182 this.writer = writer;
184 string term = Environment.GetEnvironmentVariable ("TERM");
185 bool xterm_colors = false;
187 color_supported = false;
192 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
204 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
207 color_supported = true;
208 PopulateColorPrefixes ();
209 postfix = "\x001b[0m";
212 void PopulateColorPrefixes ()
214 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");
225 colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
226 colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
227 colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
228 colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
229 colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
230 colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
231 colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
233 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
236 public void SetForeground (ConsoleColor color)
239 prefix = colorPrefixes [(int)color];
242 public void ResetColor ()
244 prefix = "\x001b[0m";
247 static int NameToCode (string s)
272 // maps a color name to its xterm color code
274 static string GetForeground (string s)
278 if (s.StartsWith ("bright")) {
284 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
287 static string GetBackground (string s)
289 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
292 string FormatText (string txt)
294 if (prefix != null && color_supported)
295 return prefix + txt + postfix;
300 public void Print (string message)
302 writer.WriteLine (FormatText (message));
308 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
309 extern static int _isatty (int fd);
311 public static bool isatty (int fd)
314 return _isatty (fd) == 1;