Skip to content

Commit ce90906

Browse files
authored
Separate command parsing from implementation to reduce dotnet-watch dependencies (#51624)
1 parent f47e024 commit ce90906

File tree

132 files changed

+2422
-2119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2422
-2119
lines changed

src/Cli/Microsoft.DotNet.Cli.CommandLine/ForwardedOptionExtensions.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,16 @@ public Option<IEnumerable<string>> ForwardAsManyArgumentsEachPrefixedByOption(st
177177
/// <summary>
178178
/// Calls the forwarding functions for all options that have declared a forwarding function (via <see cref="ForwardedOptionExtensions"/>'s extension members) in the provided <see cref="ParseResult"/>.
179179
/// </summary>
180-
/// <param name="parseResult"></param>
181180
/// <param name="command">If not provided, uses the <see cref="ParseResult.CommandResult" />'s <see cref="CommandResult.Command"/>.</param>
182-
/// <returns></returns>
183-
public IEnumerable<string> OptionValuesToBeForwarded(Command? command = null) =>
184-
(command ?? parseResult.CommandResult.Command)
185-
.Options
181+
public IEnumerable<string> OptionValuesToBeForwarded(Command? command = null)
182+
=> parseResult.OptionValuesToBeForwarded((command ?? parseResult.CommandResult.Command).Options);
183+
184+
/// <summary>
185+
/// Calls the forwarding functions for all options that have declared a forwarding function (via <see cref="ForwardedOptionExtensions"/>'s extension members) in the provided <see cref="ParseResult"/>.
186+
/// </summary>
187+
/// <param name="command">If not provided, uses the <see cref="ParseResult.CommandResult" />'s <see cref="CommandResult.Command"/>.</param>
188+
public IEnumerable<string> OptionValuesToBeForwarded(IEnumerable<Option> options)
189+
=> options
186190
.Select(o => o.ForwardingFunction)
187191
.SelectMany(f => f is not null ? f(parseResult) : []);
188192

@@ -191,9 +195,6 @@ public IEnumerable<string> OptionValuesToBeForwarded(Command? command = null) =>
191195
/// invokes its forwarding function (if any) and returns the result. If no option with that name is found, or if the option
192196
/// has no forwarding function, returns an empty enumeration.
193197
/// </summary>
194-
/// <param name="command"></param>
195-
/// <param name="alias"></param>
196-
/// <returns></returns>
197198
public IEnumerable<string> ForwardedOptionValues(Command command, string alias)
198199
{
199200
var func = command.Options?

src/Cli/dotnet/CommandFactory/CommandResolution/ProjectToolsCommandResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ internal void GenerateDepsJsonFile(
385385
string? stdOut;
386386
string? stdErr;
387387

388-
var msbuildArgs = MSBuildArgs.AnalyzeMSBuildArguments([..args], CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, BuildCommandParser.TargetOption, BuildCommandParser.VerbosityOption, BuildCommandParser.NoLogoOption);
388+
var msbuildArgs = MSBuildArgs.AnalyzeMSBuildArguments([..args], CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, BuildCommandDefinition.TargetOption, BuildCommandDefinition.VerbosityOption, BuildCommandDefinition.NoLogoOption);
389389
var forwardingAppWithoutLogging = new MSBuildForwardingAppWithoutLogging(msbuildArgs, msBuildExePath);
390390
if (forwardingAppWithoutLogging.ExecuteMSBuildOutOfProc)
391391
{

src/Cli/dotnet/CommandLineInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void PrintInfo()
2424
Reporter.Output.WriteLine($"{LocalizableStrings.DotNetSdkInfoLabel}");
2525
Reporter.Output.WriteLine($" Version: {Product.Version}");
2626
Reporter.Output.WriteLine($" Commit: {commitSha}");
27-
Reporter.Output.WriteLine($" Workload version: {WorkloadCommandParser.GetWorkloadsVersion()}");
27+
Reporter.Output.WriteLine($" Workload version: {WorkloadCommandDefinition.GetWorkloadsVersion()}");
2828
Reporter.Output.WriteLine($" MSBuild version: {MSBuildForwardingAppWithoutLogging.MSBuildVersion.ToString()}");
2929
Reporter.Output.WriteLine();
3030
Reporter.Output.WriteLine($"{LocalizableStrings.DotNetRuntimeInfoLabel}");
@@ -40,7 +40,7 @@ private static void PrintWorkloadsInfo()
4040
{
4141
Reporter.Output.WriteLine();
4242
Reporter.Output.WriteLine($"{LocalizableStrings.DotnetWorkloadInfoLabel}");
43-
WorkloadCommandParser.ShowWorkloadsInfo(showVersion: false);
43+
WorkloadCommandDefinition.ShowWorkloadsInfo(showVersion: false);
4444
}
4545

4646
private static string GetDisplayRid(DotnetVersionFile versionFile)

src/Cli/dotnet/Commands/Build/BuildCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
2222
parseResult.ShowHelpOrErrorIfAppropriate();
2323

2424
CommonOptions.ValidateSelfContainedOptions(
25-
parseResult.HasOption(BuildCommandParser.SelfContainedOption),
26-
parseResult.HasOption(BuildCommandParser.NoSelfContainedOption));
25+
parseResult.HasOption(BuildCommandDefinition.SelfContainedOption),
26+
parseResult.HasOption(BuildCommandDefinition.NoSelfContainedOption));
2727

28-
bool noRestore = parseResult.HasOption(BuildCommandParser.NoRestoreOption);
28+
bool noRestore = parseResult.HasOption(BuildCommandDefinition.NoRestoreOption);
2929

3030
return CommandFactory.CreateVirtualOrPhysicalCommand(
3131
BuildCommandParser.GetCommand(),
32-
BuildCommandParser.SlnOrProjectOrFileArgument,
32+
BuildCommandDefinition.SlnOrProjectOrFileArgument,
3333
(msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand(
3434
entryPointFileFullPath: Path.GetFullPath(appFilePath),
3535
msbuildArgs: msbuildArgs
@@ -43,7 +43,7 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
4343
noRestore: noRestore,
4444
msbuildPath: msbuildPath
4545
),
46-
[CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, BuildCommandParser.TargetOption, BuildCommandParser.VerbosityOption, BuildCommandParser.NoLogoOption],
46+
[CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, BuildCommandDefinition.TargetOption, BuildCommandDefinition.VerbosityOption, BuildCommandDefinition.NoLogoOption],
4747
parseResult,
4848
msbuildPath
4949
);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.CommandLine;
5+
using Microsoft.DotNet.Cli.CommandLine;
6+
using Microsoft.DotNet.Cli.Commands.Restore;
7+
using Microsoft.DotNet.Cli.Extensions;
8+
9+
namespace Microsoft.DotNet.Cli.Commands.Build;
10+
11+
internal static class BuildCommandDefinition
12+
{
13+
public static readonly string DocsLink = "https://aka.ms/dotnet-build";
14+
15+
public static readonly Argument<string[]> SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName)
16+
{
17+
Description = CliStrings.SolutionOrProjectOrFileArgumentDescription,
18+
Arity = ArgumentArity.ZeroOrMore
19+
};
20+
21+
public static readonly Option<string> OutputOption = new Option<string>("--output", "-o")
22+
{
23+
Description = CliCommandStrings.BuildOutputOptionDescription,
24+
HelpName = CliCommandStrings.OutputOptionName
25+
}.ForwardAsOutputPath("OutputPath");
26+
27+
public static readonly Option<bool> NoIncrementalOption = new Option<bool>("--no-incremental")
28+
{
29+
Description = CliCommandStrings.NoIncrementalOptionDescription,
30+
Arity = ArgumentArity.Zero
31+
}.ForwardAs("--target:Rebuild");
32+
33+
public static readonly Option<bool> NoDependenciesOption = new Option<bool>("--no-dependencies")
34+
{
35+
Description = CliCommandStrings.NoDependenciesOptionDescription,
36+
Arity = ArgumentArity.Zero
37+
}.ForwardAs("--property:BuildProjectReferences=false");
38+
39+
public static readonly Option<bool> NoLogoOption = CommonOptions.NoLogoOption();
40+
41+
public static readonly Option<bool> NoRestoreOption = CommonOptions.NoRestoreOption;
42+
43+
public static readonly Option<bool> SelfContainedOption = CommonOptions.SelfContainedOption;
44+
45+
public static readonly Option<bool> NoSelfContainedOption = CommonOptions.NoSelfContainedOption;
46+
47+
public static readonly Option<string> RuntimeOption = CommonOptions.RuntimeOption(CliCommandStrings.BuildRuntimeOptionDescription);
48+
49+
public static readonly Option<string> FrameworkOption = CommonOptions.FrameworkOption(CliCommandStrings.BuildFrameworkOptionDescription);
50+
51+
public static readonly Option<string?> ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.BuildConfigurationOptionDescription);
52+
53+
/// <summary>
54+
/// Build actually means 'run the default Target' generally in MSBuild
55+
/// </summary>
56+
public static readonly Option<string[]?> TargetOption = CommonOptions.MSBuildTargetOption();
57+
58+
public static readonly Option<Utils.VerbosityOptions?> VerbosityOption = CommonOptions.VerbosityOption();
59+
60+
public static Command Create()
61+
{
62+
Command command = new("build", CliCommandStrings.BuildAppFullName)
63+
{
64+
DocsLink = DocsLink
65+
};
66+
67+
command.Arguments.Add(SlnOrProjectOrFileArgument);
68+
RestoreCommandDefinition.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: false);
69+
command.Options.Add(FrameworkOption);
70+
command.Options.Add(ConfigurationOption);
71+
command.Options.Add(RuntimeOption);
72+
command.Options.Add(CommonOptions.VersionSuffixOption);
73+
command.Options.Add(NoRestoreOption);
74+
command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption);
75+
command.Options.Add(VerbosityOption);
76+
command.Options.Add(CommonOptions.DebugOption);
77+
command.Options.Add(OutputOption);
78+
command.Options.Add(CommonOptions.ArtifactsPathOption);
79+
command.Options.Add(NoIncrementalOption);
80+
command.Options.Add(NoDependenciesOption);
81+
command.Options.Add(NoLogoOption);
82+
command.Options.Add(SelfContainedOption);
83+
command.Options.Add(NoSelfContainedOption);
84+
command.Options.Add(CommonOptions.ArchitectureOption);
85+
command.Options.Add(CommonOptions.OperatingSystemOption);
86+
command.Options.Add(CommonOptions.DisableBuildServersOption);
87+
command.Options.Add(TargetOption);
88+
command.Options.Add(CommonOptions.GetPropertyOption);
89+
command.Options.Add(CommonOptions.GetItemOption);
90+
command.Options.Add(CommonOptions.GetTargetResultOption);
91+
command.Options.Add(CommonOptions.GetResultOutputFileOption);
92+
93+
return command;
94+
}
95+
}

src/Cli/dotnet/Commands/Build/BuildCommandParser.cs

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,21 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.CommandLine;
5-
using Microsoft.DotNet.Cli.CommandLine;
6-
using Microsoft.DotNet.Cli.Commands.Restore;
7-
using Microsoft.DotNet.Cli.Extensions;
85

96
namespace Microsoft.DotNet.Cli.Commands.Build;
107

118
internal static class BuildCommandParser
129
{
13-
public static readonly string DocsLink = "https://aka.ms/dotnet-build";
14-
15-
public static readonly Argument<string[]> SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName)
16-
{
17-
Description = CliStrings.SolutionOrProjectOrFileArgumentDescription,
18-
Arity = ArgumentArity.ZeroOrMore
19-
};
20-
21-
public static readonly Option<string> OutputOption = new Option<string>("--output", "-o")
22-
{
23-
Description = CliCommandStrings.BuildOutputOptionDescription,
24-
HelpName = CliCommandStrings.OutputOptionName
25-
}.ForwardAsOutputPath("OutputPath");
26-
27-
public static readonly Option<bool> NoIncrementalOption = new Option<bool>("--no-incremental")
28-
{
29-
Description = CliCommandStrings.NoIncrementalOptionDescription,
30-
Arity = ArgumentArity.Zero
31-
}.ForwardAs("--target:Rebuild");
32-
33-
public static readonly Option<bool> NoDependenciesOption = new Option<bool>("--no-dependencies")
34-
{
35-
Description = CliCommandStrings.NoDependenciesOptionDescription,
36-
Arity = ArgumentArity.Zero
37-
}.ForwardAs("--property:BuildProjectReferences=false");
38-
39-
public static readonly Option<bool> NoLogoOption = CommonOptions.NoLogoOption();
40-
41-
public static readonly Option<bool> NoRestoreOption = CommonOptions.NoRestoreOption;
42-
43-
public static readonly Option<bool> SelfContainedOption = CommonOptions.SelfContainedOption;
44-
45-
public static readonly Option<bool> NoSelfContainedOption = CommonOptions.NoSelfContainedOption;
46-
47-
public static readonly Option<string> RuntimeOption = CommonOptions.RuntimeOption(CliCommandStrings.BuildRuntimeOptionDescription);
48-
49-
public static readonly Option<string> FrameworkOption = CommonOptions.FrameworkOption(CliCommandStrings.BuildFrameworkOptionDescription);
50-
51-
public static readonly Option<string?> ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.BuildConfigurationOptionDescription);
52-
53-
/// <summary>
54-
/// Build actually means 'run the default Target' generally in MSBuild
55-
/// </summary>
56-
public static readonly Option<string[]?> TargetOption = CommonOptions.MSBuildTargetOption();
57-
58-
public static readonly Option<Utils.VerbosityOptions?> VerbosityOption = CommonOptions.VerbosityOption();
59-
60-
private static readonly Command Command = ConstructCommand();
10+
private static readonly Command Command = SetAction(BuildCommandDefinition.Create());
6111

