[xbuild] Add deprecation warning
[mono-project.git] / mcs / tools / xbuild / Main.cs
blobe34909337b00f2c6a4448f1d29fc5d8084d52af0
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).
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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;
43 namespace Mono.XBuild.CommandLine {
44 public class MainClass {
46 Parameters parameters;
47 string[] args;
48 string defaultSchema;
50 Engine engine;
51 Project project;
52 ConsoleReportPrinter printer;
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
59 public static void Main (string[] args)
61 Console.ForegroundColor = ConsoleColor.DarkRed;
62 Console.WriteLine ();
63 Console.WriteLine (">>>> xbuild tool is deprecated and will be removed in future updates, use msbuild instead <<<<");
64 Console.WriteLine ();
65 Console.ResetColor ();
68 MainClass mc = new MainClass ();
69 mc.args = args;
70 mc.Execute ();
73 public MainClass ()
75 string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
76 defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
77 parameters = new Parameters ();
80 public void Execute ()
82 bool result = false;
83 bool show_stacktrace = false;
85 try {
86 parameters.ParseArguments (args);
87 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
88 parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
90 if (!parameters.NoLogo)
91 ErrorUtilities.ShowVersion (false);
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);
98 engine.DefaultToolsVersion = parameters.ToolsVersion;
101 engine.GlobalProperties = this.parameters.Properties;
103 if (!parameters.NoConsoleLogger) {
104 printer = new ConsoleReportPrinter ();
105 ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
106 printer.Print, printer.SetForeground, printer.ResetColor);
108 cl.Parameters = parameters.ConsoleLoggerParameters;
109 cl.Verbosity = parameters.LoggerVerbosity;
110 engine.RegisterLogger (cl);
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;
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);
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);
139 project = engine.CreateNewProject ();
141 if (parameters.Validate) {
142 if (parameters.ValidationSchema == null)
143 project.SchemaFile = defaultSchema;
144 else
145 project.SchemaFile = parameters.ValidationSchema;
148 string projectFile = parameters.ProjectFile;
149 if (!File.Exists (projectFile)) {
150 ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
151 return;
154 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
157 catch (InvalidProjectFileException ipfe) {
158 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
161 catch (InternalLoggerException ile) {
162 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
165 catch (CommandLineException cle) {
166 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
168 finally {
169 if (engine != null)
170 engine.UnregisterAllLoggers ();
172 Environment.Exit (result ? 0 : 1);
178 // code from mcs/report.cs
179 class ConsoleReportPrinter
181 string prefix, postfix;
182 bool color_supported;
183 TextWriter writer;
184 string [] colorPrefixes;
186 public ConsoleReportPrinter ()
187 : this (Console.Out)
191 public ConsoleReportPrinter (TextWriter writer)
193 this.writer = writer;
195 string term = Environment.GetEnvironmentVariable ("TERM");
196 bool xterm_colors = false;
198 color_supported = false;
199 switch (term){
200 case "xterm":
201 case "rxvt":
202 case "rxvt-unicode":
203 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
204 xterm_colors = true;
206 break;
208 case "xterm-color":
209 case "xterm-256color":
210 xterm_colors = true;
211 break;
213 if (!xterm_colors)
214 return;
216 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
217 return;
219 color_supported = true;
220 PopulateColorPrefixes ();
221 postfix = "\x001b[0m";
224 void PopulateColorPrefixes ()
226 colorPrefixes = new string [16];
228 colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
229 colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
230 colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
231 colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
232 colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
233 colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
234 colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
235 colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
237 colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
238 colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
239 colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
240 colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
241 colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
242 colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
243 colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
245 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
248 public void SetForeground (ConsoleColor color)
250 if (color_supported)
251 prefix = colorPrefixes [(int)color];
254 public void ResetColor ()
256 prefix = "\x001b[0m";
259 static int NameToCode (string s)
261 switch (s) {
262 case "black":
263 return 0;
264 case "red":
265 return 1;
266 case "green":
267 return 2;
268 case "yellow":
269 return 3;
270 case "blue":
271 return 4;
272 case "magenta":
273 return 5;
274 case "cyan":
275 return 6;
276 case "grey":
277 case "white":
278 return 7;
280 return 7;
284 // maps a color name to its xterm color code
286 static string GetForeground (string s)
288 string highcode;
290 if (s.StartsWith ("bright")) {
291 highcode = "1;";
292 s = s.Substring (6);
293 } else
294 highcode = "";
296 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
299 static string GetBackground (string s)
301 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
304 string FormatText (string txt)
306 if (prefix != null && color_supported)
307 return prefix + txt + postfix;
309 return txt;
312 public void Print (string message)
314 writer.WriteLine (FormatText (message));
319 class UnixUtils {
320 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
321 extern static int _isatty (int fd);
323 public static bool isatty (int fd)
325 try {
326 return _isatty (fd) == 1;
327 } catch {
328 return false;