2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / tools / xbuild / Main.cs
blob89c84632a8d3737dc9e6513211a52fdc6bef7125
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)
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.
31 #if NET_2_0
33 using System;
34 using System.Collections;
35 using System.IO;
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;
46 string[] args;
47 string defaultSchema;
49 Engine engine;
50 Project project;
51 ConsoleReportPrinter printer;
54 public static void Main (string[] args)
56 MainClass mc = new MainClass ();
57 mc.args = args;
58 mc.Execute ();
61 public MainClass ()
63 string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
64 defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
65 parameters = new Parameters ();
68 public void Execute ()
70 bool result = false;
71 bool show_stacktrace = false;
73 try {
74 parameters.ParseArguments (args);
75 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
76 parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
78 if (parameters.DisplayVersion)
79 ErrorUtilities.ShowVersion (false);
81 //FIXME: cmd line arg to set toolsversion
82 engine = Engine.GlobalEngine;
83 if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
84 if (engine.Toolsets [parameters.ToolsVersion] == null)
85 ErrorUtilities.ReportError (0, String.Format ("Unknown tools version : {0}", parameters.ToolsVersion));
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) {
103 Assembly assembly;
104 if (li.InfoType == LoadInfoType.AssemblyFilename)
105 assembly = Assembly.LoadFrom (li.Filename);
106 else
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;
118 else
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));
125 return;
128 project.Load (projectFile);
130 string oldCurrentDirectory = Environment.CurrentDirectory;
131 string dir = Path.GetDirectoryName (projectFile);
132 if (!String.IsNullOrEmpty (dir))
133 Directory.SetCurrentDirectory (dir);
134 result = engine.BuildProject (project, parameters.Targets, null);
135 Directory.SetCurrentDirectory (oldCurrentDirectory);
138 catch (InvalidProjectFileException ipfe) {
139 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
142 catch (InternalLoggerException ile) {
143 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
146 catch (CommandLineException cle) {
147 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
150 catch (Exception) {
151 throw;
154 finally {
155 if (engine != null)
156 engine.UnregisterAllLoggers ();
158 Environment.Exit (result ? 0 : 1);
165 // code from mcs/report.cs
166 class ConsoleReportPrinter
168 string prefix, postfix;
169 bool color_supported;
170 TextWriter writer;
171 string [] colorPrefixes;
173 public ConsoleReportPrinter ()
174 : this (Console.Out)
178 public ConsoleReportPrinter (TextWriter writer)
180 this.writer = writer;
182 string term = Environment.GetEnvironmentVariable ("TERM");
183 bool xterm_colors = false;
185 color_supported = false;
186 switch (term){
187 case "xterm":
188 case "rxvt":
189 case "rxvt-unicode":
190 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
191 xterm_colors = true;
193 break;
195 case "xterm-color":
196 xterm_colors = true;
197 break;
199 if (!xterm_colors)
200 return;
202 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
203 return;
205 color_supported = true;
206 PopulateColorPrefixes ();
207 postfix = "\x001b[0m";
210 void PopulateColorPrefixes ()
212 colorPrefixes = new string [16];
214 colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
215 colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
216 colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
217 colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
218 colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
219 colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
220 colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
221 colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
223 colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
224 colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
225 colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
226 colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
227 colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
228 colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
229 colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
231 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
234 public void SetForeground (ConsoleColor color)
236 if (color_supported)
237 prefix = colorPrefixes [(int)color];
240 public void ResetColor ()
242 prefix = "\x001b[0m";
245 static int NameToCode (string s)
247 switch (s) {
248 case "black":
249 return 0;
250 case "red":
251 return 1;
252 case "green":
253 return 2;
254 case "yellow":
255 return 3;
256 case "blue":
257 return 4;
258 case "magenta":
259 return 5;
260 case "cyan":
261 return 6;
262 case "grey":
263 case "white":
264 return 7;
266 return 7;
270 // maps a color name to its xterm color code
272 static string GetForeground (string s)
274 string highcode;
276 if (s.StartsWith ("bright")) {
277 highcode = "1;";
278 s = s.Substring (6);
279 } else
280 highcode = "";
282 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
285 static string GetBackground (string s)
287 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
290 string FormatText (string txt)
292 if (prefix != null && color_supported)
293 return prefix + txt + postfix;
295 return txt;
298 public void Print (string message)
300 writer.WriteLine (FormatText (message));
305 class UnixUtils {
306 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
307 extern static int _isatty (int fd);
309 public static bool isatty (int fd)
311 try {
312 return _isatty (fd) == 1;
313 } catch {
314 return false;
321 #endif