merge -r 60439:60440
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / CommandLineBuilder.cs
1 //
2 // CommandLineBuilder.cs: Builds command line options string
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.Text;
32 using Microsoft.Build.Framework;
33
34 namespace Microsoft.Build.Utilities
35 {
36         public class CommandLineBuilder
37         {
38                 StringBuilder commandLine;
39         
40                 public CommandLineBuilder ()
41                 {
42                         commandLine = new StringBuilder ();
43                 }
44                 
45                 public void AppendFileNameIfNotNull (string fileName)
46                 {
47                         if (fileName == null)
48                                 return;
49                         
50                         AppendSpaceIfNotEmpty ();
51                         commandLine.Append (fileName);
52                 }
53                 
54                 public void AppendFileNameIfNotNull (ITaskItem fileItem)
55                 {
56                         if (fileItem == null)
57                                 return;
58                         
59                         AppendSpaceIfNotEmpty ();
60                         commandLine.Append (fileItem.ToString());
61                 }
62                 
63                 public void AppendFileNamesIfNotNull (string[] fileNames,
64                                                       string delimiter)
65                 {
66                         if (delimiter == null)
67                                 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
68                 
69                         if (fileNames == null)
70                                 return;
71                         
72                         bool appendDelimiter = false;
73                         AppendSpaceIfNotEmpty ();
74                         for (int i = 0; i < fileNames.Length; i++) {
75                                 if (fileNames [i] == null)
76                                         continue;
77                                 if (appendDelimiter) {
78                                         commandLine.Append (delimiter);
79                                         commandLine.Append (fileNames [i]);
80                                 } else {
81                                         commandLine.Append (fileNames [i]);
82                                         appendDelimiter = true;
83                                 }
84                         }
85                 }
86                 
87                 public void AppendFileNamesIfNotNull (ITaskItem[] fileItems,
88                                                       string delimiter)
89                 {
90                         if (delimiter == null)
91                                 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
92                 
93                         if (fileItems == null)
94                                 return;
95                         
96                         bool appendDelimiter = false;
97                         AppendSpaceIfNotEmpty ();
98                         for (int i = 0; i < fileItems.Length; i++) {
99                                 if (fileItems [i] == null)
100                                         continue;
101                                 if (appendDelimiter) {
102                                         commandLine.Append (delimiter);
103                                         commandLine.Append (fileItems [i].ToString ());
104                                 } else {
105                                         commandLine.Append (fileItems [i].ToString ());
106                                         appendDelimiter = true;
107                                 }
108                         }
109                 }
110                 
111                 protected void AppendFileNameWithQuoting (string fileName)
112                 {
113                         if (IsQuotingRequired (fileName))
114                                 commandLine.AppendFormat ("\"{0}\"",fileName);
115                         else
116                                 commandLine.Append (fileName);
117                 }
118                 
119                 protected void AppendSpaceIfNotEmpty ()
120                 {
121                         if (commandLine.Length != 0)
122                                 commandLine.Append (' ');
123                 }
124                 
125                 public void AppendSwitch (string switchName)
126                 {
127                         if (switchName == null)
128                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
129
130                         AppendSpaceIfNotEmpty ();
131                         commandLine.Append (switchName);
132                 }
133                 
134                 public void AppendSwitchIfNotNull (string switchName,
135                                                    string parameter)
136                 {
137                         if (switchName == null)
138                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
139                 
140                         if (parameter == null)
141                                 return;
142                         
143                         AppendSpaceIfNotEmpty ();
144                         commandLine.AppendFormat ("{0}{1}",switchName,
145                                 parameter);
146                 }
147                 
148                 public void AppendSwitchIfNotNull (string switchName,
149                                                    ITaskItem parameter)
150                 {
151                         if (switchName == null)
152                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
153                 
154                         if (parameter == null)
155                                 return;
156                         
157                         AppendSpaceIfNotEmpty ();
158                         commandLine.AppendFormat ("{0}{1}",switchName,
159                                 parameter.ToString ());
160                 }
161                 
162                 public void AppendSwitchIfNotNull (string switchName,
163                                                    string[] parameters,
164                                                    string delimiter)
165                 {
166                         if (switchName == null)
167                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
168                 
169                         if (delimiter == null)
170                                 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
171
172                         if (parameters == null)
173                                 return;
174                         
175                         AppendSpaceIfNotEmpty ();
176                         commandLine.AppendFormat ("{0}",switchName);
177                         bool appendDelimiter = false;
178                         for (int i = 0; i < parameters.Length; i++) {
179                                 if (parameters [i] == null)
180                                         continue;
181                                 if (appendDelimiter) {
182                                         commandLine.Append (delimiter);
183                                         commandLine.Append (parameters [i]);
184                                 } else {
185                                         commandLine.Append (parameters [i]);
186                                         appendDelimiter = true;
187                                 }
188                         }
189                 }
190                 
191                 public void AppendSwitchIfNotNull (string switchName,
192                                                    ITaskItem[] parameters,
193                                                    string delimiter)
194                 {
195                         if (switchName == null)
196                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
197                 
198                         if (delimiter == null)
199                                 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
200
201                         if (parameters == null)
202                                 return;
203                         
204                         AppendSpaceIfNotEmpty ();
205                         commandLine.AppendFormat ("{0}",switchName);
206                         bool appendDelimiter = false;
207                         for (int i = 0; i < parameters.Length; i++) {
208                                 if (parameters [i] == null)
209                                         continue;
210                                 if (appendDelimiter) {
211                                         commandLine.Append (delimiter);
212                                         commandLine.Append (parameters [i].ToString ());
213                                 } else {
214                                         commandLine.Append (parameters [i].ToString ());
215                                         appendDelimiter = true;
216                                 }
217                         }
218                 }
219                 
220                 public void AppendSwitchUnquotedIfNotNull (string switchName,
221                                                            string parameter)
222                 {
223                         if (switchName == null)
224                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
225                 
226                         if (parameter == null)
227                                 return;
228                         
229                         AppendSpaceIfNotEmpty ();
230                         commandLine.AppendFormat ("{0}{1}", switchName, parameter);
231                 }
232
233                 public void AppendSwitchUnquotedIfNotNull (string switchName,
234                                                            ITaskItem parameter)
235                 {
236                         if (switchName == null)
237                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
238                 
239                         if (parameter == null)
240                                 return;
241                         
242                         AppendSpaceIfNotEmpty ();
243                         commandLine.AppendFormat ("{0}{1}", switchName, parameter.ItemSpec);
244                 }
245
246                 public void AppendSwitchUnquotedIfNotNull (string switchName,
247                                                            string[] parameters,
248                                                            string delimiter)
249                 {
250                         if (switchName == null)
251                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
252                 
253                         if (delimiter == null)
254                                 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
255
256                         if (parameters == null)
257                                 return;
258                         
259                         AppendSpaceIfNotEmpty ();
260                         commandLine.AppendFormat ("{0}",switchName);
261                         bool appendDelimiter = false;
262                         for (int i = 0; i < parameters.Length; i++) {
263                                 if (parameters [i] == null)
264                                         continue; 
265                                 if (appendDelimiter) {
266                                         commandLine.Append (delimiter);
267                                         commandLine.Append (parameters [i]);
268                                 } else {
269                                         commandLine.Append (parameters [i]);
270                                         appendDelimiter = true;
271                                 }
272                         }
273                 }
274
275                 public void AppendSwitchUnquotedIfNotNull (string switchName,
276                                                            ITaskItem[] parameters,
277                                                            string delimiter)
278                 {
279                         if (switchName == null)
280                                 throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
281                 
282                         if (delimiter == null)
283                                 throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
284
285                         if (parameters == null)
286                                 return;
287                         
288                         AppendSpaceIfNotEmpty ();
289                         commandLine.AppendFormat ("{0}",switchName);
290                         bool appendDelimiter = false;
291                         for (int i = 0; i < parameters.Length; i++) {
292                                 if (parameters [i] == null)
293                                         continue;
294                                 if (appendDelimiter) {
295                                         commandLine.Append (delimiter);
296                                         commandLine.Append (parameters [i].ToString ());
297                                 } else {
298                                         commandLine.Append (parameters [i].ToString ());
299                                         appendDelimiter = true;
300                                 }
301                         }
302                 }
303                 
304                 protected void AppendTextUnquoted (string textToAppend)
305                 {
306                         commandLine.Append (textToAppend);
307                 }
308                 
309                 protected void AppendTextWithQuoting (string textToAppend)
310                 {
311                         if (IsQuotingRequired (textToAppend))
312                                 commandLine.AppendFormat ("\"{0}\"",textToAppend);
313                         else
314                                 commandLine.Append (textToAppend);
315                 }
316                 
317                 protected virtual bool IsQuotingRequired (string parameter)
318                 {
319                         parameter.Trim ();
320                         // FIXME: change this to regex
321                         foreach (char c in parameter) {
322                                 if (c == ' ' || c == '\t' || c == '\u000b' || c == '\u000c')
323                                         return true;
324                         }
325                         return false;
326                 }
327                 
328                 [MonoTODO]
329                 protected virtual void VerifyThrowNoEmbeddedDoubleQuotes (string switchName,
330                                                                          string parameter)
331                 {
332                 }
333                 
334                 public override string ToString ()
335                 {
336                         return commandLine.ToString ();
337                 }
338                 
339                 protected StringBuilder CommandLine {
340                         get {
341                                 return commandLine;
342                         }
343                 }
344         }
345 }
346
347 #endif