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.
27 using Microsoft.Build.Logging;
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;
47 IList<LoggerInfo> loggerInfos;
48 LoggerVerbosity loggerVerbosity;
52 Dictionary<string,string> properties;
53 IList remainingArguments;
54 Hashtable responseFiles;
57 string validationSchema;
64 consoleLoggerParameters = "";
66 loggerInfos = new List<LoggerInfo> ();
67 loggerVerbosity = LoggerVerbosity.Normal;
68 noConsoleLogger = false;
70 properties = new Dictionary<string,string> ();
71 targets = new string [0];
73 responseFile = Path.Combine (
74 Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location),
78 public void ParseArguments (string[] args)
80 bool autoResponse = true;
81 flatArguments = new ArrayList ();
82 remainingArguments = new ArrayList ();
83 responseFiles = new Hashtable ();
84 FileLoggerParameters = new string[10];
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 (IOException x) {
194 ErrorUtilities.ReportWarning (2, String.Format (
195 "Error loading response file. (Exception: {0}). Ignoring.",
203 private bool ParseFlatArgument (string s)
209 ErrorUtilities.ShowUsage ();
216 ErrorUtilities.ShowVersion (true);
218 case "/noconsolelogger":
220 noConsoleLogger = true;
228 if (FileLoggerParameters [0] == null)
229 FileLoggerParameters [0] = String.Empty;
232 if (s.StartsWith ("/fl") && s.Length == 4 && Char.IsDigit (s[3])) {
233 int index = Int32.Parse (s[3].ToString ());
234 if (FileLoggerParameters [index] == null)
235 FileLoggerParameters [index] = String.Empty;
236 } else if (s.StartsWith ("/fileloggerparameters") || s.StartsWith ("/flp")) {
237 ProcessFileLoggerParameters (s);
238 } else if (s.StartsWith ("/target:") || s.StartsWith ("/t:")) {
240 } else if (s.StartsWith ("/property:") || s.StartsWith ("/p:")) {
241 if (!ProcessProperty (s))
243 } else if (s.StartsWith ("/logger:") || s.StartsWith ("/l:")) {
245 } else if (s.StartsWith ("/verbosity:") || s.StartsWith ("/v:")) {
246 ProcessVerbosity (s);
247 } else if (s.StartsWith ("/consoleloggerparameters:") || s.StartsWith ("/clp:")) {
248 ProcessConsoleLoggerParameters (s);
249 } else if (s.StartsWith ("/validate:") || s.StartsWith ("/val:")) {
251 } else if (s.StartsWith ("/toolsversion:") || s.StartsWith ("/tv:")) {
252 ToolsVersion = s.Split (':') [1];
261 internal void ProcessTarget (string s)
263 TryProcessMultiOption (s, "Target names must be specified as /t:Target1;Target2",
267 internal bool ProcessProperty (string s)
269 string[] splitProperties;
270 if (!TryProcessMultiOption (s, "Property name and value expected as /p:<prop name>=<prop value>",
271 out splitProperties))
274 foreach (string st in splitProperties) {
275 if (st.IndexOf ('=') < 0) {
277 "Invalid syntax. Property name and value expected as " +
278 "<prop name>=[<prop value>]");
281 string [] property = st.Split ('=');
282 properties.Add (property [0], property.Length == 2 ? property [1] : "");
288 bool TryProcessMultiOption (string s, string error_message, out string[] values)
291 int colon = s.IndexOf (':');
292 if (colon + 1 == s.Length) {
293 ReportError (5, error_message);
297 values = s.Substring (colon + 1).Split (';');
301 private void ReportError (int errorCode, string message)
303 throw new CommandLineException (message, errorCode);
306 private void ReportError (int errorCode, string message, Exception cause)
308 throw new CommandLineException (message, cause, errorCode);
311 internal void ProcessLogger (string s)
313 loggerInfos.Add (new LoggerInfo (s));
316 internal void ProcessVerbosity (string s)
318 string[] temp = s.Split (':');
322 loggerVerbosity = LoggerVerbosity.Quiet;
326 loggerVerbosity = LoggerVerbosity.Minimal;
330 loggerVerbosity = LoggerVerbosity.Normal;
334 loggerVerbosity = LoggerVerbosity.Detailed;
338 loggerVerbosity = LoggerVerbosity.Diagnostic;
343 void ProcessFileLoggerParameters (string s)
345 int colon = s.IndexOf (':');
346 if (colon + 1 == s.Length)
347 ReportError (5, "Invalid syntax, specify parameters as /fileloggerparameters[n]:parameters");
350 string key = s.Substring (0, colon);
351 if (Char.IsDigit (key [key.Length - 1]))
352 //if (key.Length == 22 && Char.IsDigit (key [21]))
353 index = Int32.Parse (key [key.Length - 1].ToString ());
355 FileLoggerParameters [index] = s.Substring (colon + 1);
358 internal void ProcessConsoleLoggerParameters (string s)
360 int colon = s.IndexOf (':');
361 if (colon + 1 == s.Length)
362 ReportError (5, "Invalid syntax, specify parameters as /clp:parameters");
364 consoleLoggerParameters = s.Substring (colon + 1);
367 internal void ProcessValidate (string s)
371 temp = s.Split (':');
372 validationSchema = temp [1];
374 public bool DisplayHelp {
375 get { return displayHelp; }
379 get { return noLogo; }
382 public string ProjectFile {
383 get { return projectFile; }
386 public string[] Targets {
387 get { return targets; }
390 public Dictionary<string,string> Properties {
391 get { return properties; }
394 IList<ILogger> loggers;
395 public IList<ILogger> Loggers {
398 loggers = loggerInfos.Select (li => new LoggerDescription (li.ClassName, li.AssemblyNameString, li.Filename, li.Parameters, LoggerVerbosity).CreateLogger ()).ToArray ();
403 public LoggerVerbosity LoggerVerbosity {
404 get { return loggerVerbosity; }
407 public string ConsoleLoggerParameters {
408 get { return consoleLoggerParameters; }
411 public bool NoConsoleLogger {
412 get { return noConsoleLogger; }
415 public string[] FileLoggerParameters { get; set; }
417 public bool Validate {
418 get { return validate; }
421 public string ValidationSchema {
422 get { return validationSchema; }
425 public string ToolsVersion {
426 get { return toolsVersion; }
427 private set { toolsVersion = value; }