2 // Main.cs: Main program file of command line utility.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Marek Safar (marek.safar@seznam.cz)
9 // (C) 2005 Marek Sieradzki
10 // Copyright 2009 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System
.Collections
;
36 using System
.Reflection
;
37 using Microsoft
.Build
.BuildEngine
;
38 using Microsoft
.Build
.Framework
;
39 using Microsoft
.Build
.Utilities
;
40 using Mono
.XBuild
.Framework
;
42 namespace Mono
.XBuild
.CommandLine
{
43 public class MainClass
{
45 Parameters parameters
;
51 ConsoleReportPrinter printer
;
54 public static void Main (string[] args
)
56 MainClass mc
= new MainClass ();
63 string binPath
= ToolLocationHelper
.GetPathToDotNetFramework (TargetDotNetFrameworkVersion
.Version20
);
64 defaultSchema
= Path
.Combine (binPath
, "Microsoft.Build.xsd");
65 parameters
= new Parameters ();
68 public void Execute ()
71 bool show_stacktrace
= false;
74 parameters
.ParseArguments (args
);
75 show_stacktrace
= (parameters
.LoggerVerbosity
== LoggerVerbosity
.Detailed
||
76 parameters
.LoggerVerbosity
== LoggerVerbosity
.Diagnostic
);
78 if (parameters
.DisplayVersion
)
79 ErrorUtilities
.ShowVersion (false);
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
));
87 engine
.DefaultToolsVersion
= parameters
.ToolsVersion
;
90 engine
.GlobalProperties
= this.parameters
.Properties
;
92 if (!parameters
.NoConsoleLogger
) {
93 printer
= new ConsoleReportPrinter ();
94 ConsoleLogger cl
= new ConsoleLogger (parameters
.LoggerVerbosity
,
95 printer
.Print
, printer
.SetForeground
, printer
.ResetColor
);
97 cl
.Parameters
= parameters
.ConsoleLoggerParameters
;
98 cl
.Verbosity
= parameters
.LoggerVerbosity
;
99 engine
.RegisterLogger (cl
);
102 foreach (LoggerInfo li
in parameters
.Loggers
) {
104 if (li
.InfoType
== LoadInfoType
.AssemblyFilename
)
105 assembly
= Assembly
.LoadFrom (li
.Filename
);
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
);
113 project
= engine
.CreateNewProject ();
115 if (parameters
.Validate
) {
116 if (parameters
.ValidationSchema
== null)
117 project
.SchemaFile
= defaultSchema
;
119 project
.SchemaFile
= parameters
.ValidationSchema
;
122 string projectFile
= parameters
.ProjectFile
;
123 if (!File
.Exists (projectFile
)) {
124 ErrorUtilities
.ReportError (0, String
.Format ("Project file '{0}' not found.", projectFile
));
128 project
.Load (projectFile
);
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
);
138 catch (InvalidProjectFileException ipfe
) {
139 ErrorUtilities
.ReportError (0, show_stacktrace
? ipfe
.ToString () : ipfe
.Message
);
142 catch (InternalLoggerException ile
) {
143 ErrorUtilities
.ReportError (0, show_stacktrace
? ile
.ToString () : ile
.Message
);
146 catch (CommandLineException cle
) {
147 ErrorUtilities
.ReportError(cle
.ErrorCode
, show_stacktrace
? cle
.ToString() : cle
.Message
);
156 engine
.UnregisterAllLoggers ();
158 Environment
.Exit (result
? 0 : 1);
165 // code from mcs/report.cs
166 class ConsoleReportPrinter
168 string prefix
, postfix
;
169 bool color_supported
;
171 string [] colorPrefixes
;
173 public ConsoleReportPrinter ()
178 public ConsoleReportPrinter (TextWriter writer
)
180 this.writer
= writer
;
182 string term
= Environment
.GetEnvironmentVariable ("TERM");
183 bool xterm_colors
= false;
185 color_supported
= false;
190 if (Environment
.GetEnvironmentVariable ("COLORTERM") != null){
202 if (!(UnixUtils
.isatty (1) && UnixUtils
.isatty (2)))
205 color_supported
= true;
206 PopulateColorPrefixes ();
207 postfix
= "\x001b[0m";
210 void PopulateColorPrefixes ()
212 colorPrefixes
= new string [16];
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");
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");
231 colorPrefixes
[(int)ConsoleColor
.White
] = GetForeground ("brightwhite");
234 public void SetForeground (ConsoleColor color
)
237 prefix
= colorPrefixes
[(int)color
];
240 public void ResetColor ()
242 prefix
= "\x001b[0m";
245 static int NameToCode (string s
)
270 // maps a color name to its xterm color code
272 static string GetForeground (string s
)
276 if (s
.StartsWith ("bright")) {
282 return "\x001b[" + highcode
+ (30 + NameToCode (s
)).ToString () + "m";
285 static string GetBackground (string s
)
287 return "\x001b[" + (40 + NameToCode (s
)).ToString () + "m";
290 string FormatText (string txt
)
292 if (prefix
!= null && color_supported
)
293 return prefix
+ txt
+ postfix
;
298 public void Print (string message
)
300 writer
.WriteLine (FormatText (message
));
306 [System
.Runtime
.InteropServices
.DllImport ("libc", EntryPoint
="isatty")]
307 extern static int _isatty (int fd
);
309 public static bool isatty (int fd
)
312 return _isatty (fd
) == 1;