6212
public static Command GetCommand()
6313
{
6414
return Command;
6515
}
6616

67-
private static Command ConstructCommand()
17+
private static Command SetAction(Command command)
6818
{
69-
Command command = new("build", CliCommandStrings.BuildAppFullName)
70-
{
71-
DocsLink = DocsLink
72-
};
73-
74-
command.Arguments.Add(SlnOrProjectOrFileArgument);
75-
RestoreCommandParser.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: false);
76-
command.Options.Add(FrameworkOption);
77-
command.Options.Add(ConfigurationOption);
78-
command.Options.Add(RuntimeOption);
79-
command.Options.Add(CommonOptions.VersionSuffixOption);
80-
command.Options.Add(NoRestoreOption);
81-
command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption);
82-
command.Options.Add(VerbosityOption);
83-
command.Options.Add(CommonOptions.DebugOption);
84-
command.Options.Add(OutputOption);
85-
command.Options.Add(CommonOptions.ArtifactsPathOption);
86-
command.Options.Add(NoIncrementalOption);
87-
command.Options.Add(NoDependenciesOption);
88-
command.Options.Add(NoLogoOption);
89-
command.Options.Add(SelfContainedOption);
90-
command.Options.Add(NoSelfContainedOption);
91-
command.Options.Add(CommonOptions.ArchitectureOption);
92-
command.Options.Add(CommonOptions.OperatingSystemOption);
93-
command.Options.Add(CommonOptions.DisableBuildServersOption);
94-
command.Options.Add(TargetOption);
95-
command.Options.Add(CommonOptions.GetPropertyOption);
96-
command.Options.Add(CommonOptions.GetItemOption);
97-
command.Options.Add(CommonOptions.GetTargetResultOption);
98-
command.Options.Add(CommonOptions.GetResultOutputFileOption);
99-
10019
command.SetAction(BuildCommand.Run);
101-
10220
return command;
10321
}
10422
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.CommandLine;
5+
using Microsoft.DotNet.Cli.Commands.BuildServer.Shutdown;
6+
using Microsoft.DotNet.Cli.CommandLine;
7+
using Microsoft.DotNet.Cli.Extensions;
8+
9+
namespace Microsoft.DotNet.Cli.Commands.BuildServer;
10+
11+
internal static class BuildServerCommandDefinition
12+
{
13+
public const string Name = "build-server";
14+
public static readonly string DocsLink = "https://aka.ms/dotnet-build-server";
15+
16+
public static Command Create()
17+
{
18+
var command = new Command(Name, CliCommandStrings.BuildServerCommandDescription)
19+
{
20+
DocsLink = DocsLink
21+
};
22+
23+
command.Subcommands.Add(BuildServerShutdownCommandDefinition.Create());
24+
25+
return command;
26+
}
27+
}
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using Microsoft.DotNet.Cli.Commands.BuildServer.Shutdown;
8-
using Microsoft.DotNet.Cli.CommandLine;
96
using Microsoft.DotNet.Cli.Extensions;
107

