Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit deb9681

Browse files
committed
Update System.CommandLine
1 parent f08719b commit deb9681

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ while [[ -h $source ]]; do
1313
done
1414

1515
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
16-
"$scriptroot/eng/common/build.sh" --build --restore $@
16+
"$scriptroot/eng/common/build.sh" --build --restore --pack $@

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<NewtonsoftJsonVersion>13.0.1</NewtonsoftJsonVersion>
1717
<NuGetVersioningVersion>6.0.0</NuGetVersioningVersion>
1818
<SystemCollectionsImmutableVersion>6.0.0</SystemCollectionsImmutableVersion>
19-
<SystemCommandLineVersion>2.0.0-beta1.20371.2</SystemCommandLineVersion>
19+
<SystemCommandLineVersion>2.0.0-beta3.22114.1</SystemCommandLineVersion>
2020
<SystemConfigurationConfigurationManagerVersion>6.0.0</SystemConfigurationConfigurationManagerVersion>
2121
<SystemSecurityPrincipalWindowsVersion>5.0.0</SystemSecurityPrincipalWindowsVersion>
2222
</PropertyGroup>

src/try-convert/Program.cs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.CommandLine;
33
using System.CommandLine.Builder;
4-
using System.CommandLine.Invocation;
54
using System.CommandLine.Parsing;
65
using System.IO;
76
using System.Threading.Tasks;
@@ -14,58 +13,88 @@ namespace MSBuild.Conversion
1413
{
1514
internal class Program
1615
{
16+
private static Task<int> ErrorResult => Task.FromResult(-1);
17+
private static Task<int> SuccessResult => Task.FromResult(0);
18+
19+
private static Option ProjectOption => new Option<string?>(new[] { "-p", "--project" }, "The path to a project to convert");
20+
private static Option WorkspaceOption => new Option<string?>(new[] { "-w", "--workspace" }, "The solution or project file to operate on. If a project is not specified, the command will search the current directory for one.");
21+
private static Option MSBuildPathOption => new Option<string?>(new[] { "-m", "--msbuild-path" }, "The path to an MSBuild.exe, if you prefer to use that");
22+
private static Option TargetFrameworkOption => new Option<string?>(new[] { "-tfm", "--target-framework" }, "The name of the framework you would like to upgrade to. If unspecified, the default TFM for apps chosen will be the highest available one found on your machine, and the default TFM for libraries will be .NET Standard 2.0.");
23+
private static Option ForceWebConversionOption => new Option<bool>(new[] { "--force-web-conversion" }, "Attempt to convert MVC and WebAPI projects even though significant manual work is necessary after migrating such projects.");
24+
private static Option PreviewOption => new Option<bool>(new[] { "--preview" }, "Use preview SDKs as part of conversion");
25+
private static Option DiffOnlyOption => new Option<bool>(new[] { "--diff-only" }, "Produces a diff of the project to convert; no conversion is done");
26+
private static Option NoBackupOption => new Option<bool>(new[] { "--no-backup" }, "Converts projects, does not create a backup of the originals and removes packages.config file.");
27+
private static Option KeepCurrentTfmsOption => new Option<bool>(new[] { "--keep-current-tfms" }, "Converts project files but does not change any TFMs. If unspecified, TFMs may change.");
28+
private static Option MauiConverionOption => new Option<bool>(new[] { "--maui-conversion" }, "Attempt to convert Xamarin.Forms Projects to .NET MAUI projects. There may be additional manual work necessary after migrating such projects.");
29+
private static Option ForceRemoveCustomImportsOption => new Option<bool>(new[] { "--force-remove-custom-imports" }, "Force remove custom imports from the project file if set to true.");
30+
private static Option UpdateOption => new Option<bool>(new[] { "-u", "--update" }, "Updates the try-convert tool to the latest available version");
31+
1732
private static async Task<int> Main(string[] args)
1833
{
1934
var rootCommand = new RootCommand
2035
{
21-
Name = "try-convert",
22-
Handler = CommandHandler.Create(typeof(Program).GetMethod(nameof(Run))!)
36+
ProjectOption,
37+
WorkspaceOption,
38+
MSBuildPathOption,
39+
TargetFrameworkOption,
40+
ForceWebConversionOption,
41+
PreviewOption,
42+
DiffOnlyOption,
43+
NoBackupOption,
44+
KeepCurrentTfmsOption,
45+
MauiConverionOption,
46+
ForceRemoveCustomImportsOption,
47+
UpdateOption
2348
};
2449

50+
Func<string?, string?, string?, string?, bool, bool, bool, bool, bool, bool, bool, bool, Task<int>> handler = Run;
51+
52+
rootCommand.SetHandler(
53+
handler,
54+
ProjectOption,
55+
WorkspaceOption,
56+
MSBuildPathOption,
57+
TargetFrameworkOption,
58+
ForceWebConversionOption,
59+
PreviewOption,
60+
DiffOnlyOption,
61+
NoBackupOption,
62+
KeepCurrentTfmsOption,
63+
MauiConverionOption,
64+
ForceRemoveCustomImportsOption,
65+
UpdateOption);
66+
2567
var parser =
2668
new CommandLineBuilder(rootCommand)
2769
.UseParseDirective()
2870
.UseHelp()
29-
.UseDebugDirective()
3071
.UseSuggestDirective()
3172
.RegisterWithDotnetSuggest()
3273
.UseParseErrorReporting()
3374
.UseExceptionHandler()
34-
.AddOption(new Option(new[] { "-p", "--project" }, "The path to a project to convert") { Argument = new Argument<string?>(() => null) })
35-
.AddOption(new Option(new[] { "-w", "--workspace" }, "The solution or project file to operate on. If a project is not specified, the command will search the current directory for one.") { Argument = new Argument<string?>(() => null) })
36-
.AddOption(new Option(new[] { "-m", "--msbuild-path" }, "The path to an MSBuild.exe, if you prefer to use that") { Argument = new Argument<string?>(() => null) })
37-
.AddOption(new Option(new[] { "-tfm", "--target-framework" }, "The name of the framework you would like to upgrade to. If unspecified, the default TFM for apps chosen will be the highest available one found on your machine, and the default TFM for libraries will be .NET Standard 2.0.") { Argument = new Argument<string?>(() => null) })
38-
.AddOption(new Option(new[] { "--force-web-conversion" }, "Attempt to convert MVC and WebAPI projects even though significant manual work is necessary after migrating such projects.") { Argument = new Argument<bool>(() => false) })
39-
.AddOption(new Option(new[] { "--preview" }, "Use preview SDKs as part of conversion") { Argument = new Argument<bool>(() => false) })
40-
.AddOption(new Option(new[] { "--diff-only" }, "Produces a diff of the project to convert; no conversion is done") { Argument = new Argument<bool>(() => false) })
41-
.AddOption(new Option(new[] { "--no-backup" }, "Converts projects, does not create a backup of the originals and removes packages.config file.") { Argument = new Argument<bool>(() => false) })
42-
.AddOption(new Option(new[] { "--keep-current-tfms" }, "Converts project files but does not change any TFMs. If unspecified, TFMs may change.") { Argument = new Argument<bool>(() => false) })
43-
.AddOption(new Option(new[] { "--maui-conversion" }, "Attempt to convert Xamarin.Forms Projects to .NET MAUI projects. There may be additional manual work necessary after migrating such projects.") { Argument = new Argument<bool>(() => false) })
44-
.AddOption(new Option(new[] { "--force-remove-custom-imports" }, "Force remove custom imports from the project file if set to true.") { Argument = new Argument<bool>(() => false) })
45-
.AddOption(new Option(new[] { "-u", "--update" }, "Updates the try-convert tool to the latest available version") { Argument = new Argument<bool>(() => false) })
4675
.Build();
4776

4877
return await parser.InvokeAsync(args).ConfigureAwait(false);
4978
}
5079

51-
public static int Run(string? project, string? workspace, string? msbuildPath, string? tfm, bool forceWebConversion, bool preview, bool diffOnly, bool noBackup, bool keepCurrentTfms, bool update, bool mauiConversion, bool forceRemoveCustomImports)
80+
public static Task<int> Run(string? project, string? workspace, string? msbuildPath, string? tfm, bool forceWebConversion, bool preview, bool diffOnly, bool noBackup, bool keepCurrentTfms, bool update, bool mauiConversion, bool forceRemoveCustomImports)
5281
{
5382
if (update)
5483
{
5584
UpdateTryConvert.Update();
56-
return 0;
85+
return SuccessResult;
5786
}
5887

5988
if (!string.IsNullOrWhiteSpace(project) && !string.IsNullOrWhiteSpace(workspace))
6089
{
6190
Console.WriteLine("Cannot specify both a project and a workspace.");
62-
return -1;
91+
return ErrorResult;
6392
}
6493

6594
if (!string.IsNullOrWhiteSpace(tfm) && keepCurrentTfms)
6695
{
6796
Console.WriteLine($"Both '{nameof(tfm)}' and '{nameof(keepCurrentTfms)}' cannot be specified. Please pick one.");
68-
return -1;
97+
return ErrorResult;
6998
}
7099

71100
try
@@ -86,14 +115,14 @@ public static int Run(string? project, string? workspace, string? msbuildPath, s
86115
else
87116
{
88117
Console.WriteLine("Error locating VS Install Directory. Try setting Environment Variable VSINSTALLDIR.");
89-
return -1;
118+
return ErrorResult;
90119
}
91120
}
92121

93122
if (string.IsNullOrWhiteSpace(msbuildPath))
94123
{
95124
Console.WriteLine("Could not find an MSBuild.");
96-
return -1;
125+
return ErrorResult;
97126
}
98127

99128
if (!string.IsNullOrWhiteSpace(tfm))
@@ -102,7 +131,7 @@ public static int Run(string? project, string? workspace, string? msbuildPath, s
102131
if (!TargetFrameworkHelper.IsValidTargetFramework(tfm))
103132
{
104133
Console.WriteLine($"Invalid framework specified for --target-framework: '{tfm}'");
105-
return -1;
134+
return ErrorResult;
106135
}
107136
}
108137
else
@@ -133,7 +162,7 @@ public static int Run(string? project, string? workspace, string? msbuildPath, s
133162
if (msbuildWorkspace.WorkspaceItems.Length is 0)
134163
{
135164
Console.WriteLine("No projects converted.");
136-
return 0;
165+
return SuccessResult;
137166
}
138167

139168
foreach (var item in msbuildWorkspace.WorkspaceItems)
@@ -161,11 +190,11 @@ public static int Run(string? project, string? workspace, string? msbuildPath, s
161190
catch (Exception e)
162191
{
163192
Console.WriteLine(e.ToString());
164-
return -1;
193+
return ErrorResult;
165194
}
166195

167196
Console.WriteLine("Conversion complete!");
168-
return 0;
197+
return SuccessResult;
169198
}
170199
}
171200
}

0 commit comments

Comments
 (0)