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;
48 LoggerVerbosity loggerVerbosity;
52 BuildPropertyGroup properties;
53 IList remainingArguments;
54 Hashtable responseFiles;
57 string validationSchema;
64 consoleLoggerParameters = "";
66 loggers = new ArrayList ();
67 loggerVerbosity = LoggerVerbosity.Normal;
68 noConsoleLogger = false;
70 properties = new BuildPropertyGroup ();
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 foreach (string s in args) {
85 if (s.StartsWith ("/noautoresponse") || s.StartsWith ("/noautorsp")) {
90 flatArguments.Add (s);
93 string responseFilename = Path.GetFullPath (UnquoteIfNeeded (s.Substring (1)));
94 if (responseFiles.ContainsKey (responseFilename))
95 ReportError (1, String.Format ("We already have {0} file.", responseFilename));
96 responseFiles [responseFilename] = responseFilename;
97 LoadResponseFile (responseFilename);
99 if (autoResponse == true) {
100 // FIXME: we do not allow nested auto response file
101 LoadResponseFile (responseFile);
103 foreach (string s in flatArguments) {
104 if (s [0] != '/' || !ParseFlatArgument (s))
105 remainingArguments.Add (s);
107 if (remainingArguments.Count == 0) {
108 string[] sln_files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*.sln");
109 string[] proj_files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*proj");
111 if (sln_files.Length == 0 && proj_files.Length == 0)
112 ReportError (3, "Please specify the project or solution file " +
113 "to build, as none was found in the current directory.");
115 if (sln_files.Length == 1 && proj_files.Length > 0) {
116 var projects_table = new Dictionary<string, string> ();
117 foreach (string pfile in SolutionParser.GetAllProjectFileNames (sln_files [0])) {
118 string full_path = Path.GetFullPath (pfile);
119 projects_table [full_path] = full_path;
122 if (!proj_files.Any (p => !projects_table.ContainsKey (Path.GetFullPath (p))))
123 // if all the project files in the cur dir, are referenced
124 // from the single .sln in the cur dir, then pick the sln
125 proj_files = new string [0];
128 if (sln_files.Length + proj_files.Length > 1)
129 ReportError (5, "Please specify the project or solution file " +
130 "to build, as more than one solution or project file was found " +
131 "in the current directory");
133 if (sln_files.Length == 1)
134 projectFile = sln_files [0];
136 projectFile = proj_files [0];
137 } else if (remainingArguments.Count == 1) {
138 projectFile = (string) remainingArguments [0];
140 ReportError (4, "Too many project files specified");
144 private string UnquoteIfNeeded(string arg)
146 if (arg.StartsWith("\""))
147 return arg.Substring(1, arg.Length - 2);
151 void LoadResponseFile (string filename)
153 StreamReader sr = null;
156 sr = new StreamReader (filename);
157 StringBuilder sb = new StringBuilder ();
159 while ((line = sr.ReadLine ()) != null) {
162 for (int i = 0; i < t; i++) {
166 // comment, ignore rest of the line
169 if (c == '"' || c == '\'') {
172 for (i++; i < t; i++) {
179 } else if (c == ' ') {
181 flatArguments.Add (sb.ToString ());
188 flatArguments.Add (sb.ToString ());
192 } catch (IOException x) {
193 ErrorUtilities.ReportWarning (2, String.Format (
194 "Error loading response file. (Exception: {0}). Ignoring.",
202 private bool ParseFlatArgument (string s)
208 ErrorUtilities.ShowUsage ();
215 ErrorUtilities.ShowVersion (true);
217 case "/noconsolelogger":
219 noConsoleLogger = true;
226 if (s.StartsWith ("/target:") || s.StartsWith ("/t:")) {
228 } else if (s.StartsWith ("/property:") || s.StartsWith ("/p:")) {
229 if (!ProcessProperty (s))
231 } else if (s.StartsWith ("/logger:") || s.StartsWith ("/l:")) {
233 } else if (s.StartsWith ("/verbosity:") || s.StartsWith ("/v:")) {
234 ProcessVerbosity (s);
235 } else if (s.StartsWith ("/consoleloggerparameters:") || s.StartsWith ("/clp:")) {
236 ProcessConsoleLoggerParameters (s);
237 } else if (s.StartsWith ("/validate:") || s.StartsWith ("/val:")) {
239 } else if (s.StartsWith ("/toolsversion:") || s.StartsWith ("/tv:")) {
240 ToolsVersion = s.Split (':') [1];
249 internal void ProcessTarget (string s)
251 TryProcessMultiOption (s, "Target names must be specified as /t:Target1;Target2",
255 internal bool ProcessProperty (string s)
257 string[] splitProperties;
258 if (!TryProcessMultiOption (s, "Property name and value expected as /p:<prop name>=<prop value>",
259 out splitProperties))
262 foreach (string st in splitProperties) {
263 if (st.IndexOf ('=') < 0) {
265 "Invalid syntax. Property name and value expected as " +
266 "<prop name>=[<prop value>]");
269 string [] property = st.Split ('=');
270 properties.SetProperty (property [0], property.Length == 2 ? property [1] : "");
276 bool TryProcessMultiOption (string s, string error_message, out string[] values)
279 int colon = s.IndexOf (':');
280 if (colon + 1 == s.Length) {
281 ReportError (5, error_message);
285 values = s.Substring (colon + 1).Split (';');
289 private void ReportError (int errorCode, string message)
291 throw new CommandLineException (message, errorCode);
294 private void ReportError (int errorCode, string message, Exception cause)
296 throw new CommandLineException (message, cause, errorCode);
299 internal void ProcessLogger (string s)
301 loggers.Add (new LoggerInfo (s));
304 internal void ProcessVerbosity (string s)
306 string[] temp = s.Split (':');
310 loggerVerbosity = LoggerVerbosity.Quiet;
314 loggerVerbosity = LoggerVerbosity.Minimal;
318 loggerVerbosity = LoggerVerbosity.Normal;
322 loggerVerbosity = LoggerVerbosity.Detailed;
326 loggerVerbosity = LoggerVerbosity.Diagnostic;
331 internal void ProcessConsoleLoggerParameters (string s)
333 int colon = s.IndexOf (':');
334 if (colon + 1 == s.Length)
335 ReportError (5, "Invalid syntax, specify parameters as /clp:parameters");
337 consoleLoggerParameters = s.Substring (colon + 1);
340 internal void ProcessValidate (string s)
344 temp = s.Split (':');
345 validationSchema = temp [1];
347 public bool DisplayHelp {
348 get { return displayHelp; }
352 get { return noLogo; }
355 public string ProjectFile {
356 get { return projectFile; }
359 public string[] Targets {
360 get { return targets; }
363 public BuildPropertyGroup Properties {
364 get { return properties; }
367 public IList Loggers {
368 get { return loggers; }
371 public LoggerVerbosity LoggerVerbosity {
372 get { return loggerVerbosity; }
375 public string ConsoleLoggerParameters {
376 get { return consoleLoggerParameters; }
379 public bool NoConsoleLogger {
380 get { return noConsoleLogger; }
383 public bool Validate {
384 get { return validate; }
387 public string ValidationSchema {
388 get { return validationSchema; }
391 public string ToolsVersion {
392 get { return toolsVersion; }
393 private set { toolsVersion = value; }