118
namespace Microsoft.DotNet.Cli.Commands.BuildServer;
129

1310
internal static class BuildServerCommandParser
1411
{
15-
public static readonly string DocsLink = "https://aka.ms/dotnet-build-server";
16-
17-
private static readonly Command Command = ConstructCommand();
12+
private static readonly Command Command = SetAction(BuildServerCommandDefinition.Create());
1813

1914
public static Command GetCommand()
2015
{
2116
return Command;
2217
}
2318

24-
private static Command ConstructCommand()
19+
private static Command SetAction(Command command)
2520
{
26-
var command = new Command("build-server", CliCommandStrings.BuildServerCommandDescription)
27-
{
28-
DocsLink = DocsLink
29-
};
30-
31-
command.Subcommands.Add(BuildServerShutdownCommandParser.GetCommand());
32-
3321
command.SetAction((parseResult) => parseResult.HandleMissingCommand());
34-
22+
command.Subcommands.Single(c => c.Name == BuildServerShutdownCommandDefinition.Name).SetAction((parseResult) => new BuildServerShutdownCommand(parseResult).Execute());
3523
return command;
3624
}
3725
}

src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public BuildServerShutdownCommand(
2525
IReporter reporter = null)
2626
: base(result)
2727
{
28-
bool msbuild = result.GetValue(BuildServerShutdownCommandParser.MSBuildOption);
29-
bool vbcscompiler = result.GetValue(BuildServerShutdownCommandParser.VbcsOption);
30-
bool razor = result.GetValue(BuildServerShutdownCommandParser.RazorOption);
28+
bool msbuild = result.GetValue(BuildServerShutdownCommandDefinition.MSBuildOption);
29+
bool vbcscompiler = result.GetValue(BuildServerShutdownCommandDefinition.VbcsOption);
30+
bool razor = result.GetValue(BuildServerShutdownCommandDefinition.RazorOption);
3131
bool all = !msbuild && !vbcscompiler && !razor;
3232

3333
_enumerationFlags = ServerEnumerationFlags.None;

0 commit comments

Comments
 (0)