Merge pull request #730 from LogosBible/locale-changes
[mono.git] / mcs / tools / xbuild / Main.cs
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
32 #if NET_2_0
33
34 using System;
35 using System.Collections;
36 using System.IO;
37 using System.Reflection;
38 using System.Text;
39 using Microsoft.Build.BuildEngine;
40 using Microsoft.Build.Framework;
41 using Microsoft.Build.Utilities;
42 using Mono.XBuild.Framework;
43
44 namespace Mono.XBuild.CommandLine {
45         public class MainClass {
46                 
47                 Parameters      parameters;
48                 string[]        args;
49                 string          defaultSchema;
50                 
51                 Engine          engine;
52                 Project         project;
53                 ConsoleReportPrinter printer;
54
55                 
56                 public static void Main (string[] args)
57                 {
58                         MainClass mc = new MainClass ();
59                         mc.args = args;
60                         mc.Execute ();
61                 }
62                 
63                 public MainClass ()
64                 {
65                         string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
66                         defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
67                         parameters = new Parameters ();
68                 }
69
70                 public void Execute ()
71                 {
72                         bool result = false;
73                         bool show_stacktrace = false;
74                         
75                         try {
76                                 parameters.ParseArguments (args);
77                                 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
78                                         parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
79                                 
80                                 if (!parameters.NoLogo)
81                                         ErrorUtilities.ShowVersion (false);
82                                 
83                                 engine  = Engine.GlobalEngine;
84                                 if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
85                                         if (engine.Toolsets [parameters.ToolsVersion] == null)
86                                                 ErrorUtilities.ReportError (0, new UnknownToolsVersionException (parameters.ToolsVersion).Message);
87
88                                         engine.DefaultToolsVersion = parameters.ToolsVersion;
89                                 }
90                                 
91                                 engine.GlobalProperties = this.parameters.Properties;
92                                 
93                                 if (!parameters.NoConsoleLogger) {
94                                         printer = new ConsoleReportPrinter ();
95                                         ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
96                                                         printer.Print, printer.SetForeground, printer.ResetColor);
97
98                                         cl.Parameters = parameters.ConsoleLoggerParameters;
99                                         cl.Verbosity = parameters.LoggerVerbosity; 
100                                         engine.RegisterLogger (cl);
101                                 }
102
103                                 if (parameters.FileLoggerParameters != null) {
104                                         for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
105                                                 string fl_params = parameters.FileLoggerParameters [i];
106                                                 if (fl_params == null)
107                                                         continue;
108
109                                                 var fl = new FileLogger ();
110                                                 if (fl_params.Length == 0 && i > 0)
111                                                         fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
112                                                 else
113                                                         fl.Parameters = fl_params;
114                                                 engine.RegisterLogger (fl);
115                                         }
116                                 }
117                                 
118                                 foreach (LoggerInfo li in parameters.Loggers) {
119                                         Assembly assembly;
120                                         if (li.InfoType == LoadInfoType.AssemblyFilename)
121                                                 assembly = Assembly.LoadFrom (li.Filename);
122                                         else
123                                                 assembly = Assembly.Load (li.AssemblyName);
124                                         ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
125                                         logger.Parameters = li.Parameters;
126                                         engine.RegisterLogger (logger); 
127                                 }
128                                 
129                                 project = engine.CreateNewProject ();
130                                 
131                                 if (parameters.Validate) {
132                                         if (parameters.ValidationSchema == null)
133                                                 project.SchemaFile = defaultSchema;
134                                         else
135                                                 project.SchemaFile = parameters.ValidationSchema;
136                                 }
137
138                                 string projectFile = parameters.ProjectFile;
139                                 if (!File.Exists (projectFile)) {
140                                         ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
141                                         return;
142                                 }
143
144                                 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
145                         }
146                         
147                         catch (InvalidProjectFileException ipfe) {
148                                 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
149                         }
150
151                         catch (InternalLoggerException ile) {
152                                 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
153                         }
154
155                         catch (CommandLineException cle) {
156                                 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
157                         }
158                         finally {
159                                 if (engine != null)
160                                         engine.UnregisterAllLoggers ();
161
162                                 Environment.Exit (result ? 0 : 1);
163                         }
164
165                 }
166         }
167
168         // code from mcs/report.cs
169         class ConsoleReportPrinter
170         {
171                 string prefix, postfix;
172                 bool color_supported;
173                 TextWriter writer;
174                 string [] colorPrefixes;
175
176                 public ConsoleReportPrinter ()
177                         : this (Console.Out)
178                 {
179                 }
180
181                 public ConsoleReportPrinter (TextWriter writer)
182                 {
183                         this.writer = writer;
184
185                         string term = Environment.GetEnvironmentVariable ("TERM");
186                         bool xterm_colors = false;
187
188                         color_supported = false;
189                         switch (term){
190                         case "xterm":
191                         case "rxvt":
192                         case "rxvt-unicode":
193                                 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
194                                         xterm_colors = true;
195                                 }
196                                 break;
197
198                         case "xterm-color":
199                         case "xterm-256color":
200                                 xterm_colors = true;
201                                 break;
202                         }
203                         if (!xterm_colors)
204                                 return;
205
206                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
207                                 return;
208
209                         color_supported = true;
210                         PopulateColorPrefixes ();
211                         postfix = "\x001b[0m";
212                 }
213
214                 void PopulateColorPrefixes ()
215                 {
216                         colorPrefixes = new string [16];
217
218                         colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
219                         colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
220                         colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
221                         colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
222                         colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
223                         colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
224                         colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
225                         colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
226
227                         colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
228                         colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
229                         colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
230                         colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
231                         colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
232                         colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
233                         colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
234
235                         colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
236                 }
237
238                 public void SetForeground (ConsoleColor color)
239                 {
240                         if (color_supported)
241                                 prefix = colorPrefixes [(int)color];
242                 }
243
244                 public void ResetColor ()
245                 {
246                         prefix = "\x001b[0m";
247                 }
248
249                 static int NameToCode (string s)
250                 {
251                         switch (s) {
252                         case "black":
253                                 return 0;
254                         case "red":
255                                 return 1;
256                         case "green":
257                                 return 2;
258                         case "yellow":
259                                 return 3;
260                         case "blue":
261                                 return 4;
262                         case "magenta":
263                                 return 5;
264                         case "cyan":
265                                 return 6;
266                         case "grey":
267                         case "white":
268                                 return 7;
269                         }
270                         return 7;
271                 }
272
273                 //
274                 // maps a color name to its xterm color code
275                 //
276                 static string GetForeground (string s)
277                 {
278                         string highcode;
279
280                         if (s.StartsWith ("bright")) {
281                                 highcode = "1;";
282                                 s = s.Substring (6);
283                         } else
284                                 highcode = "";
285
286                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
287                 }
288
289                 static string GetBackground (string s)
290                 {
291                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
292                 }
293
294                 string FormatText (string txt)
295                 {
296                         if (prefix != null && color_supported)
297                                 return prefix + txt + postfix;
298
299                         return txt;
300                 }
301
302                 public void Print (string message)
303                 {
304                         writer.WriteLine (FormatText (message));
305                 }
306
307         }
308
309         class UnixUtils {
310                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
311                 extern static int _isatty (int fd);
312
313                 public static bool isatty (int fd)
314                 {
315                         try {
316                                 return _isatty (fd) == 1;
317                         } catch {
318                                 return false;
319                         }
320                 }
321         }
322
323 }
324
325 #endif