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