-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathCommandLineBuilderExtension.cs
309 lines (285 loc) · 11.6 KB
/
CommandLineBuilderExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Globalization;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
#nullable disable
namespace Microsoft.Build.Tasks
{
/// <summary>
/// CommandLineBuilder derived class for specialized logic specific to MSBuild tasks
/// </summary>
public class CommandLineBuilderExtension : CommandLineBuilder
{
/// <summary>
/// Initializes a new instance of the CommandLineBuilderExtension class.
/// </summary>
public CommandLineBuilderExtension()
{
}
/// <summary>
/// Initializes a new instance of the CommandLineBuilderExtension class.
/// </summary>
public CommandLineBuilderExtension(bool quoteHyphensOnCommandLine, bool useNewLineSeparator)
: base(quoteHyphensOnCommandLine, useNewLineSeparator)
{
}
/// <summary>
/// Set a boolean switch iff its value exists and its value is 'true'.
/// </summary>
internal void AppendWhenTrue(
string switchName,
Hashtable bag,
string parameterName)
{
object obj = bag[parameterName];
// If the switch isn't set, don't add it to the command line.
if (obj != null)
{
bool value = (bool)obj;
if (value)
{
AppendSwitch(switchName);
}
}
}
/// <summary>
/// Set a boolean switch only if its value exists.
/// </summary>
internal void AppendPlusOrMinusSwitch(
string switchName,
Hashtable bag,
string parameterName)
{
object obj = bag[parameterName];
// If the switch isn't set, don't add it to the command line.
if (obj != null)
{
bool value = (bool)obj;
// Do not quote - or + as they are part of the switch
AppendSwitchUnquotedIfNotNull(switchName, value ? "+" : "-");
}
}
/// <summary>
/// Set a boolean switch only if its value exists.
/// </summary>
internal void AppendPlusOrMinusSwitch(
string switchName,
bool? value)
{
if (value.HasValue)
{
AppendSwitchUnquotedIfNotNull(switchName, value.Value ? "+" : "-");
}
}
/// <summary>
/// Set a switch if its value exists by choosing from the input choices
/// </summary>
internal void AppendByChoiceSwitch(
string switchName,
Hashtable bag,
string parameterName,
string choice1,
string choice2)
{
object obj = bag[parameterName];
// If the switch isn't set, don't add it to the command line.
if (obj != null)
{
bool value = (bool)obj;
AppendSwitchUnquotedIfNotNull(switchName, value ? choice1 : choice2);
}
}
/// <summary>
/// Set an integer switch only if its value exists.
/// </summary>
internal void AppendSwitchWithInteger(
string switchName,
Hashtable bag,
string parameterName)
{
object obj = bag[parameterName];
// If the switch isn't set, don't add it to the command line.
if (obj != null)
{
int value = (int)obj;
AppendSwitchIfNotNull(switchName, value.ToString(CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Adds an aliased switch, used for ResGen:
/// /reference:Foo=System.Xml.dll
/// </summary>
internal void AppendSwitchAliased(string switchName, string alias, string parameter)
{
AppendSwitchUnquotedIfNotNull(switchName, alias + "=");
AppendTextWithQuoting(parameter);
}
/// <summary>
/// Adds a nested switch, used by SGen.exe. For example:
/// /compiler:"/keyfile:\"c:\some folder\myfile.snk\""
/// </summary>
/// <param name="outerSwitchName"></param>
/// <param name="innerSwitchName"></param>
/// <param name="parameter"></param>
internal void AppendNestedSwitch(string outerSwitchName, string innerSwitchName, string parameter)
{
string quotedParameter = GetQuotedText(parameter);
AppendSwitchIfNotNull(outerSwitchName, innerSwitchName + quotedParameter);
}
/// <summary>
/// Returns a quoted string appropriate for appending to a command line.
/// </summary>
/// <remarks>
/// Escapes any double quotes in the string.
/// </remarks>
/// <param name="unquotedText"></param>
protected string GetQuotedText(string unquotedText)
{
StringBuilder quotedText = new StringBuilder();
AppendQuotedTextToBuffer(quotedText, unquotedText);
return quotedText.ToString();
}
/// <summary>
/// Appends a command-line switch that takes a compound string parameter. The parameter is built up from the item-spec and
/// the specified attributes. The switch is appended as many times as there are parameters given.
/// </summary>
/// <param name="switchName"></param>
/// <param name="parameters"></param>
/// <param name="attributes"></param>
internal void AppendSwitchIfNotNull(
string switchName,
ITaskItem[] parameters,
string[] attributes)
{
AppendSwitchIfNotNull(switchName, parameters, attributes, null /* treatAsFlag */);
}
/// <summary>
/// Append a switch if 'parameter' is not null.
/// Split on the characters provided.
/// </summary>
internal void AppendSwitchWithSplitting(string switchName, string parameter, string delimiter, params char[] splitOn)
{
if (parameter != null)
{
string[] splits = parameter.Split(splitOn, /* omitEmptyEntries */ StringSplitOptions.RemoveEmptyEntries);
string[] splitAndTrimmed = new string[splits.Length];
for (int i = 0; i < splits.Length; ++i)
{
splitAndTrimmed[i] = splits[i].Trim();
}
AppendSwitchIfNotNull(switchName, splitAndTrimmed, delimiter);
}
}
/// <summary>
/// Returns true if the parameter is empty in spirits,
/// even if it contains the separators and white space only
/// Split on the characters provided.
/// </summary>
internal static bool IsParameterEmpty(string parameter, params char[] splitOn)
{
if (parameter != null)
{
string[] splits = parameter.Split(splitOn, /* omitEmptyEntries */ StringSplitOptions.RemoveEmptyEntries);
foreach (string s in splits)
{
if (!String.IsNullOrEmpty(s.Trim()))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Designed to handle the /link and /embed swithes:
///
/// /embed[resource]:<filename>[,<name>[,Private]]
/// /link[resource]:<filename>[,<name>[,Private]]
///
/// Where the last flag--Private--is either present or not present
/// depending on whether the ITaskItem has a Private="True" attribue.
/// </summary>
/// <param name="switchName"></param>
/// <param name="parameters"></param>
/// <param name="metadataNames"></param>
/// <param name="treatAsFlags"></param>
internal void AppendSwitchIfNotNull(
string switchName,
ITaskItem[] parameters,
string[] metadataNames,
bool[] treatAsFlags) // May be null. In this case no metadata are treated as flags.
{
ErrorUtilities.VerifyThrow(
treatAsFlags == null ||
(metadataNames != null && metadataNames.Length == treatAsFlags.Length),
"metadataNames and treatAsFlags should have the same length.");
if (parameters != null)
{
foreach (ITaskItem parameter in parameters)
{
AppendSwitchIfNotNull(switchName, parameter.ItemSpec);
if (metadataNames != null)
{
for (int i = 0; i < metadataNames.Length; ++i)
{
string metadataValue = parameter.GetMetadata(metadataNames[i]);
if (!string.IsNullOrEmpty(metadataValue))
{
// Treat attribute as a boolean flag?
if (treatAsFlags == null || !treatAsFlags[i])
{
// Not a boolean flag.
CommandLine.Append(',');
AppendTextWithQuoting(metadataValue);
}
else
{
// A boolean flag.
bool flagSet = MetadataConversionUtilities.TryConvertItemMetadataToBool(parameter, metadataNames[i]);
if (flagSet)
{
CommandLine.Append(',');
AppendTextWithQuoting(metadataNames[i]);
}
}
}
else
{
if (treatAsFlags == null || !treatAsFlags[i])
{
// If the caller of this method asked us to add metadata
// A, B, and C, and metadata A doesn't exist on the item,
// then it doesn't make sense to check for B and C. Because
// since these metadata are just being appended on the
// command-line switch with comma-separation, you can't pass
// in the B metadata unless you've also passed in the A
// metadata. Otherwise the tool's command-line parser will
// get totally confused.
// This only applies to non-flag attributes.
break;
}
}
}
}
}
}
}
/// <summary>
/// Appends a switch if the specified value is <code>true</code>.
/// </summary>
internal void AppendSwitchIfTrue(
string switchName,
bool? value)
{
if (value.HasValue && value.Value)
{
AppendSwitch(switchName);
}
}
}
}