Skip to content

Commit 4a7e8ce

Browse files
committed
upgrade to latest System.Commandline package on the dotnet-libraries feed
1 parent 1d0496d commit 4a7e8ce

File tree

14 files changed

+86
-91
lines changed

14 files changed

+86
-91
lines changed

new-cli/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageVersion Include="Serilog.Sinks.Console" Version="5.0.1" />
1515
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
1616
<PackageVersion Include="Serilog.Sinks.Map" Version="1.0.2" />
17-
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
17+
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.24126.1" />
1818
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
1919
</ItemGroup>
2020
</Project>

new-cli/GitVersion.Cli.Generator/CommandImplGenerator.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,31 @@ private static SettingsPropertyInfo MapToPropertyInfo(IPropertySymbol propertySy
127127
{
128128
var ctorArguments = attribute.ConstructorArguments;
129129

130-
var aliases = (ctorArguments[0].Kind == TypedConstantKind.Array
131-
? ctorArguments[0].Values.Select(x => Convert.ToString(x.Value)).ToArray()
132-
: [Convert.ToString(ctorArguments[0].Value)]).Select(x => $@"""{x?.Trim()}""");
133-
134-
var alias = string.Join(", ", aliases);
130+
var name = Convert.ToString(ctorArguments[0].Value);
135131
var description = Convert.ToString(ctorArguments[1].Value);
132+
133+
ArgumentNullException.ThrowIfNull(name);
136134
ArgumentNullException.ThrowIfNull(description);
137135

136+
string alias = string.Empty;
137+
if (ctorArguments.Length == 3)
138+
{
139+
var aliasesArgs = ctorArguments[2];
140+
var aliases = (aliasesArgs.Kind == TypedConstantKind.Array
141+
? aliasesArgs.Values.Select(x => Convert.ToString(x.Value)).ToArray()
142+
: [Convert.ToString(aliasesArgs.Value)]).Select(x => $@"""{x?.Trim()}""");
143+
alias = string.Join(", ", aliases);
144+
}
145+
138146
var isRequired = propertySymbol.Type.NullableAnnotation == NullableAnnotation.NotAnnotated;
139147
return new()
140148
{
141149
Name = propertySymbol.Name,
142150
TypeName = propertySymbol.Type.ToDisplayString(),
151+
OptionName = name,
143152
Aliases = alias,
144153
Description = description,
145-
IsRequired = isRequired
154+
Required = isRequired
146155
};
147156
}
148157
}

