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;
37 using Microsoft.Build.BuildEngine;
38 using Microsoft.Build.Framework;
39 using Microsoft.Build.Utilities;
40 using Mono.XBuild.Framework;
42 namespace Mono.XBuild.CommandLine {
43 public class MainClass {
45 Parameters parameters;
52 ConsoleReportPrinter printer;
55 public static void Main (string[] args)
57 MainClass mc = new MainClass ();
64 binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
65 defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
66 parameters = new Parameters (binPath);
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.DisplayVersion)
80 ErrorUtilities.ShowVersion (false);
82 engine = new Engine (binPath);
84 engine.GlobalProperties = this.parameters.Properties;
86 if (!parameters.NoConsoleLogger) {
87 printer = new ConsoleReportPrinter ();
88 ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
89 printer.Print, printer.SetForeground, printer.ResetColor);
91 cl.Parameters = parameters.ConsoleLoggerParameters;
92 cl.Verbosity = parameters.LoggerVerbosity;
93 engine.RegisterLogger (cl);
96 foreach (LoggerInfo li in parameters.Loggers) {
98 if (li.InfoType == LoadInfoType.AssemblyFilename)
99 assembly = Assembly.LoadFrom (li.Filename);
101 assembly = Assembly.Load (li.AssemblyName);
102 ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
103 logger.Parameters = li.Parameters;
104 engine.RegisterLogger (logger);
107 project = engine.CreateNewProject ();
109 if (parameters.Validate) {
110 if (parameters.ValidationSchema == null)
111 project.SchemaFile = defaultSchema;
113 project.SchemaFile = parameters.ValidationSchema;
116 string projectFile = parameters.ProjectFile;
117 if (!File.Exists (projectFile)) {
118 ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
122 project.Load (projectFile);
124 string oldCurrentDirectory = Environment.CurrentDirectory;
125 string dir = Path.GetDirectoryName (projectFile);
126 if (!String.IsNullOrEmpty (dir))
127 Directory.SetCurrentDirectory (dir);
128 result = engine.BuildProject (project, parameters.Targets, null);
129 Directory.SetCurrentDirectory (oldCurrentDirectory);
132 catch (InvalidProjectFileException ipfe) {
133 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
136 catch (InternalLoggerException ile) {
137 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
140 catch (CommandLineException cle) {
141 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
150 engine.UnregisterAllLoggers ();
152 Environment.Exit (result ? 0 : 1);
159 // code from mcs/report.cs
160 class ConsoleReportPrinter
162 string prefix, postfix;
163 bool color_supported;
165 string [] colorPrefixes;
167 public ConsoleReportPrinter ()
172 public ConsoleReportPrinter (TextWriter writer)
174 this.writer = writer;
176 string term = Environment.GetEnvironmentVariable ("TERM");
177 bool xterm_colors = false;
179 color_supported = false;
184 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
196 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
199 color_supported = true;
200 PopulateColorPrefixes ();
201 postfix = "\x001b[0m";
204 void PopulateColorPrefixes ()
206 colorPrefixes = new string [16];
208 colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
209 colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
210 colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
211 colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
212 colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
213 colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
214 colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
215 colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
217 colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
218 colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
219 colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
220 colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
221 colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
222 colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
223 colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
225 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
228 public void SetForeground (ConsoleColor color)
231 prefix = colorPrefixes [(int)color];
234 public void ResetColor ()
236 prefix = "\x001b[0m";
239 static int NameToCode (string s)
264 // maps a color name to its xterm color code
266 static string GetForeground (string s)
270 if (s.StartsWith ("bright")) {
276 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
279 static string GetBackground (string s)
281 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
284 string FormatText (string txt)
286 if (prefix != null && color_supported)
287 return prefix + txt + postfix;
292 public void Print (string message)
294 writer.WriteLine (FormatText (message));
300 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
301 extern static int _isatty (int fd);
303 public static bool isatty (int fd)
306 return _isatty (fd) == 1;