2 // Parameters.cs: Class that contains information about command line parameters
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.
32 using System.Collections;
33 using System.Collections.Generic;
36 using System.Reflection;
37 using Microsoft.Build.BuildEngine;
38 using Microsoft.Build.Framework;
39 using Microsoft.Build.Utilities;
41 namespace Mono.XBuild.CommandLine {
42 public class Parameters {
44 string consoleLoggerParameters;
49 LoggerVerbosity loggerVerbosity;
53 BuildPropertyGroup properties;
54 IList remainingArguments;
55 Hashtable responseFiles;
58 string validationSchema;
62 public Parameters (string binPath)
64 consoleLoggerParameters = "";
66 displayVersion = true;
67 loggers = new ArrayList ();
68 loggerVerbosity = LoggerVerbosity.Normal;
69 noConsoleLogger = false;
71 properties = new BuildPropertyGroup ();
72 targets = new string [0];
74 responseFile = Path.Combine (
75 Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location),
79 public void ParseArguments (string[] args)
81 bool autoResponse = true;
82 flatArguments = new ArrayList ();
83 remainingArguments = new ArrayList ();
84 responseFiles = new Hashtable ();
85 foreach (string s in args) {
86 if (s.StartsWith ("/noautoresponse") || s.StartsWith ("/noautorsp")) {
91 flatArguments.Add (s);
94 string responseFilename = Path.GetFullPath (UnquoteIfNeeded (s.Substring (1)));
95 if (responseFiles.ContainsKey (responseFilename))
96 ReportError (1, String.Format ("We already have {0} file.", responseFilename));
97 responseFiles [responseFilename] = responseFilename;
98 LoadResponseFile (responseFilename);
100 if (autoResponse == true) {
101 // FIXME: we do not allow nested auto response file
102 LoadResponseFile (responseFile);
104 foreach (string s in flatArguments) {
105 if (s [0] != '/' || !ParseFlatArgument (s))
106 remainingArguments.Add (s);
108 if (remainingArguments.Count == 0) {
109 string[] sln_files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*.sln");
110 string[] proj_files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*proj");
112 if (sln_files.Length == 0 && proj_files.Length == 0)
113 ReportError (3, "Please specify the project or solution file " +
114 "to build, as none was found in the current directory.");
116 if (sln_files.Length == 1 && proj_files.Length > 0) {
117 var projects_table = new Dictionary<string, string> ();
118 foreach (string pfile in SolutionParser.GetAllProjectFileNames (sln_files [0])) {
119 string full_path = Path.GetFullPath (pfile);
120 projects_table [full_path] = full_path;
123 if (!proj_files.Any (p => !projects_table.ContainsKey (Path.GetFullPath (p))))
124 // if all the project files in the cur dir, are referenced
125 // from the single .sln in the cur dir, then pick the sln
126 proj_files = new string [0];
129 if (sln_files.Length + proj_files.Length > 1)
130 ReportError (5, "Please specify the project or solution file " +
131 "to build, as more than one solution or project file was found " +
132 "in the current directory");
134 if (sln_files.Length == 1)
135 projectFile = sln_files [0];
137 projectFile = proj_files [0];
138 } else if (remainingArguments.Count == 1) {
139 projectFile = (string) remainingArguments [0];
141 ReportError (4, "Too many project files specified");
145 private string UnquoteIfNeeded(string arg)
147 if (arg.StartsWith("\""))
148 return arg.Substring(1, arg.Length - 2);
152 void LoadResponseFile (string filename)
154 StreamReader sr = null;
157 sr = new StreamReader (filename);
158 StringBuilder sb = new StringBuilder ();
160 while ((line = sr.ReadLine ()) != null) {
163 for (int i = 0; i < t; i++) {
167 // comment, ignore rest of the line
170 if (c == '"' || c == '\'') {
173 for (i++; i < t; i++) {
180 } else if (c == ' ') {
182 flatArguments.Add (sb.ToString ());
189 flatArguments.Add (sb.ToString ());
193 } catch (Exception x) {
194 ReportError (2, "Error during loading response file.", x);
201 private bool ParseFlatArgument (string s)
207 ErrorUtilities.ShowUsage ();
214 ErrorUtilities.ShowVersion (true);
216 case "/noconsolelogger":
218 noConsoleLogger = true;
225 if (s.StartsWith ("/target:") || s.StartsWith ("/t:")) {
227 } else if (s.StartsWith ("/property:") || s.StartsWith ("/p:")) {
228 if (!ProcessProperty (s))
230 } else if (s.StartsWith ("/logger:") || s.StartsWith ("/l:")) {
232 } else if (s.StartsWith ("/verbosity:") || s.StartsWith ("/v:")) {
233 ProcessVerbosity (s);
234 } else if (s.StartsWith ("/consoleloggerparameters:") || s.StartsWith ("/clp:")) {
235 ProcessConsoleLoggerParameters (s);
236 } else if (s.StartsWith ("/validate:") || s.StartsWith ("/val:")) {
246 internal void ProcessTarget (string s)
248 TryProcessMultiOption (s, "Target names must be specified as /t:Target1;Target2",
252 internal bool ProcessProperty (string s)
254 string[] splitProperties;
255 if (!TryProcessMultiOption (s, "Property name and value expected as /p:<prop name>=<prop value>",
256 out splitProperties))
259 foreach (string st in splitProperties) {
260 if (st.IndexOf ('=') < 0) {
262 "Invalid syntax. Property name and value expected as " +
263 "<prop name>=[<prop value>]");
266 string [] property = st.Split ('=');
267 properties.SetProperty (property [0], property.Length == 2 ? property [1] : "");
273 bool TryProcessMultiOption (string s, string error_message, out string[] values)
276 int colon = s.IndexOf (':');
277 if (colon + 1 == s.Length) {
278 ReportError (5, error_message);
282 values = s.Substring (colon + 1).Split (';');
286 private void ReportError (int errorCode, string message)
288 throw new CommandLineException (message, errorCode);
291 private void ReportError (int errorCode, string message, Exception cause)
293 throw new CommandLineException (message, cause, errorCode);
296 internal void ProcessLogger (string s)
298 loggers.Add (new LoggerInfo (s));
301 internal void ProcessVerbosity (string s)
303 string[] temp = s.Split (':');
307 loggerVerbosity = LoggerVerbosity.Quiet;
311 loggerVerbosity = LoggerVerbosity.Minimal;
315 loggerVerbosity = LoggerVerbosity.Normal;
319 loggerVerbosity = LoggerVerbosity.Detailed;
323 loggerVerbosity = LoggerVerbosity.Diagnostic;
328 internal void ProcessConsoleLoggerParameters (string s)
330 consoleLoggerParameters = s;
333 internal void ProcessValidate (string s)
337 temp = s.Split (':');
338 validationSchema = temp [1];
340 public bool DisplayHelp {
341 get { return displayHelp; }
345 get { return noLogo; }
348 public bool DisplayVersion {
349 get { return displayVersion; }
352 public string ProjectFile {
353 get { return projectFile; }
356 public string[] Targets {
357 get { return targets; }
360 public BuildPropertyGroup Properties {
361 get { return properties; }
364 public IList Loggers {
365 get { return loggers; }
368 public LoggerVerbosity LoggerVerbosity {
369 get { return loggerVerbosity; }
372 public string ConsoleLoggerParameters {
373 get { return consoleLoggerParameters; }
376 public bool NoConsoleLogger {
377 get { return noConsoleLogger; }
380 public bool Validate {
381 get { return validate; }
384 public string ValidationSchema {
385 get { return validationSchema; }