//
// Author:
// Marek Sieradzki (marek.sieradzki@gmail.com)
+// Miguel de Icaza (miguel@ximian.com)
+// Marek Safar (marek.safar@seznam.cz)
//
// (C) 2005 Marek Sieradzki
+// Copyright 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
using System.Collections;
using System.IO;
using System.Reflection;
+using System.Text;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.XBuild.Framework;
-using Mono.XBuild.Utilities;
namespace Mono.XBuild.CommandLine {
public class MainClass {
Parameters parameters;
string[] args;
- string binPath;
string defaultSchema;
Engine engine;
Project project;
+ ConsoleReportPrinter printer;
+
public static void Main (string[] args)
{
public MainClass ()
{
- binPath = MonoLocationHelper.GetXBuildDir ();
+ string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
- parameters = new Parameters (binPath);
+ parameters = new Parameters ();
}
-
+
public void Execute ()
{
+ bool result = false;
+ bool show_stacktrace = false;
+
try {
parameters.ParseArguments (args);
+ show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
+ parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
- if (parameters.DisplayVersion == true)
- Display (version);
+ if (!parameters.NoLogo)
+ ErrorUtilities.ShowVersion (false);
- engine = new Engine (binPath);
+ engine = Engine.GlobalEngine;
+ if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
+ if (engine.Toolsets [parameters.ToolsVersion] == null)
+ ErrorUtilities.ReportError (0, new UnknownToolsVersionException (parameters.ToolsVersion).Message);
+
+ engine.DefaultToolsVersion = parameters.ToolsVersion;
+ }
engine.GlobalProperties = this.parameters.Properties;
- if (parameters.NoConsoleLogger == false ) {
- ConsoleLogger cl = new ConsoleLogger ();
+ if (!parameters.NoConsoleLogger) {
+ printer = new ConsoleReportPrinter ();
+ ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
+ printer.Print, printer.SetForeground, printer.ResetColor);
+
cl.Parameters = parameters.ConsoleLoggerParameters;
cl.Verbosity = parameters.LoggerVerbosity;
engine.RegisterLogger (cl);
}
+
+ if (parameters.FileLoggerParameters != null) {
+ for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
+ string fl_params = parameters.FileLoggerParameters [i];
+ if (fl_params == null)
+ continue;
+
+ var fl = new FileLogger ();
+ if (fl_params.Length == 0 && i > 0)
+ fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
+ else
+ fl.Parameters = fl_params;
+ engine.RegisterLogger (fl);
+ }
+ }
foreach (LoggerInfo li in parameters.Loggers) {
Assembly assembly;
project = engine.CreateNewProject ();
- if (parameters.Validate == true) {
+ if (parameters.Validate) {
if (parameters.ValidationSchema == null)
project.SchemaFile = defaultSchema;
else
project.SchemaFile = parameters.ValidationSchema;
}
- project.LoadFromFile (parameters.ProjectFile);
-
- engine.BuildProject (project, parameters.Targets, new Hashtable ());
- }
- catch (CommandLineException cex) {
- switch (cex.ErrorCode) {
- case 1:
- ReportErrorFromException (cex);
- break;
- case 2:
- ReportErrorFromException (cex);
- break;
- case 3:
- if (parameters.NoLogo)
- ReportErrorFromException (cex);
- else {
- Display (version);
- Display (usage);
- }
- break;
- case 4:
- ReportErrorFromException (cex);
- break;
- case 5:
- Version ();
- break;
- case 6:
- Usage ();
- break;
- case 7:
- ReportErrorFromException (cex);
- break;
- default:
- throw;
+ string projectFile = parameters.ProjectFile;
+ if (!File.Exists (projectFile)) {
+ ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
+ return;
}
+
+ result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
}
- catch (InvalidProjectFileException ipfe ) {
- ReportError (0008, ipfe.Message);
+
+ catch (InvalidProjectFileException ipfe) {
+ ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
}
- catch (Exception ex) {
- ReportError (0, String.Format ("{0}\n{1}",ex.Message, ex.StackTrace));
+
+ catch (InternalLoggerException ile) {
+ ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
+ }
+
+ catch (CommandLineException cle) {
+ ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
}
finally {
if (engine != null)
engine.UnregisterAllLoggers ();
+
+ Environment.Exit (result ? 0 : 1);
}
+
}
-
- private void Display (string[] array) {
- foreach (string s in array)
- Console.WriteLine (s);
+ }
+
+ // code from mcs/report.cs
+ class ConsoleReportPrinter
+ {
+ string prefix, postfix;
+ bool color_supported;
+ TextWriter writer;
+ string [] colorPrefixes;
+
+ public ConsoleReportPrinter ()
+ : this (Console.Out)
+ {
}
-
- private void Version ()
+
+ public ConsoleReportPrinter (TextWriter writer)
{
- Display (version);
- Environment.Exit (0);
+ this.writer = writer;
+
+ string term = Environment.GetEnvironmentVariable ("TERM");
+ bool xterm_colors = false;
+
+ color_supported = false;
+ switch (term){
+ case "xterm":
+ case "rxvt":
+ case "rxvt-unicode":
+ if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
+ xterm_colors = true;
+ }
+ break;
+
+ case "xterm-color":
+ xterm_colors = true;
+ break;
+ }
+ if (!xterm_colors)
+ return;
+
+ if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
+ return;
+
+ color_supported = true;
+ PopulateColorPrefixes ();
+ postfix = "\x001b[0m";
}
-
- private void Usage ()
+
+ void PopulateColorPrefixes ()
{
- Display (version);
- Display (usage);
- Environment.Exit (0);
+ colorPrefixes = new string [16];
+
+ colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
+ colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
+ colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
+ colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
+ colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
+ colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
+ colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
+ colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
+
+ colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
+ colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
+ colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
+ colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
+ colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
+ colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
+ colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
+
+ colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
}
-
- private void ReportErrorFromException (CommandLineException cex)
+
+ public void SetForeground (ConsoleColor color)
{
- ReportError (cex.ErrorCode, cex.Message);
+ if (color_supported)
+ prefix = colorPrefixes [(int)color];
}
-
- private void ReportError (int errorNum, string msg) {
- Console.WriteLine (String.Format ("MSBUILD: error MSBUILD{0:0000}: {1}", errorNum, msg));
- Environment.Exit (1);
- }
-
- private void ReportWarning (int errorNum, string msg) {
- Console.WriteLine (String.Format ("MSBUILD: warning MSBUILD{0:0000}: {1}", errorNum, msg));
- }
-
- private void ReportInvalidArgument (string option, string value) {
- ReportError (1012, String.Format ("'{0}' is not a valid setting for option '{1}'", value, option));
- }
-
- private void ReportMissingArgument (string option) {
- ReportError (1003, String.Format ("Compiler option '{0}' must be followed by an argument", option));
- }
-
- private void ReportNotImplemented (string option) {
- ReportError (0, String.Format ("Compiler option '{0}' is not implemented", option));
- }
-
- private void ReportMissingFileSpec (string option) {
- ReportError (1008, String.Format ("Missing file specification for '{0}' command-line option", option));
- }
-
- private void ReportMissingText (string option) {
- ReportError (1010, String.Format ("Missing ':<text>' for '{0}' option", option));
- }
-
- string[] usage = {
- "",
- "Syntax: xbuild.exe [options] [project file]",
- "",
- "Description: Builds the specified targets in the project file. If",
- " a project file is not specified, MSBuild searches the",
- " current working directory for a file that has a file",
- " extension that ends in \"proj\" and uses that file.",
- "",
- "Switches:",
- "",
- " /help Display this usage message. (Short form: /? or /h)",
- "",
- " /nologo Do not display the startup banner and copyright message.",
- "",
- " /version Display version information only. (Short form: /ver)",
- "",
- " @<file> Insert command-line settings from a text file. To specify",
- " multiple response files, specify each response file",
- " separately.",
- "",
- " /noautoresponse Do not auto-include the MSBuild.rsp file. (Short form:",
- " /noautorsp)",
- "",
- " /target:<targets> Build these targets in this project. Use a semicolon or a",
- " comma to separate multiple targets, or specify each",
- " target separately. (Short form: /t)",
- " Example:",
- " /target:Resources;Compile",
- "",
- " /property:<n>=<v> Set or override these project-level properties. <n> is",
- " the property name, and <v> is the property value. Use a",
- " semicolon or a comma to separate multiple properties, or",
- " specify each property separately. (Short form: /p)",
- " Example:",
- @" /property:WarningLevel=2;OutDir=bin\Debug\",
- "",
- " /logger:<logger> Use this logger to log events from MSBuild. To specify",
- " multiple loggers, specify each logger separately.",
- " The <logger> syntax is:",
- " [<logger class>,]<logger assembly>[;<logger parameters>]",
- " The <logger class> syntax is:",
- " [<partial or full namespace>.]<logger class name>",
- " The <logger assembly> syntax is:",
- " {<assembly name>[,<strong name>] | <assembly file>}",
- " The <logger parameters> are optional, and are passed",
- " to the logger exactly as you typed them. (Short form: /l)",
- " Examples:",
- " /logger:XMLLogger,MyLogger,Version=1.0.2,Culture=neutral",
- @" /logger:XMLLogger,C:\Loggers\MyLogger.dll;OutputAsHTML",
- "",
- " /verbosity:<level> Display this amount of information in the event log.",
- " The available verbosity levels are: q[uiet], m[inimal],",
- " n[ormal], d[etailed], and diag[nostic]. (Short form: /v)",
- " Example:",
- " /verbosity:quiet",
- "",
- " /consoleloggerparameters:<parameters>",
- " Parameters to console logger. (Short form: /clp)",
- " The available parameters are:",
- " PerformanceSummary--show time spent in tasks, targets",
- " and projects.",
- " NoSummary--don't show error and warning summary at the",
- " end.",
- " Example:",
- " /consoleloggerparameters:PerformanceSummary;NoSummary",
- "",
- " /noconsolelogger Disable the default console logger and do not log events",
- " to the console. (Short form: /noconlog)",
- "",
- " /validate Validate the project against the default schema. (Short",
- " form: /val)",
- "",
- " /validate:<schema> Validate the project against the specified schema. (Short",
- " form: /val)",
- " Example:",
- " /validate:MyExtendedBuildSchema.xsd",
- "",
- "Examples:",
- "",
- " MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release",
- " MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug",
- };
-
- string[] version = {
- "XBuild Engine Version 0.1",
- String.Format ("Mono, Version {0}", Consts.MonoVersion),
- "Copyright (C) Marek Sieradzki 2005. All rights reserved.",
- };
+
+ public void ResetColor ()
+ {
+ prefix = "\x001b[0m";
+ }
+
+ static int NameToCode (string s)
+ {
+ switch (s) {
+ case "black":
+ return 0;
+ case "red":
+ return 1;
+ case "green":
+ return 2;
+ case "yellow":
+ return 3;
+ case "blue":
+ return 4;
+ case "magenta":
+ return 5;
+ case "cyan":
+ return 6;
+ case "grey":
+ case "white":
+ return 7;
+ }
+ return 7;
+ }
+
+ //
+ // maps a color name to its xterm color code
+ //
+ static string GetForeground (string s)
+ {
+ string highcode;
+
+ if (s.StartsWith ("bright")) {
+ highcode = "1;";
+ s = s.Substring (6);
+ } else
+ highcode = "";
+
+ return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
+ }
+
+ static string GetBackground (string s)
+ {
+ return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
+ }
+
+ string FormatText (string txt)
+ {
+ if (prefix != null && color_supported)
+ return prefix + txt + postfix;
+
+ return txt;
+ }
+
+ public void Print (string message)
+ {
+ writer.WriteLine (FormatText (message));
+ }
+
}
+
+ class UnixUtils {
+ [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
+ extern static int _isatty (int fd);
+
+ public static bool isatty (int fd)
+ {
+ try {
+ return _isatty (fd) == 1;
+ } catch {
+ return false;
+ }
+ }
+ }
+
}
-#endif
\ No newline at end of file
+#endif