new-cli/GitVersion.Cli.Generator/Content.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class Content
2828
2929
namespace {{Namespace}};
3030
31-
public class {{Model.CommandTypeName}}Impl : Command, ICommandImpl
31+
public class {{Model.CommandTypeName}}Impl : CliCommand, ICommandImpl
3232
{
3333
public string CommandName => nameof({{Model.CommandTypeName}}Impl);
3434
{{- if (Model.ParentCommand | string.empty) }}
@@ -39,41 +39,37 @@ public class {{Model.CommandTypeName}}Impl : Command, ICommandImpl
3939
{{- $settingsProperties = Model.SettingsProperties | array.sort "Name" }}
4040
// Options list
4141
{{~ for $prop in $settingsProperties ~}}
42-
protected readonly Option<{{$prop.TypeName}}> {{$prop.Name}}Option;
42+
protected readonly CliOption<{{$prop.TypeName}}> {{$prop.Name}}Option;
4343
{{~ end ~}}
4444
4545
public {{Model.CommandTypeName}}Impl({{Model.CommandTypeName}} command)
4646
: base("{{Model.CommandName}}", "{{Model.CommandDescription}}")
4747
{
4848
{{~ for $prop in $settingsProperties ~}}
49-
{{$prop.Name}}Option = new Option<{{$prop.TypeName}}>(new[] { {{$prop.Aliases}} })
49+
{{$prop.Name}}Option = new CliOption<{{$prop.TypeName}}>("{{$prop.OptionName}}", [{{$prop.Aliases}}])
5050
{
51-
IsRequired = {{$prop.IsRequired}},
51+
Required = {{$prop.Required}},
5252
Description = "{{$prop.Description}}",
5353
};
5454
{{~ end ~}}
5555
5656
{{- for $prop in $settingsProperties ~}}
57-
AddOption({{$prop.Name}}Option);
57+
Add({{$prop.Name}}Option);
5858
{{~ end ~}}
59+
60+
this.SetAction(Run);
61+
return;
5962
60-
this.SetHandler(settings => command.InvokeAsync(settings), new {{Model.SettingsTypeName}}Binder(this));
61-
}
62-
63-
// Binds the command-line arguments to the command-line options.
64-
private class {{Model.SettingsTypeName}}Binder : BinderBase<{{Model.SettingsTypeName}}>
65-
{
66-
private readonly {{Model.CommandTypeName}}Impl _handler;
67-
68-
public {{Model.SettingsTypeName}}Binder({{Model.CommandTypeName}}Impl handler) => _handler = handler;
69-
70-
protected override {{Model.SettingsTypeName}} GetBoundValue(BindingContext bindingContext) =>
71-
new()
63+
Task<int> Run(ParseResult parseResult, CancellationToken cancellationToken)
64+
{
65+
var settings = new {{Model.SettingsTypeName}}
7266
{
7367
{{~ for $prop in $settingsProperties ~}}
74-
{{$prop.Name}} = bindingContext.ParseResult.GetValueForOption(_handler.{{$prop.Name}}Option){{ if $prop.IsRequired }}!{{ end}},
68+
{{$prop.Name}} = parseResult.GetValue({{$prop.Name}}Option){{ if $prop.Required }}!{{ end}},
7569
{{~ end ~}}
7670
};
71+
return command.InvokeAsync(settings, cancellationToken);
72+
}
7773
}
7874
}
7975
""";
@@ -85,7 +81,7 @@ private class {{Model.SettingsTypeName}}Binder : BinderBase<{{Model.SettingsType
8581
using {{InfraNamespaceName}};
8682
namespace {{Namespace}};
8783
88-
public class RootCommandImpl : RootCommand
84+
public class RootCommandImpl : CliRootCommand
8985
{
9086
public RootCommandImpl(IEnumerable<ICommandImpl> commands)
9187
{
@@ -99,12 +95,12 @@ private void AddCommand(ICommandImpl command, IDictionary<string, ICommandImpl>
9995
{
10096
if (!string.IsNullOrWhiteSpace(command.ParentCommandName))
10197
{
102-
var parent = map[command.ParentCommandName] as Command;
103-
parent?.AddCommand((Command)command);
98+
var parent = map[command.ParentCommandName] as CliCommand;
99+
parent?.Add((CliCommand)command);
104100
}
105101
else
106102
{
107-
AddCommand((Command)command);
103+
Add((CliCommand)command);
108104
}
109105
}
110106
}

new-cli/GitVersion.Cli.Generator/Models.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ internal record SettingsPropertyInfo
1616
{
1717
public required string Name { get; init; }
1818
public required string TypeName { get; init; }
19+
public required string OptionName { get; init; }
1920
public required string Aliases { get; init; }
2021
public required string Description { get; init; }
21-
public required bool IsRequired { get; init; }
22+
public required bool Required { get; init; }
2223
}

new-cli/GitVersion.Cli/GitVersionApp.cs

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System.CommandLine;
2-
using System.CommandLine.Builder;
3-
using System.CommandLine.Invocation;
4-
using System.CommandLine.Parsing;
52
using GitVersion.Generated;
63
using GitVersion.Infrastructure;
74
using Serilog.Events;
@@ -10,46 +7,24 @@ namespace GitVersion;
107

118
internal class GitVersionApp(RootCommandImpl rootCommand)
129
{
13-
public Task<int> RunAsync(string[] args) =>
14-
new CommandLineBuilder(rootCommand)
15-
.AddMiddleware(async (context, next) =>
16-
{
17-
EnrichLogger(context);
18-
await next(context);
19-
})
20-
.UseDefaults() // this will also register for dotnet-suggest
21-
.Build()
22-
.InvokeAsync(args);
23-
24-
// Note: there are 2 locations to watch for dotnet-suggest
25-
// - sentinel file: $env:TEMP\system-commandline-sentinel-files\ and
26-
// - registration file: $env:LOCALAPPDATA\.dotnet-suggest-registration.txt
27-
28-
private static void EnrichLogger(InvocationContext context)
10+
public Task<int> RunAsync(string[] args, CancellationToken cancellationToken)
2911
{
30-
Option<T>? GetOption<T>(string alias)
31-
{
32-
foreach (var symbolResult in context.ParseResult.CommandResult.Children)
33-
{
34-
if (symbolResult.Symbol is Option<T> id && id.HasAlias(alias))
35-
return id;
36-
}
37-
return null;
38-
}
39-
40-
T? GetOptionValue<T>(string alias)
41-
{
42-
var option = GetOption<T>(alias);
43-
return option != null ? context.ParseResult.GetValueForOption(option) : default;
44-
}
12+
var cliConfiguration = new CliConfiguration(rootCommand);
13+
var parseResult = cliConfiguration.Parse(args);
14+
15+
var logFile = parseResult.GetValue<FileInfo?>(GitVersionSettings.LogFileOptionName);
16+
var verbosity = parseResult.GetValue<Verbosity?>(GitVersionSettings.VerbosityOption) ?? Verbosity.Normal;
4517

46-
var logFile = GetOptionValue<FileInfo>(GitVersionSettings.LogFileOptionAlias1);
47-
var verbosity = GetOptionValue<Verbosity?>(GitVersionSettings.VerbosityOption) ?? Verbosity.Normal;
48-
49-
LoggingEnricher.Path = logFile?.FullName ?? "log.txt";
18+
if (logFile?.FullName != null) LoggingEnricher.Path = logFile.FullName;
5019
LoggingEnricher.LogLevel.MinimumLevel = GetLevelForVerbosity(verbosity);
20+
21+
return parseResult.InvokeAsync(cancellationToken);
5122
}
5223

24+
// Note: there are 2 locations to watch for dotnet-suggest
25+
// - sentinel file: $env:TEMP\system-commandline-sentinel-files\ and
26+
// - registration file: $env:LOCALAPPDATA\.dotnet-suggest-registration.txt
27+
5328
private static LogEventLevel GetLevelForVerbosity(Verbosity verbosity) => VerbosityMaps[verbosity];
5429

5530
private static readonly Dictionary<Verbosity, LogEventLevel> VerbosityMaps = new()

new-cli/GitVersion.Cli/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
new LibGit2SharpCoreModule()
1010
};
1111

12+
var cts = new CancellationTokenSource();
13+
Console.CancelKeyPress += (_, _) => cts.Cancel();
14+
1215
using var serviceProvider = RegisterModules(modules);
1316
var app = serviceProvider.GetRequiredService<GitVersionApp>();
14-
var result = await app.RunAsync(args);
17+
var result = await app.RunAsync(args, cts.Token).ConfigureAwait(false);
1518

1619
if (!Console.IsInputRedirected) Console.ReadKey();
1720

new-cli/GitVersion.Common.Command/GitVersionSettings.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ namespace GitVersion;
44

55
public record GitVersionSettings
66
{
7-
public const string LogFileOptionAlias1 = "--log-file";
8-
private const string LogFileOptionAlias2 = "-l";
9-
7+
public const string LogFileOptionName = "--log-file";
108
public const string VerbosityOption = "--verbosity";
11-
private const string WorkDirOption = "--work-dir";
129

13-
[Option([LogFileOptionAlias1, LogFileOptionAlias2], "The log file")]
10+
[Option(LogFileOptionName, "The log file", "-l")]
1411
public FileInfo? LogFile { get; init; }
1512

1613
[Option(VerbosityOption, "The verbosity of the logging information")]
17-
public Verbosity Verbosity { get; init; } = Verbosity.Normal;
14+
public Verbosity? Verbosity { get; init; } = GitVersion.Infrastructure.Verbosity.Normal;
1815

19-
[Option(WorkDirOption, "The working directory with the git repository")]
20-
public DirectoryInfo WorkDir { get; init; } = new(Environment.CurrentDirectory);
16+
[Option("--work-dir", "The working directory with the git repository")]
17+
public DirectoryInfo? WorkDir { get; init; } = new(Environment.CurrentDirectory);
2118
}
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
namespace GitVersion;
22

33
[AttributeUsage(AttributeTargets.Property)]
4-
public class OptionAttribute : Attribute
4+
public class OptionAttribute(string name, string description = "", params string[] aliases) : Attribute
55
{
6-
public string[] Aliases { get; }
7-
public string Description { get; }
6+
public string Name { get; } = name;
7+
public string[] Aliases { get; } = aliases;
8+
public string Description { get; } = description;
89

9-
public OptionAttribute(string alias, string description = "")
10-
: this([alias], description)
10+
public OptionAttribute(string name, string description = "")
11+
: this(name, description, [])
1112
{
1213
}
13-
14-
public OptionAttribute(string[] aliases, string description = "")
15-
{
16-
Aliases = aliases;
17-
Description = description;
18-
}
1914
}

new-cli/GitVersion.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{610883EF-A96
1212
ProjectSection(SolutionItems) = preProject
1313
Directory.Build.props = Directory.Build.props
1414
Directory.Packages.props = Directory.Packages.props
15+
nuget.config = nuget.config
1516
EndProjectSection
1617
EndProject
1718
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitVersion.Normalization", "GitVersion.Normalization\GitVersion.Normalization.csproj", "{45776CC1-7FDA-4299-86A7-9F11E7855CE4}"

new-cli/GitVersion.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=enricher/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)