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