New test.
[mono.git] / mcs / tools / xbuild / Parameters.cs
1 //
2 // Parameters.cs: Class that contains information about command line parameters
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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
28 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Collections;
33 using System.Text;
34 using Microsoft.Build.BuildEngine;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Mono.XBuild.CommandLine {
39         public class Parameters {
40         
41                 string                  consoleLoggerParameters;
42                 bool                    displayHelp;
43                 bool                    displayVersion;
44                 IList                   flatArguments;
45                 IList                   loggers;
46                 LoggerVerbosity         loggerVerbosity;
47                 bool                    noConsoleLogger;
48                 bool                    noLogo;
49                 string                  projectFile;
50                 BuildPropertyGroup      properties;
51                 IList                   remainingArguments;
52                 Hashtable               responseFiles;
53                 string[]                targets;
54                 bool                    validate;
55                 string                  validationSchema;
56                 
57                 string                  responseFile;
58         
59                 public Parameters (string binPath)
60                 {
61                         consoleLoggerParameters = "";
62                         displayHelp = false;
63                         displayVersion = true;
64                         loggers = new ArrayList ();
65                         loggerVerbosity = LoggerVerbosity.Normal;
66                         noConsoleLogger = false;
67                         noLogo = false;
68                         properties = new BuildPropertyGroup ();
69                         targets = new string [0];
70                         
71                         responseFile = Path.Combine (binPath, "xbuild.rsp");
72                 }
73                 
74                 public void ParseArguments (string[] args)
75                 {
76                         bool autoResponse = true;
77                         flatArguments = new ArrayList ();
78                         remainingArguments = new ArrayList ();
79                         responseFiles = new Hashtable ();
80                         foreach (string s in args) {
81                                 if (s.StartsWith ("/noautoresponse") || s.StartsWith ("/noautorsp")) {
82                                         autoResponse = false;
83                                         continue;
84                                 }
85                                 if (s [0] != '@') {
86                                         flatArguments.Add (s);
87                                         continue;
88                                 }
89                                 string responseFilename = Path.GetFullPath (s.Substring (1));
90                                 if (responseFiles.ContainsKey (responseFilename))
91                                         ErrorUtilities.ReportError (1, String.Format ("We already have {0} file.", responseFilename));
92                                 responseFiles [responseFilename] = responseFilename;
93                                 LoadResponseFile (responseFilename);
94                         }
95                         if (autoResponse == true) {
96                                 // FIXME: we do not allow nested auto response file
97                                 LoadResponseFile (responseFile);
98                         }
99                         foreach (string s in flatArguments) {
100                                 if (s [0] == '/') {
101                                         ParseFlatArgument (s);
102                                 } else
103                                         remainingArguments.Add (s);
104                         }
105                         if (remainingArguments.Count == 0) {
106                                 string[] files = Directory.GetFiles (Directory.GetCurrentDirectory (), "*.??proj");
107                                 if (files.Length > 0)
108                                         projectFile = files [0];
109                                 else
110                                         ErrorUtilities.ReportError (3, "No .proj file specified and no found in current directory.");
111                         } else if (remainingArguments.Count == 1) {
112                                 projectFile = (string) remainingArguments [0];
113                         } else {
114                                 ErrorUtilities.ReportError (4, "Too many project files specified");
115                         }
116                 }
117                 
118                 private void LoadResponseFile (string filename)
119                 {
120                         StreamReader sr = null;
121                         string line;
122                         try {
123                                 sr = new StreamReader (filename);
124                                 StringBuilder sb = new StringBuilder ();
125
126                                 while ((line = sr.ReadLine ()) != null) {
127                                         int t = line.Length;
128
129                                         for (int i = 0; i < t; i++) {
130                                                 char c = line [i];
131
132                                                 if (c == '"' || c == '\'') {
133                                                         char end = c;
134
135                                                         for (i++; i < t; i++) {
136                                                                 c = line [i];
137
138                                                                 if (c == end)
139                                                                         break;
140                                                                 sb.Append (c);
141                                                         }
142                                                 } else if (c == ' ') {
143                                                         if (sb.Length > 0) {
144                                                                 flatArguments.Add (sb.ToString ());
145                                                                 sb.Length = 0;
146                                                         }
147                                                 } else
148                                                         sb.Append (c);
149                                         }
150                                         if (sb.Length > 0){
151                                                 flatArguments.Add (sb.ToString ());
152                                                 sb.Length = 0;
153                                         }
154                                 }
155                         } catch (Exception) {
156                                 // FIXME: we lose exception message
157                                 ErrorUtilities.ReportError (2, "Error during loading response file.");
158                         } finally {
159                                 if (sr != null)
160                                         sr.Close ();
161                         }
162                 }
163                 
164                 private void ParseFlatArgument (string s)
165                 {
166                         switch (s) {
167                         case "/help":
168                         case "/h":
169                         case "/?":
170                                 ErrorUtilities.ShowUsage ();
171                                 break;
172                         case "/nologo":
173                                 noLogo = true;
174                                 break;
175                         case "/version":
176                         case "/ver":
177                                 ErrorUtilities.ShowVersion (true);
178                                 break;
179                         case "/noconsolelogger":
180                         case "/noconlog":
181                                 noConsoleLogger = true;
182                                 break;
183                         case "/validate":
184                         case "/val":
185                                 validate = true;
186                                 break;
187                         default:
188                                 if (s.StartsWith ("/target:") || s.StartsWith ("/t:")) {
189                                         ProcessTarget (s);
190                                 }
191                                 if (s.StartsWith ("/property:") || s.StartsWith ("/p:")) {
192                                         ProcessProperty (s);
193                                 }
194                                 if (s.StartsWith ("/logger:") || s.StartsWith ("/l:")) {
195                                         ProcessLogger (s);
196                                 }
197                                 if (s.StartsWith ("/verbosity:") || s.StartsWith ("/v:")) {
198                                         ProcessVerbosity (s);
199                                 }
200                                 if (s.StartsWith ("/consoleloggerparameters:") || s.StartsWith ("/clp:")) {
201                                         ProcessConsoleLoggerParameters (s);
202                                 }
203                                 if (s.StartsWith ("/validate:") || s.StartsWith ("/val:")) {
204                                         ProcessValidate (s);
205                                 }
206                                 break;
207                         }
208                 }
209                 
210                 internal void ProcessTarget (string s)
211                 {
212                         string[] temp = s.Split (':');
213                         targets = temp [1].Split (';');
214                 }
215                 
216                 internal void ProcessProperty (string s)
217                 {
218                         string[] parameter, splittedProperties, property;
219                         parameter = s.Split (':');
220                         splittedProperties = parameter [1].Split (';');
221                         foreach (string st in splittedProperties) {
222                                 property = st.Split ('=');
223                                 properties.AddNewProperty (property [0], property [1]);
224                         }
225                 }
226                 
227                 internal void ProcessLogger (string s)
228                 {
229                         loggers.Add (new LoggerInfo (s));
230                 }
231                 
232                 internal void ProcessVerbosity (string s)
233                 {
234                         string[] temp = s.Split (':');
235                         switch (temp [1]) {
236                         case "q":
237                         case "quiet":
238                                 loggerVerbosity = LoggerVerbosity.Quiet;
239                                 break;
240                         case "m":
241                         case "minimal":
242                                 loggerVerbosity = LoggerVerbosity.Minimal;
243                                 break;
244                         case "n":
245                         case "normal":
246                                 loggerVerbosity = LoggerVerbosity.Normal;
247                                 break;
248                         case "d":
249                         case "detailed":
250                                 loggerVerbosity = LoggerVerbosity.Detailed;
251                                 break;
252                         case "diag":
253                         case "diagnostic":
254                                 loggerVerbosity = LoggerVerbosity.Diagnostic;
255                                 break;
256                         }
257                 }
258                 
259                 internal void ProcessConsoleLoggerParameters (string s)
260                 {
261                         consoleLoggerParameters = s; 
262                 }
263                 
264                 internal void ProcessValidate (string s)
265                 {
266                         string[] temp;
267                         validate = true;
268                         temp = s.Split (':');
269                         validationSchema = temp [1];
270                 }
271                 public bool DisplayHelp {
272                         get { return displayHelp; }
273                 }
274                 
275                 public bool NoLogo {
276                         get { return noLogo; }
277                 }
278                 
279                 public bool DisplayVersion {
280                         get { return displayVersion; }
281                 }
282                 
283                 public string ProjectFile {
284                         get { return projectFile; }
285                 }
286                 
287                 public string[] Targets {
288                         get { return targets; }
289                 }
290                 
291                 public BuildPropertyGroup Properties {
292                         get { return properties; }
293                 }
294                 
295                 public IList Loggers {
296                         get { return loggers; }
297                 }
298                 
299                 public LoggerVerbosity LoggerVerbosity {
300                         get { return loggerVerbosity; }
301                 }
302                 
303                 public string ConsoleLoggerParameters {
304                         get { return consoleLoggerParameters; }
305                 }
306                 
307                 public bool NoConsoleLogger {
308                         get { return noConsoleLogger; }
309                 }
310                 
311                 public bool Validate {
312                         get { return validate; }
313                 }
314                 
315                 public string ValidationSchema {
316                         get { return validationSchema; }
317                 }
318                 
319         }
320 }
321
322 #endif