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 foreach (LoggerInfo li in parameters.Loggers) {
104 if (li.InfoType == LoadInfoType.AssemblyFilename)
105 assembly = Assembly.LoadFrom (li.Filename);
107 assembly = Assembly.Load (li.AssemblyName);
108 ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
109 logger.Parameters = li.Parameters;
110 engine.RegisterLogger (logger);
113 project = engine.CreateNewProject ();
115 if (parameters.Validate) {
116 if (parameters.ValidationSchema == null)
117 project.SchemaFile = defaultSchema;
119 project.SchemaFile = parameters.ValidationSchema;
122 string projectFile = parameters.ProjectFile;
123 if (!File.Exists (projectFile)) {
124 ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
128 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
131 catch (InvalidProjectFileException ipfe) {
132 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
135 catch (InternalLoggerException ile) {
136 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
139 catch (CommandLineException cle) {
140 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
149 engine.UnregisterAllLoggers ();
151 Environment.Exit (result ? 0 : 1);
157 // code from mcs/report.cs
158 class ConsoleReportPrinter
160 string prefix, postfix;
161 bool color_supported;
163 string [] colorPrefixes;
165 public ConsoleReportPrinter ()
170 public ConsoleReportPrinter (TextWriter writer)
172 this.writer = writer;
174 string term = Environment.GetEnvironmentVariable ("TERM");
175 bool xterm_colors = false;
177 color_supported = false;
182 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
194 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
197 color_supported = true;
198 PopulateColorPrefixes ();
199 postfix = "\x001b[0m";
202 void PopulateColorPrefixes ()
204 colorPrefixes = new string [16];
206 colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
207 colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
208 colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
209 colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
210 colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
211 colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
212 colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
213 colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
215 colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
216 colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
217 colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
218 colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
219 colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
220 colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
221 colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
223 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
226 public void SetForeground (ConsoleColor color)
229 prefix = colorPrefixes [(int)color];
232 public void ResetColor ()
234 prefix = "\x001b[0m";
237 static int NameToCode (string s)
262 // maps a color name to its xterm color code
264 static string GetForeground (string s)
268 if (s.StartsWith ("bright")) {
274 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
277 static string GetBackground (string s)
279 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
282 string FormatText (string txt)
284 if (prefix != null && color_supported)
285 return prefix + txt + postfix;
290 public void Print (string message)
292 writer.WriteLine (FormatText (message));
298 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
299 extern static int _isatty (int fd);
301 public static bool isatty (int fd)
304 return _isatty (fd) == 1;