Skip to content

Commit 1d0496d

Browse files
committed
cleanup
1 parent 2410b6a commit 1d0496d

File tree

8 files changed

+38
-118
lines changed

8 files changed

+38
-118
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2020
{
2121
var commandTypes = context.CompilationProvider.Select(SelectCommandTypes);
2222

23-
context.RegisterSourceOutput(commandTypes, GenerateSourceCode);
23+
context.RegisterImplementationSourceOutput(commandTypes, GenerateSourceCode);
2424
}
2525

2626
private static ImmutableArray<CommandInfo?> SelectCommandTypes(Compilation compilation, CancellationToken ct)
2727
{
2828
ct.ThrowIfCancellationRequested();
2929

30-
static bool SearchQuery(INamedTypeSymbol typeSymbol)
31-
{
32-
var attributeData = typeSymbol.GetAttributeData(CommandAttributeFullName) ?? typeSymbol.GetAttributeData(CommandAttributeGenericFullName);
33-
return attributeData is not null;
34-
}
35-
3630
var visitor = new TypeVisitor(SearchQuery, ct);
3731
visitor.Visit(compilation.GlobalNamespace);
3832
var selectCommandTypes = visitor.GetResults();
3933

4034
return selectCommandTypes.Select(selectCommandType => MapToCommandInfo(selectCommandType, ct)).ToImmutableArray();
35+
36+
static bool SearchQuery(INamedTypeSymbol typeSymbol)
37+
{
38+
var attributeData = typeSymbol.GetAttributeData(CommandAttributeFullName) ?? typeSymbol.GetAttributeData(CommandAttributeGenericFullName);
39+
return attributeData is not null;
40+
}
4141
}
4242
private static void GenerateSourceCode(SourceProductionContext context, ImmutableArray<CommandInfo?> commandInfos)
4343
{
@@ -129,14 +129,14 @@ private static SettingsPropertyInfo MapToPropertyInfo(IPropertySymbol propertySy
129129

130130
var aliases = (ctorArguments[0].Kind == TypedConstantKind.Array
131131
? ctorArguments[0].Values.Select(x => Convert.ToString(x.Value)).ToArray()
132-
: new[] { Convert.ToString(ctorArguments[0].Value) }).Select(x => $@"""{x?.Trim()}""");
132+
: [Convert.ToString(ctorArguments[0].Value)]).Select(x => $@"""{x?.Trim()}""");
133133
134134
var alias = string.Join(", ", aliases);
135135
var description = Convert.ToString(ctorArguments[1].Value);
136136
ArgumentNullException.ThrowIfNull(description);
137137

138138
var isRequired = propertySymbol.Type.NullableAnnotation == NullableAnnotation.NotAnnotated;
139-
return new SettingsPropertyInfo
139+
return new()
140140
{
141141
Name = propertySymbol.Name,
142142
TypeName = propertySymbol.Type.ToDisplayString(),

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace GitVersion;
22

33
public static class Content
44
{
5+
/*language=cs*/
56
private const string GeneratedHeader = """
67
//------------------------------------------------------------------------------
78
// <auto-generated>
@@ -14,6 +15,8 @@ public static class Content
1415
#nullable enable
1516
""";
1617

18+
19+
/*language=cs*/
1720
public const string CommandImplContent = $$$"""
1821
{{{GeneratedHeader}}}
1922
using System.CommandLine;
@@ -75,6 +78,7 @@ private class {{Model.SettingsTypeName}}Binder : BinderBase<{{Model.SettingsType
7578
}
7679
""";
7780

81+
/*language=cs*/
7882
public const string RootCommandImplContent = $$$"""
7983
{{{GeneratedHeader}}}
8084
using System.CommandLine;
@@ -106,6 +110,7 @@ private void AddCommand(ICommandImpl command, IDictionary<string, ICommandImpl>
106110
}
107111
""";
108112

113+
/*language=cs*/
109114
public const string CommandsModuleContent = $$$"""
110115
{{{GeneratedHeader}}}
111116
using System.CommandLine;
Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace GitVersion;
22

3-
internal class CommandInfo : IEquatable<CommandInfo>
3+
internal record CommandInfo
44
{
55
public string? ParentCommand { get; init; }
66
public required string CommandTypeNamespace { get; init; }
@@ -9,96 +9,14 @@ internal class CommandInfo : IEquatable<CommandInfo>
99
public required string CommandDescription { get; init; }
1010
public required string SettingsTypeName { get; init; }
1111
public required string SettingsTypeNamespace { get; init; }
12-
public required SettingsPropertyInfo[] SettingsProperties { get; init; } = Array.Empty<SettingsPropertyInfo>();
13-
public bool Equals(CommandInfo? other)
14-
{
15-
if (other is null)
16-
return false;
17-
if (ReferenceEquals(this, other))
18-
return true;
19-
20-
return ParentCommand == other.ParentCommand
21-
&& CommandTypeNamespace == other.CommandTypeNamespace
22-
&& CommandTypeName == other.CommandTypeName
23-
&& CommandName == other.CommandName
24-
&& CommandDescription == other.CommandDescription
25-
&& SettingsTypeName == other.SettingsTypeName
26-
&& SettingsTypeNamespace == other.SettingsTypeNamespace
27-
&& SettingsProperties.Equals(other.SettingsProperties);
28-
}
29-
public override bool Equals(object? obj)
30-
{
31-
if (obj is null)
32-
return false;
33-
if (ReferenceEquals(this, obj))
34-
return true;
35-
if (obj.GetType() != GetType())
36-
return false;
37-
38-
return Equals((CommandInfo)obj);
39-
}
40-
public override int GetHashCode()
41-
{
42-
unchecked
43-
{
44-
var hashCode = (ParentCommand != null ? ParentCommand.GetHashCode() : 0);
45-
hashCode = (hashCode * 397) ^ CommandTypeNamespace.GetHashCode();
46-
hashCode = (hashCode * 397) ^ CommandTypeName.GetHashCode();
47-
hashCode = (hashCode * 397) ^ CommandName.GetHashCode();
48-
hashCode = (hashCode * 397) ^ CommandDescription.GetHashCode();
49-
hashCode = (hashCode * 397) ^ SettingsTypeName.GetHashCode();
50-
hashCode = (hashCode * 397) ^ SettingsTypeNamespace.GetHashCode();
51-
hashCode = (hashCode * 397) ^ SettingsProperties.GetHashCode();
52-
return hashCode;
53-
}
54-
}
55-
public static bool operator ==(CommandInfo? left, CommandInfo? right) => Equals(left, right);
56-
public static bool operator !=(CommandInfo? left, CommandInfo? right) => !Equals(left, right);
12+
public required SettingsPropertyInfo[] SettingsProperties { get; init; } = [];
5713
}
5814

59-
internal class SettingsPropertyInfo : IEquatable<SettingsPropertyInfo>
15+
internal record SettingsPropertyInfo
6016
{
6117
public required string Name { get; init; }
6218
public required string TypeName { get; init; }
6319
public required string Aliases { get; init; }
6420
public required string Description { get; init; }
6521
public required bool IsRequired { get; init; }
66-
public bool Equals(SettingsPropertyInfo? other)
67-
{
68-
if (other is null)
69-
return false;
70-
if (ReferenceEquals(this, other))
71-
return true;
72-
73-
return Name == other.Name
74-
&& TypeName == other.TypeName
75-
&& Aliases == other.Aliases
76-
&& Description == other.Description
77-
&& IsRequired == other.IsRequired;
78-
}
79-
public override bool Equals(object? obj)
80-
{
81-
if (obj is null)
82-
return false;
83-
if (ReferenceEquals(this, obj))
84-
return true;
85-
if (obj.GetType() != GetType())
86-
return false;
87-
88-
return Equals((SettingsPropertyInfo)obj);
89-
}
90-
public override int GetHashCode()
91-
{
92-
unchecked
93-
{
94-
var hashCode = Name.GetHashCode();
95-
hashCode = (hashCode * 397) ^ TypeName.GetHashCode();
96-
hashCode = (hashCode * 397) ^ Aliases.GetHashCode();
97-
hashCode = (hashCode * 397) ^ Description.GetHashCode();
98-
hashCode = (hashCode * 397) ^ IsRequired.GetHashCode();
99-
return hashCode;
100-
}
101-
}
102-
public static bool operator ==(SettingsPropertyInfo? left, SettingsPropertyInfo? right) => Equals(left, right);
103-
public static bool operator !=(SettingsPropertyInfo? left, SettingsPropertyInfo? right) => !Equals(left, right);
10422
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"SourceGenerator": {
4+
"commandName": "DebugRoslynComponent",
5+
"targetProject": "..\\GitVersion.Cli\\GitVersion.Cli.csproj"
6+
}
7+
}
8+
}

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,35 @@
33

44
namespace GitVersion;
55

6-
internal class TypeVisitor : SymbolVisitor
6+
internal class TypeVisitor(Func<INamedTypeSymbol, bool> searchQuery, CancellationToken cancellation)
7+
: SymbolVisitor
78
{
8-
private readonly CancellationToken cancellationToken;
9-
private readonly HashSet<INamedTypeSymbol> exportedTypes;
10-
private readonly Func<INamedTypeSymbol, bool> searchQuery;
9+
private readonly HashSet<INamedTypeSymbol> _exportedTypes = new(SymbolEqualityComparer.Default);
1110

12-
public TypeVisitor(Func<INamedTypeSymbol, bool> searchQuery, CancellationToken cancellation)
13-
{
14-
cancellationToken = cancellation;
15-
exportedTypes = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
16-
this.searchQuery = searchQuery;
17-
}
18-
19-
public ImmutableArray<INamedTypeSymbol> GetResults() => exportedTypes.ToImmutableArray();
11+
public ImmutableArray<INamedTypeSymbol> GetResults() => this._exportedTypes.ToImmutableArray();
2012

2113
public override void VisitAssembly(IAssemblySymbol symbol)
2214
{
23-
cancellationToken.ThrowIfCancellationRequested();
15+
cancellation.ThrowIfCancellationRequested();
2416
symbol.GlobalNamespace.Accept(this);
2517
}
2618

2719
public override void VisitNamespace(INamespaceSymbol symbol)
2820
{
2921
foreach (var namespaceOrType in symbol.GetMembers())
3022
{
31-
cancellationToken.ThrowIfCancellationRequested();
23+
cancellation.ThrowIfCancellationRequested();
3224
namespaceOrType.Accept(this);
3325
}
3426
}
3527

3628
public override void VisitNamedType(INamedTypeSymbol type)
3729
{
38-
cancellationToken.ThrowIfCancellationRequested();
30+
cancellation.ThrowIfCancellationRequested();
3931

4032
if (searchQuery(type))
4133
{
42-
exportedTypes.Add(type);
34+
this._exportedTypes.Add(type);
4335
}
4436
}
4537
}

new-cli/GitVersion.Cli/GitVersionApp.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
namespace GitVersion;
1010

11-
internal class GitVersionApp
11+
internal class GitVersionApp(RootCommandImpl rootCommand)
1212
{
13-
private readonly RootCommandImpl rootCommand;
14-
public GitVersionApp(RootCommandImpl rootCommand) => this.rootCommand = rootCommand;
15-
1613
public Task<int> RunAsync(string[] args) =>
1714
new CommandLineBuilder(rootCommand)
1815
.AddMiddleware(async (context, next) =>
@@ -53,9 +50,9 @@ private static void EnrichLogger(InvocationContext context)
5350
LoggingEnricher.LogLevel.MinimumLevel = GetLevelForVerbosity(verbosity);
5451
}
5552

56-
private static LogEventLevel GetLevelForVerbosity(Verbosity verbosity) => verbosityMaps[verbosity];
53+
private static LogEventLevel GetLevelForVerbosity(Verbosity verbosity) => VerbosityMaps[verbosity];
5754

58-
private static readonly Dictionary<Verbosity, LogEventLevel> verbosityMaps = new()
55+
private static readonly Dictionary<Verbosity, LogEventLevel> VerbosityMaps = new()
5956
{
6057
{ Verbosity.Verbose, LogEventLevel.Verbose },
6158
{ Verbosity.Diagnostic, LogEventLevel.Debug },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public record GitVersionSettings
1010
public const string VerbosityOption = "--verbosity";
1111
private const string WorkDirOption = "--work-dir";
1212

13-
[Option(new[] { LogFileOptionAlias1, LogFileOptionAlias2 }, "The log file")]
13+
[Option([LogFileOptionAlias1, LogFileOptionAlias2], "The log file")]
1414
public FileInfo? LogFile { get; init; }
1515

1616
[Option(VerbosityOption, "The verbosity of the logging information")]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class OptionAttribute : Attribute
77
public string Description { get; }
88

99
public OptionAttribute(string alias, string description = "")
10-
: this(new[] { alias }, description)
10+
: this([alias], description)
1111
{
1212
}
1313

0 commit comments

Comments
 (0)