-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathParser.cs
345 lines (306 loc) · 13.6 KB
/
Parser.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.CommandLine;
using System.CommandLine.Completions;
using System.CommandLine.Help;
using System.Reflection;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Format;
using Microsoft.DotNet.Tools.Help;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.NuGet;
using Microsoft.TemplateEngine.Cli;
namespace Microsoft.DotNet.Cli
{
public static class Parser
{
public static readonly CliRootCommand RootCommand = new()
{
Directives = { new DiagramDirective(), new SuggestDirective() }
};
internal static Dictionary<CliOption, Dictionary<CliCommand, string>> HelpDescriptionCustomizations = new();
public static readonly CliCommand InstallSuccessCommand = InternalReportinstallsuccessCommandParser.GetCommand();
// Subcommands
public static readonly CliCommand[] Subcommands = new CliCommand[]
{
AddCommandParser.GetCommand(),
BuildCommandParser.GetCommand(),
BuildServerCommandParser.GetCommand(),
CleanCommandParser.GetCommand(),
FormatCommandParser.GetCommand(),
CompleteCommandParser.GetCommand(),
FsiCommandParser.GetCommand(),
ListCommandParser.GetCommand(),
MSBuildCommandParser.GetCommand(),
NewCommandParser.GetCommand(),
NuGetCommandParser.GetCommand(),
PackCommandParser.GetCommand(),
ParseCommandParser.GetCommand(),
PublishCommandParser.GetCommand(),
RemoveCommandParser.GetCommand(),
RestoreCommandParser.GetCommand(),
RunCommandParser.GetCommand(),
SlnCommandParser.GetCommand(),
StoreCommandParser.GetCommand(),
TestCommandParser.GetCommand(),
ToolCommandParser.GetCommand(),
VSTestCommandParser.GetCommand(),
HelpCommandParser.GetCommand(),
SdkCommandParser.GetCommand(),
InstallSuccessCommand,
WorkloadCommandParser.GetCommand()
};
public static readonly CliOption<bool> DiagOption = CommonOptionsFactory.CreateDiagnosticsOption(recursive: false);
public static readonly CliOption<bool> VersionOption = new("--version");
public static readonly CliOption<bool> InfoOption = new("--info");
public static readonly CliOption<bool> ListSdksOption = new("--list-sdks");
public static readonly CliOption<bool> ListRuntimesOption = new("--list-runtimes");
// Argument
public static readonly CliArgument<string> DotnetSubCommand = new("subcommand") { Arity = ArgumentArity.ZeroOrOne, Hidden = true };
private static CliCommand ConfigureCommandLine(CliCommand rootCommand)
{
for (int i = rootCommand.Options.Count - 1; i >= 0; i--)
{
CliOption option = rootCommand.Options[i];
if (option is VersionOption)
{
rootCommand.Options.RemoveAt(i);
}
else if (option is HelpOption helpOption)
{
helpOption.Action = new HelpAction()
{
Builder = DotnetHelpBuilder.Instance.Value
};
option.Description = CommandLineValidation.LocalizableStrings.ShowHelpInfo;
}
}
// Add subcommands
foreach (var subcommand in Subcommands)
{
rootCommand.Subcommands.Add(subcommand);
}
// Add options
rootCommand.Options.Add(DiagOption);
rootCommand.Options.Add(VersionOption);
rootCommand.Options.Add(InfoOption);
rootCommand.Options.Add(ListSdksOption);
rootCommand.Options.Add(ListRuntimesOption);
// Add argument
rootCommand.Arguments.Add(DotnetSubCommand);
rootCommand.SetAction(parseResult =>
{
if (parseResult.GetValue(DiagOption) && parseResult.Tokens.Count == 1)
{
// when user does not specify any args except of diagnostics ("dotnet -d"), we do nothing
// as Program.ProcessArgs already enabled the diagnostic output
return 0;
}
else
{
// when user does not specify any args (just "dotnet"), a usage needs to be printed
parseResult.Configuration.Output.WriteLine(HelpUsageText.UsageText);
return 0;
}
});
return rootCommand;
}
public static CliCommand GetBuiltInCommand(string commandName)
{
return Subcommands
.FirstOrDefault(c => c.Name.Equals(commandName, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Implements token-per-line response file handling for the CLI. We use this instead of the built-in S.CL handling
/// to ensure backwards-compatibility with MSBuild.
/// </summary>
public static bool TokenPerLine(string tokenToReplace, out IReadOnlyList<string> replacementTokens, out string errorMessage)
{
var filePath = Path.GetFullPath(tokenToReplace);
if (File.Exists(filePath))
{
var lines = File.ReadAllLines(filePath);
var trimmedLines =
lines
// Remove content in the lines that start with # after trimmer leading whitespace
.Select(line => line.TrimStart().StartsWith('#') ? string.Empty : line)
// trim leading/trailing whitespace to not pass along dead spaces
.Select(x => x.Trim())
// Remove empty lines
.Where(line => line.Length > 0);
replacementTokens = trimmedLines.ToArray();
errorMessage = null;
return true;
}
else
{
replacementTokens = null;
errorMessage = string.Format(CommonLocalizableStrings.ResponseFileNotFound, tokenToReplace);
return false;
}
}
public static CliConfiguration Instance { get; } = new(ConfigureCommandLine(RootCommand))
{
EnableDefaultExceptionHandler = false,
EnablePosixBundling = false,
ResponseFileTokenReplacer = TokenPerLine
};
internal static int ExceptionHandler(Exception exception, ParseResult parseResult)
{
if (exception is TargetInvocationException)
{
exception = exception.InnerException;
}
if (exception is Utils.GracefulException)
{
Reporter.Error.WriteLine(CommandLoggingContext.IsVerbose
? exception.ToString().Red().Bold()
: exception.Message.Red().Bold());
}
else if (exception is CommandParsingException)
{
Reporter.Error.WriteLine(CommandLoggingContext.IsVerbose
? exception.ToString().Red().Bold()
: exception.Message.Red().Bold());
parseResult.ShowHelp();
}
else
{
Reporter.Error.Write("Unhandled exception: ".Red().Bold());
Reporter.Error.WriteLine(exception.ToString().Red().Bold());
}
return 1;
}
internal class DotnetHelpBuilder : HelpBuilder
{
private DotnetHelpBuilder(int maxWidth = int.MaxValue) : base(maxWidth) { }
public static Lazy<HelpBuilder> Instance = new(() =>
{
int windowWidth;
try
{
windowWidth = Console.WindowWidth;
}
catch
{
windowWidth = int.MaxValue;
}
DotnetHelpBuilder dotnetHelpBuilder = new(windowWidth);
SetHelpCustomizations(dotnetHelpBuilder);
return dotnetHelpBuilder;
});
private static void SetHelpCustomizations(HelpBuilder builder)
{
foreach (var option in HelpDescriptionCustomizations.Keys)
{
Func<HelpContext, string> descriptionCallback = (HelpContext context) =>
{
foreach (var (command, helpText) in HelpDescriptionCustomizations[option])
{
if (context.ParseResult.CommandResult.Command.Equals(command))
{
return helpText;
}
}
return null;
};
builder.CustomizeSymbol(option, secondColumnText: descriptionCallback);
}
}
public void additionalOption(HelpContext context)
{
List<TwoColumnHelpRow> options = new();
HashSet<CliOption> uniqueOptions = new();
foreach (CliOption option in context.Command.Options)
{
if (!option.Hidden && uniqueOptions.Add(option))
{
options.Add(context.HelpBuilder.GetTwoColumnRow(option, context));
}
}
if (options.Count <= 0)
{
return;
}
context.Output.WriteLine(CommonLocalizableStrings.MSBuildAdditionalOptionTitle);
context.HelpBuilder.WriteColumns(options, context);
context.Output.WriteLine();
}
public override void Write(HelpContext context)
{
var command = context.Command;
var helpArgs = new string[] { "--help" };
if (command.Equals(RootCommand))
{
Console.Out.WriteLine(HelpUsageText.UsageText);
return;
}
foreach (var option in command.Options)
{
option.EnsureHelpName();
}
if (command.Equals(NuGetCommandParser.GetCommand()) || command.Parents.Any(parent => parent == NuGetCommandParser.GetCommand()))
{
NuGetCommand.Run(context.ParseResult);
}
else if (command.Name.Equals(MSBuildCommandParser.GetCommand().Name))
{
new MSBuildForwardingApp(helpArgs).Execute();
context.Output.WriteLine();
additionalOption(context);
}
else if (command.Name.Equals(VSTestCommandParser.GetCommand().Name))
{
new VSTestForwardingApp(helpArgs).Execute();
}
else if (command.Name.Equals(FormatCommandParser.GetCommand().Name))
{
var argumetns = context.ParseResult.GetValue(FormatCommandParser.Arguments);
new DotnetFormatForwardingApp(argumetns.Concat(helpArgs).ToArray()).Execute();
}
else if (command.Name.Equals(FsiCommandParser.GetCommand().Name))
{
new FsiForwardingApp(helpArgs).Execute();
}
else if (command is Microsoft.TemplateEngine.Cli.Commands.ICustomHelp helpCommand)
{
var blocks = helpCommand.CustomHelpLayout();
foreach (var block in blocks)
{
block(context);
}
}
else if (command.Name.Equals(FormatCommandParser.GetCommand().Name))
{
new DotnetFormatForwardingApp(helpArgs).Execute();
}
else if (command.Name.Equals(FsiCommandParser.GetCommand().Name))
{
new FsiForwardingApp(helpArgs).Execute();
}
else
{
if (command.Name.Equals(ListProjectToProjectReferencesCommandParser.GetCommand().Name))
{
CliCommand listCommand = command.Parents.Single() as CliCommand;
for (int i = 0; i < listCommand.Arguments.Count; i++)
{
if (listCommand.Arguments[i].Name == CommonLocalizableStrings.SolutionOrProjectArgumentName)
{
// Name is immutable now, so we create a new Argument with the right name..
listCommand.Arguments[i] = ListCommandParser.CreateSlnOrProjectArgument(CommonLocalizableStrings.ProjectArgumentName, CommonLocalizableStrings.ProjectArgumentDescription);
}
}
}
else if (command.Name.Equals(AddPackageParser.GetCommand().Name) || command.Name.Equals(AddCommandParser.GetCommand().Name))
{
// Don't show package completions in help
AddPackageParser.CmdPackageArgument.CompletionSources.Clear();
}
base.Write(context);
}
}
}
}
}