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;
33 using System.Collections;
34 using System.Collections.Generic;
37 using System.Reflection;
38 using Microsoft.Build.BuildEngine;
39 using Microsoft.Build.Framework;
40 using Microsoft.Build.Utilities;
42 namespace Mono.XBuild.CommandLine {
43 public class Parameters {
45 string consoleLoggerParameters;
48 IList<LoggerInfo> loggerInfos;
49 LoggerVerbosity loggerVerbosity;
53 Dictionary<string,string> properties;
54 IList remainingArguments;
55 Hashtable responseFiles;
58 string validationSchema;
65 consoleLoggerParameters = "";
67 loggerInfos = new List<LoggerInfo> ();
68 loggerVerbosity = LoggerVerbosity.Normal;
69 noConsoleLogger = false;
71 properties = new Dictionary<string,string> ();
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 FileLoggerParameters = new string[10];
86 foreach (string s in args) {
87 if (s.StartsWith ("/noautoresponse") || s.StartsWith ("/noautorsp")) {
92 flatArguments.Add (s);
95 string responseFilename = Path.GetFullPath (UnquoteIfNeeded (s.Substring (1)));
96 if (responseFiles.ContainsKey (responseFilename))
97 ReportError (1, String.Format ("We already have {0} file.", responseFilename));
98 responseFiles [responseFilename] = responseFilename;
99 LoadResponseFile (responseFilename);
101 if (autoResponse == true) {
102 // FIXME: we do not allow nested auto response file
103 LoadResponseFile (responseFile);
105 foreach (string s in flatArguments) {
106 if (s [0] != '/' || !ParseFlatArgument (s))
107 remainingArguments.Add (s);
109 if (remainingArguments.Count == 0) {
110 string[] sln_files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*.sln");
111 string[] proj_files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*proj");
113 if (sln_files.Length == 0 && proj_files.Length == 0)
114 ReportError (3, "Please specify the project or solution file " +
115 "to build, as none was found in the current directory.");
117 if (sln_files.Length == 1 && proj_files.Length > 0) {
118 var projects_table = new Dictionary<string, string> ();
119 foreach (string pfile in SolutionParser.GetAllProjectFileNames (sln_files [0])) {
120 string full_path = Path.GetFullPath (pfile);
121 projects_table [full_path] = full_path;
124 if (!proj_files.Any (p => !projects_table.ContainsKey (Path.GetFullPath (p))))
125 // if all the project files in the cur dir, are referenced
126 // from the single .sln in the cur dir, then pick the sln
127 proj_files = new string [0];
130 if (sln_files.Length + proj_files.Length > 1)
131 ReportError (5, "Please specify the project or solution file " +
132 "to build, as more than one solution or project file was found " +
133 "in the current directory");
135 if (sln_files.Length == 1)
136 projectFile = sln_files [0];
138 projectFile = proj_files [0];
139 } else if (remainingArguments.Count == 1) {
140 projectFile = (string) remainingArguments [0];
142 ReportError (4, "Too many project files specified");
146 private string UnquoteIfNeeded(string arg)
148 if (arg.StartsWith("\""))
149 return arg.Substring(1, arg.Length - 2);
153 void LoadResponseFile (string filename)
155 StreamReader sr = null;
158 sr = new StreamReader (filename);
159 StringBuilder sb = new StringBuilder ();
161 while ((line = sr.ReadLine ()) != null) {
164 for (int i = 0; i < t; i++) {
168 // comment, ignore rest of the line
171 if (c == '"' || c == '\'') {
174 for (i++; i < t; i++) {
181 } else if (c == ' ') {
183 flatArguments.Add (sb.ToString ());
190 flatArguments.Add (sb.ToString ());
194 } catch (IOException x) {
195 ErrorUtilities.ReportWarning (2, String.Format (
196 "Error loading response file. (Exception: {0}). Ignoring.",
204 private bool ParseFlatArgument (string s)
210 ErrorUtilities.ShowUsage ();
217 ErrorUtilities.ShowVersion (true);
219 case "/noconsolelogger":
221 noConsoleLogger = true;
229 if (FileLoggerParameters [0] == null)
230 FileLoggerParameters [0] = String.Empty;
233 if (s.StartsWith ("/fl") && s.Length == 4 && Char.IsDigit (s[3])) {
234 int index = Int32.Parse (s[3].ToString ());
235 if (FileLoggerParameters [index] == null)
236 FileLoggerParameters [index] = String.Empty;
237 } else if (s.StartsWith ("/fileloggerparameters") || s.StartsWith ("/flp")) {
238 ProcessFileLoggerParameters (s);
239 } else if (s.StartsWith ("/target:") || s.StartsWith ("/t:")) {
241 } else if (s.StartsWith ("/property:") || s.StartsWith ("/p:")) {
242 if (!ProcessProperty (s))
244 } else if (s.StartsWith ("/logger:") || s.StartsWith ("/l:")) {
246 } else if (s.StartsWith ("/verbosity:") || s.StartsWith ("/v:")) {
247 ProcessVerbosity (s);
248 } else if (s.StartsWith ("/consoleloggerparameters:") || s.StartsWith ("/clp:")) {
249 ProcessConsoleLoggerParameters (s);
250 } else if (s.StartsWith ("/validate:") || s.StartsWith ("/val:")) {
252 } else if (s.StartsWith ("/toolsversion:") || s.StartsWith ("/tv:")) {
253 ToolsVersion = s.Split (':') [1];
262 internal void ProcessTarget (string s)
264 TryProcessMultiOption (s, "Target names must be specified as /t:Target1;Target2",
268 internal bool ProcessProperty (string s)
270 string[] splitProperties;
271 if (!TryProcessMultiOption (s, "Property name and value expected as /p:<prop name>=<prop value>",
272 out splitProperties))
275 foreach (string st in splitProperties) {
276 if (st.IndexOf ('=') < 0) {
278 "Invalid syntax. Property name and value expected as " +
279 "<prop name>=[<prop value>]");
282 string [] property = st.Split ('=');
283 properties.Add (property [0], property.Length == 2 ? property [1] : "");
289 bool TryProcessMultiOption (string s, string error_message, out string[] values)
292 int colon = s.IndexOf (':');
293 if (colon + 1 == s.Length) {
294 ReportError (5, error_message);
298 values = s.Substring (colon + 1).Split (';');
302 private void ReportError (int errorCode, string message)
304 throw new CommandLineException (message, errorCode);
307 private void ReportError (int errorCode, string message, Exception cause)
309 throw new CommandLineException (message, cause, errorCode);
312 internal void ProcessLogger (string s)
314 loggerInfos.Add (new LoggerInfo (s));
317 internal void ProcessVerbosity (string s)
319 string[] temp = s.Split (':');
323 loggerVerbosity = LoggerVerbosity.Quiet;
327 loggerVerbosity = LoggerVerbosity.Minimal;
331 loggerVerbosity = LoggerVerbosity.Normal;
335 loggerVerbosity = LoggerVerbosity.Detailed;
339 loggerVerbosity = LoggerVerbosity.Diagnostic;
344 void ProcessFileLoggerParameters (string s)
346 int colon = s.IndexOf (':');
347 if (colon + 1 == s.Length)
348 ReportError (5, "Invalid syntax, specify parameters as /fileloggerparameters[n]:parameters");
351 string key = s.Substring (0, colon);
352 if (Char.IsDigit (key [key.Length - 1]))
353 //if (key.Length == 22 && Char.IsDigit (key [21]))
354 index = Int32.Parse (key [key.Length - 1].ToString ());
356 FileLoggerParameters [index] = s.Substring (colon + 1);
359 internal void ProcessConsoleLoggerParameters (string s)
361 int colon = s.IndexOf (':');
362 if (colon + 1 == s.Length)
363 ReportError (5, "Invalid syntax, specify parameters as /clp:parameters");
365 consoleLoggerParameters = s.Substring (colon + 1);
368 internal void ProcessValidate (string s)
372 temp = s.Split (':');
373 validationSchema = temp [1];
375 public bool DisplayHelp {
376 get { return displayHelp; }
380 get { return noLogo; }
383 public string ProjectFile {
384 get { return projectFile; }
387 public string[] Targets {
388 get { return targets; }
391 public Dictionary<string,string> Properties {
392 get { return properties; }
395 IList<ILogger> loggers;
396 public IList<ILogger> Loggers {
399 loggers = loggerInfos.Select (li => new LoggerDescription (li.ClassName, li.AssemblyNameString, li.Filename, li.Parameters, LoggerVerbosity).CreateLogger ()).ToArray ();
404 public LoggerVerbosity LoggerVerbosity {
405 get { return loggerVerbosity; }
408 public string ConsoleLoggerParameters {
409 get { return consoleLoggerParameters; }
412 public bool NoConsoleLogger {
413 get { return noConsoleLogger; }
416 public string[] FileLoggerParameters { get; set; }
418 public bool Validate {
419 get { return validate; }
422 public string ValidationSchema {
423 get { return validationSchema; }
426 public string ToolsVersion {
427 get { return toolsVersion; }
428 private set { toolsVersion = value; }