2 // Main.cs: Main program file of command line utility.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
33 using System.Reflection;
34 using Microsoft.Build.BuildEngine;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37 using Mono.XBuild.Framework;
38 using Mono.XBuild.Utilities;
40 namespace Mono.XBuild.CommandLine {
41 public class MainClass {
43 Parameters parameters;
51 public static void Main (string[] args)
53 MainClass mc = new MainClass ();
60 binPath = MonoLocationHelper.GetXBuildDir ();
61 defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
62 parameters = new Parameters (binPath);
65 public void Execute ()
68 parameters.ParseArguments (args);
70 if (parameters.DisplayVersion == true)
73 engine = new Engine (binPath);
75 engine.GlobalProperties = this.parameters.Properties;
77 if (parameters.NoConsoleLogger == false ) {
78 ConsoleLogger cl = new ConsoleLogger ();
79 cl.Parameters = parameters.ConsoleLoggerParameters;
80 cl.Verbosity = parameters.LoggerVerbosity;
81 engine.RegisterLogger (cl);
84 foreach (LoggerInfo li in parameters.Loggers) {
86 if (li.InfoType == LoadInfoType.AssemblyFilename)
87 assembly = Assembly.LoadFrom (li.Filename);
89 assembly = Assembly.Load (li.AssemblyName);
90 ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
91 logger.Parameters = li.Parameters;
92 engine.RegisterLogger (logger);
95 project = engine.CreateNewProject ();
97 if (parameters.Validate == true) {
98 if (parameters.ValidationSchema == null)
99 project.SchemaFile = defaultSchema;
101 project.SchemaFile = parameters.ValidationSchema;
104 project.LoadFromFile (parameters.ProjectFile);
106 engine.BuildProject (project, parameters.Targets, new Hashtable ());
108 catch (CommandLineException cex) {
109 switch (cex.ErrorCode) {
111 ReportErrorFromException (cex);
114 ReportErrorFromException (cex);
117 if (parameters.NoLogo)
118 ReportErrorFromException (cex);
125 ReportErrorFromException (cex);
134 ReportErrorFromException (cex);
140 catch (InvalidProjectFileException ipfe ) {
141 ReportError (0008, ipfe.Message);
143 catch (Exception ex) {
144 ReportError (0, String.Format ("{0}\n{1}",ex.Message, ex.StackTrace));
148 engine.UnregisterAllLoggers ();
152 private void Display (string[] array) {
153 foreach (string s in array)
154 Console.WriteLine (s);
157 private void Version ()
160 Environment.Exit (0);
163 private void Usage ()
167 Environment.Exit (0);
170 private void ReportErrorFromException (CommandLineException cex)
172 ReportError (cex.ErrorCode, cex.Message);
175 private void ReportError (int errorNum, string msg) {
176 Console.WriteLine (String.Format ("MSBUILD: error MSBUILD{0:0000}: {1}", errorNum, msg));
177 Environment.Exit (1);
180 private void ReportWarning (int errorNum, string msg) {
181 Console.WriteLine (String.Format ("MSBUILD: warning MSBUILD{0:0000}: {1}", errorNum, msg));
184 private void ReportInvalidArgument (string option, string value) {
185 ReportError (1012, String.Format ("'{0}' is not a valid setting for option '{1}'", value, option));
188 private void ReportMissingArgument (string option) {
189 ReportError (1003, String.Format ("Compiler option '{0}' must be followed by an argument", option));
192 private void ReportNotImplemented (string option) {
193 ReportError (0, String.Format ("Compiler option '{0}' is not implemented", option));
196 private void ReportMissingFileSpec (string option) {
197 ReportError (1008, String.Format ("Missing file specification for '{0}' command-line option", option));
200 private void ReportMissingText (string option) {
201 ReportError (1010, String.Format ("Missing ':<text>' for '{0}' option", option));
206 "Syntax: xbuild.exe [options] [project file]",
208 "Description: Builds the specified targets in the project file. If",
209 " a project file is not specified, MSBuild searches the",
210 " current working directory for a file that has a file",
211 " extension that ends in \"proj\" and uses that file.",
215 " /help Display this usage message. (Short form: /? or /h)",
217 " /nologo Do not display the startup banner and copyright message.",
219 " /version Display version information only. (Short form: /ver)",
221 " @<file> Insert command-line settings from a text file. To specify",
222 " multiple response files, specify each response file",
225 " /noautoresponse Do not auto-include the MSBuild.rsp file. (Short form:",
228 " /target:<targets> Build these targets in this project. Use a semicolon or a",
229 " comma to separate multiple targets, or specify each",
230 " target separately. (Short form: /t)",
232 " /target:Resources;Compile",
234 " /property:<n>=<v> Set or override these project-level properties. <n> is",
235 " the property name, and <v> is the property value. Use a",
236 " semicolon or a comma to separate multiple properties, or",
237 " specify each property separately. (Short form: /p)",
239 @" /property:WarningLevel=2;OutDir=bin\Debug\",
241 " /logger:<logger> Use this logger to log events from MSBuild. To specify",
242 " multiple loggers, specify each logger separately.",
243 " The <logger> syntax is:",
244 " [<logger class>,]<logger assembly>[;<logger parameters>]",
245 " The <logger class> syntax is:",
246 " [<partial or full namespace>.]<logger class name>",
247 " The <logger assembly> syntax is:",
248 " {<assembly name>[,<strong name>] | <assembly file>}",
249 " The <logger parameters> are optional, and are passed",
250 " to the logger exactly as you typed them. (Short form: /l)",
252 " /logger:XMLLogger,MyLogger,Version=1.0.2,Culture=neutral",
253 @" /logger:XMLLogger,C:\Loggers\MyLogger.dll;OutputAsHTML",
255 " /verbosity:<level> Display this amount of information in the event log.",
256 " The available verbosity levels are: q[uiet], m[inimal],",
257 " n[ormal], d[etailed], and diag[nostic]. (Short form: /v)",
261 " /consoleloggerparameters:<parameters>",
262 " Parameters to console logger. (Short form: /clp)",
263 " The available parameters are:",
264 " PerformanceSummary--show time spent in tasks, targets",
266 " NoSummary--don't show error and warning summary at the",
269 " /consoleloggerparameters:PerformanceSummary;NoSummary",
271 " /noconsolelogger Disable the default console logger and do not log events",
272 " to the console. (Short form: /noconlog)",
274 " /validate Validate the project against the default schema. (Short",
277 " /validate:<schema> Validate the project against the specified schema. (Short",
280 " /validate:MyExtendedBuildSchema.xsd",
284 " MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release",
285 " MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug",
289 "XBuild Engine Version 0.1",
290 String.Format ("Mono, Version {0}", Consts.MonoVersion),
291 "Copyright (C) Marek Sieradzki 2005. All rights reserved.",