Skip to content

Commit e3af6a9

Browse files
Copilottmat
andcommitted
Refactor workload subcommands to separate parsing from actions
Split workload subcommand parsers into Definition and Parser files following the SolutionCommand pattern: - Created *CommandDefinition.cs files containing command structure without SetAction - Created *CommandParser.cs files to set actions via CreateCommand() - Updated WorkloadCommandDefinition.Create() to call *CommandDefinition.Create() - Updated WorkloadCommandParser.ConfigureCommand() to set actions on all subcommands - Updated all references to use *CommandDefinition for accessing options/arguments Refactored commands: - WorkloadInstall, WorkloadUpdate, WorkloadList, WorkloadSearch, WorkloadUninstall - WorkloadRepair, WorkloadRestore, WorkloadClean, WorkloadElevate, WorkloadConfig, WorkloadHistory Co-authored-by: tmat <41759+tmat@users.noreply.github.com>
1 parent 54085da commit e3af6a9

40 files changed

+494
-288
lines changed

src/Cli/dotnet/Commands/Format/FormatCommandDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ public static Command Create()
2121
Arguments = { Arguments },
2222
DocsLink = DocsLink,
2323
};
24+
25+
return formatCommand;
26+
}
2427
}

src/Cli/dotnet/Commands/Workload/Clean/WorkloadCleanCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public WorkloadCleanCommand(
2929
IReporter? reporter = null,
3030
IWorkloadResolverFactory? workloadResolverFactory = null) : base(parseResult, reporter: reporter)
3131
{
32-
_cleanAll = parseResult.GetValue(WorkloadCleanCommandParser.CleanAllOption);
32+
_cleanAll = parseResult.GetValue(WorkloadCleanCommandDefinition.CleanAllOption);
3333

3434
_workloadResolverFactory = workloadResolverFactory ?? new WorkloadResolverFactory();
3535

36-
if (!string.IsNullOrEmpty(parseResult.GetValue(WorkloadUninstallCommandParser.VersionOption)))
36+
if (!string.IsNullOrEmpty(parseResult.GetValue(WorkloadUninstallCommandDefinition.VersionOption)))
3737
{
3838
throw new GracefulException(CliCommandStrings.SdkVersionOptionNotSupported);
3939
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
#nullable disable
5+
6+
using System.CommandLine;
7+
8+
namespace Microsoft.DotNet.Cli.Commands.Workload.Clean;
9+
10+
internal static class WorkloadCleanCommandDefinition
11+
{
12+
public static readonly string Name = "clean";
13+
14+
public static readonly Option<bool> CleanAllOption = new("--all") { Description = CliCommandStrings.CleanAllOptionDescription };
15+
16+
public static Command Create()
17+
{
18+
Command command = new(Name, CliCommandStrings.WorkloadCleanCommandDescription);
19+
20+
command.Options.Add(CleanAllOption);
21+
22+
return command;
23+
}
24+
}

src/Cli/dotnet/Commands/Workload/Clean/WorkloadCleanCommandParser.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,17 @@ namespace Microsoft.DotNet.Cli.Commands.Workload.Clean;
99

1010
internal static class WorkloadCleanCommandParser
1111
{
12-
public static readonly Option<bool> CleanAllOption = new("--all") { Description = CliCommandStrings.CleanAllOptionDescription };
13-
14-
private static readonly Command Command = ConstructCommand();
12+
private static readonly Command Command = CreateCommand();
1513

1614
public static Command GetCommand()
1715
{
1816
return Command;
1917
}
2018

21-
private static Command ConstructCommand()
19+
private static Command CreateCommand()
2220
{
23-
Command command = new("clean", CliCommandStrings.WorkloadCleanCommandDescription);
24-
25-
command.Options.Add(CleanAllOption);
26-
21+
var command = WorkloadCleanCommandDefinition.Create();
2722
command.SetAction((parseResult) => new WorkloadCleanCommand(parseResult).Execute());
28-
2923
return command;
3024
}
3125
}

src/Cli/dotnet/Commands/Workload/Config/WorkloadConfigCommand.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public WorkloadConfigCommand(
3232
IWorkloadResolverFactory? workloadResolverFactory = null
3333
) : base(parseResult, CommonOptions.HiddenVerbosityOption, reporter)
3434
{
35-
_hasUpdateMode = parseResult.HasOption(WorkloadConfigCommandParser.UpdateMode);
36-
_updateMode = parseResult.GetValue(WorkloadConfigCommandParser.UpdateMode);
35+
_hasUpdateMode = parseResult.HasOption(WorkloadConfigCommandDefinition.UpdateMode);
36+
_updateMode = parseResult.GetValue(WorkloadConfigCommandDefinition.UpdateMode);
3737

3838
_workloadResolverFactory = workloadResolverFactory ?? new WorkloadResolverFactory();
3939

@@ -57,22 +57,22 @@ public override int Execute()
5757
string? globalJsonPath = SdkDirectoryWorkloadManifestProvider.GetGlobalJsonPath(Environment.CurrentDirectory);
5858
var globalJsonVersion = SdkDirectoryWorkloadManifestProvider.GlobalJsonReader.GetWorkloadVersionFromGlobalJson(globalJsonPath, out bool? shouldUseWorkloadSets);
5959
shouldUseWorkloadSets ??= string.IsNullOrWhiteSpace(globalJsonVersion) ? null : true;
60-
if (WorkloadConfigCommandParser.UpdateMode_WorkloadSet.Equals(_updateMode, StringComparison.InvariantCultureIgnoreCase))
60+
if (WorkloadConfigCommandDefinition.UpdateMode_WorkloadSet.Equals(_updateMode, StringComparison.InvariantCultureIgnoreCase))
6161
{
6262
if (shouldUseWorkloadSets == false)
6363
{
64-
Reporter.WriteLine(string.Format(CliCommandStrings.UpdateModeDoesNotMatchGlobalJson, WorkloadConfigCommandParser.UpdateMode_WorkloadSet, globalJsonPath, WorkloadConfigCommandParser.UpdateMode_Manifests).Yellow());
64+
Reporter.WriteLine(string.Format(CliCommandStrings.UpdateModeDoesNotMatchGlobalJson, WorkloadConfigCommandDefinition.UpdateMode_WorkloadSet, globalJsonPath, WorkloadConfigCommandDefinition.UpdateMode_Manifests).Yellow());
6565
}
6666
else
6767
{
6868
_workloadInstaller.UpdateInstallMode(_sdkFeatureBand, true);
6969
}
7070
}
71-
else if (WorkloadConfigCommandParser.UpdateMode_Manifests.Equals(_updateMode, StringComparison.InvariantCultureIgnoreCase))
71+
else if (WorkloadConfigCommandDefinition.UpdateMode_Manifests.Equals(_updateMode, StringComparison.InvariantCultureIgnoreCase))
7272
{
7373
if (shouldUseWorkloadSets == true)
7474
{
75-
Reporter.WriteLine(string.Format(CliCommandStrings.UpdateModeDoesNotMatchGlobalJson, WorkloadConfigCommandParser.UpdateMode_Manifests, globalJsonPath, WorkloadConfigCommandParser.UpdateMode_WorkloadSet).Yellow());
75+
Reporter.WriteLine(string.Format(CliCommandStrings.UpdateModeDoesNotMatchGlobalJson, WorkloadConfigCommandDefinition.UpdateMode_Manifests, globalJsonPath, WorkloadConfigCommandDefinition.UpdateMode_WorkloadSet).Yellow());
7676
}
7777
else
7878
{
@@ -83,11 +83,11 @@ public override int Execute()
8383
{
8484
if (shouldUseWorkloadSets ?? InstallingWorkloadCommand.ShouldUseWorkloadSetMode(_sdkFeatureBand, _dotnetPath))
8585
{
86-
Reporter.WriteLine(WorkloadConfigCommandParser.UpdateMode_WorkloadSet);
86+
Reporter.WriteLine(WorkloadConfigCommandDefinition.UpdateMode_WorkloadSet);
8787
}
8888
else
8989
{
90-
Reporter.WriteLine(WorkloadConfigCommandParser.UpdateMode_Manifests);
90+
Reporter.WriteLine(WorkloadConfigCommandDefinition.UpdateMode_Manifests);
9191
}
9292
}
9393
else
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#nullable disable
5+
6+
using System.CommandLine;
7+
8+
namespace Microsoft.DotNet.Cli.Commands.Workload.Config;
9+
10+
internal static class WorkloadConfigCommandDefinition
11+
{
12+
public static readonly string Name = "config";
13+
14+
// dotnet workload config --update-mode workload-set
15+
16+
public static readonly string UpdateMode_WorkloadSet = "workload-set";
17+
public static readonly string UpdateMode_Manifests = "manifests";
18+
19+
public static readonly Option<string> UpdateMode = new("--update-mode")
20+
{
21+
Description = CliCommandStrings.UpdateModeDescription,
22+
Arity = ArgumentArity.ZeroOrOne
23+
};
24+
25+
public static Command Create()
26+
{
27+
UpdateMode.AcceptOnlyFromAmong(UpdateMode_WorkloadSet, UpdateMode_Manifests);
28+
29+
Command command = new(Name, CliCommandStrings.WorkloadConfigCommandDescription);
30+
command.Options.Add(UpdateMode);
31+
32+
return command;
33+
}
34+
}

src/Cli/dotnet/Commands/Workload/Config/WorkloadConfigCommandParser.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,20 @@ namespace Microsoft.DotNet.Cli.Commands.Workload.Config;
99

1010
internal static class WorkloadConfigCommandParser
1111
{
12-
// dotnet workload config --update-mode workload-set
13-
14-
public static readonly string UpdateMode_WorkloadSet = "workload-set";
15-
public static readonly string UpdateMode_Manifests = "manifests";
16-
17-
public static readonly Option<string> UpdateMode = new("--update-mode")
18-
{
19-
Description = CliCommandStrings.UpdateModeDescription,
20-
Arity = ArgumentArity.ZeroOrOne
21-
};
22-
23-
private static readonly Command Command = ConstructCommand();
12+
private static readonly Command Command = CreateCommand();
2413

2514
public static Command GetCommand()
2615
{
2716
return Command;
2817
}
2918

30-
private static Command ConstructCommand()
19+
private static Command CreateCommand()
3120
{
32-
UpdateMode.AcceptOnlyFromAmong(UpdateMode_WorkloadSet, UpdateMode_Manifests);
33-
34-
Command command = new("config", CliCommandStrings.WorkloadConfigCommandDescription);
35-
command.Options.Add(UpdateMode);
36-
21+
var command = WorkloadConfigCommandDefinition.Create();
3722
command.SetAction(parseResult =>
3823
{
3924
new WorkloadConfigCommand(parseResult).Execute();
4025
});
41-
4226
return command;
4327
}
4428
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#nullable disable
5+
6+
using System.CommandLine;
7+
8+
namespace Microsoft.DotNet.Cli.Commands.Workload.Elevate;
9+
10+
internal static class WorkloadElevateCommandDefinition
11+
{
12+
public static readonly string Name = "elevate";
13+
14+
public static Command Create()
15+
{
16+
Command command = new(Name, CliCommandStrings.WorkloadElevateCommandDescription)
17+
{
18+
Hidden = true
19+
};
20+
21+
return command;
22+
}
23+
}

src/Cli/dotnet/Commands/Workload/Elevate/WorkloadElevateCommandParser.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,17 @@ namespace Microsoft.DotNet.Cli.Commands.Workload.Elevate;
99

1010
internal static class WorkloadElevateCommandParser
1111
{
12-
private static readonly Command Command = ConstructCommand();
12+
private static readonly Command Command = CreateCommand();
1313

1414
public static Command GetCommand()
1515
{
1616
return Command;
1717
}
1818

19-
private static Command ConstructCommand()
19+
private static Command CreateCommand()
2020
{
21-
Command command = new("elevate", CliCommandStrings.WorkloadElevateCommandDescription)
22-
{
23-
Hidden = true
24-
};
25-
21+
var command = WorkloadElevateCommandDefinition.Create();
2622
command.SetAction((parseResult) => new WorkloadElevateCommand(parseResult).Execute());
27-
2823
return command;
2924
}
3025
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
#nullable disable
5+
6+
using System.CommandLine;
7+
8+
namespace Microsoft.DotNet.Cli.Commands.Workload.History;
9+
10+
internal static class WorkloadHistoryCommandDefinition
11+
{
12+
public static readonly string Name = "history";
13+
14+
public static Command Create()
15+
{
16+
var command = new Command(Name, CliCommandStrings.WorkloadHistoryCommandDescription);
17+
18+
return command;
19+
}
20+
}

0 commit comments

Comments
 (0)