Flush (work in progress)
[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 Microsoft.Build.BuildEngine;
38 using Microsoft.Build.Framework;
39 using Microsoft.Build.Utilities;
40 using Mono.XBuild.Framework;
41
42 namespace Mono.XBuild.CommandLine {
43         public class MainClass {
44                 
45                 Parameters      parameters;
46                 string[]        args;
47                 string          defaultSchema;
48                 
49                 Engine          engine;
50                 Project         project;
51                 ConsoleReportPrinter printer;
52
53                 
54                 public static void Main (string[] args)
55                 {
56                         MainClass mc = new MainClass ();
57                         mc.args = args;
58                         mc.Execute ();
59                 }
60                 
61                 public MainClass ()
62                 {
63                         string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
64                         defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
65                         parameters = new Parameters ();
66                 }
67
68                 public void Execute ()
69                 {
70                         bool result = false;
71                         bool show_stacktrace = false;
72                         
73                         try {
74                                 parameters.ParseArguments (args);
75                                 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
76                                         parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
77                                 
78                                 if (parameters.DisplayVersion)
79                                         ErrorUtilities.ShowVersion (false);
80                                 
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));
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                                 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); 
111                                 }
112                                 
113                                 project = engine.CreateNewProject ();
114                                 
115                                 if (parameters.Validate) {
116                                         if (parameters.ValidationSchema == null)
117                                                 project.SchemaFile = defaultSchema;
118                                         else
119                                                 project.SchemaFile = parameters.ValidationSchema;
120                                 }
121
122                                 string projectFile = parameters.ProjectFile;
123                                 if (!File.Exists (projectFile)) {
124                                         ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
125                                         return;
126                                 }
127
128                                 project.Load (projectFile);
129                                 
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);
136                         }
137                         
138                         catch (InvalidProjectFileException ipfe) {
139                                 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
140                         }
141
142                         catch (InternalLoggerException ile) {
143                                 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
144                         }
145
146                         catch (CommandLineException cle) {
147                                 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
148                         }
149
150                         catch (Exception) {
151                                 throw;
152                         }
153                         
154                         finally {
155                                 if (engine != null)
156                                         engine.UnregisterAllLoggers ();
157
158                                 Environment.Exit (result ? 0 : 1);
159                         }
160
161                 }
162
163         }
164
165         // code from mcs/report.cs
166         class ConsoleReportPrinter
167         {
168                 string prefix, postfix;
169                 bool color_supported;
170                 TextWriter writer;
171                 string [] colorPrefixes;
172
173                 public ConsoleReportPrinter ()
174                         : this (Console.Out)
175                 {
176                 }
177
178                 public ConsoleReportPrinter (TextWriter writer)
179                 {
180                         this.writer = writer;
181
182                         string term = Environment.GetEnvironmentVariable ("TERM");
183                         bool xterm_colors = false;
184
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;
192                                 }
193                                 break;
194
195                         case "xterm-color":
196                                 xterm_colors = true;
197                                 break;
198                         }
199                         if (!xterm_colors)
200                                 return;
201
202                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
203                                 return;
204
205                         color_supported = true;
206                         PopulateColorPrefixes ();
207                         postfix = "\x001b[0m";
208                 }
209
210                 void PopulateColorPrefixes ()
211                 {
212                         colorPrefixes = new string [16];
213
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");
222
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");
230
231                         colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
232                 }
233
234                 public void SetForeground (ConsoleColor color)
235                 {
236                         if (color_supported)
237                                 prefix = colorPrefixes [(int)color];
238                 }
239
240                 public void ResetColor ()
241                 {
242                         prefix = "\x001b[0m";
243                 }
244
245                 static int NameToCode (string s)
246                 {
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;
265                         }
266                         return 7;
267                 }
268
269                 //
270                 // maps a color name to its xterm color code
271                 //
272                 static string GetForeground (string s)
273                 {
274                         string highcode;
275
276                         if (s.StartsWith ("bright")) {
277                                 highcode = "1;";
278                                 s = s.Substring (6);
279                         } else
280                                 highcode = "";
281
282                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
283                 }
284
285                 static string GetBackground (string s)
286                 {
287                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
288                 }
289
290                 string FormatText (string txt)
291                 {
292                         if (prefix != null && color_supported)
293                                 return prefix + txt + postfix;
294
295                         return txt;
296                 }
297
298                 public void Print (string message)
299                 {
300                         writer.WriteLine (FormatText (message));
301                 }
302
303         }
304
305         class UnixUtils {
306                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
307                 extern static int _isatty (int fd);
308
309                 public static bool isatty (int fd)
310                 {
311                         try {
312                                 return _isatty (fd) == 1;
313                         } catch {
314                                 return false;
315                         }
316                 }
317         }
318
319 }
320
321 #endif