Skip to content

Commit b6bebbc

Browse files
committed
Refactor and rename command generators for clarity
Renamed `CommandImplGenerator` to `SystemCommandlineGenerator` and introduced an abstract `CommandBaseGenerator` for better extensibility. Moved source generation logic to the new `SystemCommandlineGenerator` and updated related tests accordingly.
1 parent 8e69d19 commit b6bebbc

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

new-cli/GitVersion.Cli.Generator.Tests/CommandImplGeneratorTests.cs renamed to new-cli/GitVersion.Cli.Generator.Tests/SystemCommandlineGeneratorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace GitVersion.Cli.Generator.Tests;
88

9-
public class CommandImplGeneratorTests
9+
public class SystemCommandlineGeneratorTests
1010
{
1111
/*language=cs*/
1212
private const string TestCommandSourceCode =
@@ -140,8 +140,8 @@ private void AddCommand(ICommandImpl command, IDictionary<string, ICommandImpl>
140140
[Test]
141141
public async Task ValidateGeneratedCommandImplementation()
142142
{
143-
var generatorType = typeof(CommandImplGenerator);
144-
var sourceGeneratorTest = new CSharpSourceGeneratorTest<CommandImplGenerator, DefaultVerifier>
143+
var generatorType = typeof(SystemCommandlineGenerator);
144+
var sourceGeneratorTest = new CSharpSourceGeneratorTest<SystemCommandlineGenerator, DefaultVerifier>
145145
{
146146
TestState =
147147
{

new-cli/GitVersion.Cli.Generator/CommandImplGenerator.cs renamed to new-cli/GitVersion.Cli.Generator/CommandBaseGenerator.cs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
using GitVersion.Polyfill;
22

3-
// ReSharper disable InconsistentNaming
43
namespace GitVersion;
54

6-
[Generator(LanguageNames.CSharp)]
7-
public class CommandImplGenerator : IIncrementalGenerator
5+
public abstract class CommandBaseGenerator : IIncrementalGenerator
86
{
9-
private const string GeneratedNamespaceName = "GitVersion.Generated";
107
private const string InfraNamespaceName = "GitVersion";
11-
private const string DependencyInjectionNamespaceName = "GitVersion.Infrastructure";
12-
private const string CommandNamespaceName = "GitVersion.Commands";
138
private const string CommandInterfaceFullName = $"{InfraNamespaceName}.ICommand<T>";
149
private const string CommandAttributeFullName = $"{InfraNamespaceName}.CommandAttribute";
1510
private const string CommandAttributeGenericFullName = $"{InfraNamespaceName}.CommandAttribute<T>";
@@ -22,6 +17,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2217
context.RegisterImplementationSourceOutput(commandTypes, GenerateSourceCode);
2318
}
2419

20+
internal abstract void GenerateSourceCode(SourceProductionContext context, ImmutableArray<CommandInfo?> commandInfos);
21+
2522
private static ImmutableArray<CommandInfo?> SelectCommandTypes(Compilation compilation, CancellationToken ct)
2623
{
2724
ct.ThrowIfCancellationRequested();
@@ -38,43 +35,7 @@ static bool SearchQuery(INamedTypeSymbol typeSymbol)
3835
return attributeData is not null;
3936
}
4037
}
41-
private static void GenerateSourceCode(SourceProductionContext context, ImmutableArray<CommandInfo?> commandInfos)
42-
{
43-
foreach (var commandInfo in commandInfos)
44-
{
45-
if (commandInfo == null)
46-
continue;
47-
48-
var commandHandlerTemplate = Template.Parse(Content.CommandImplContent);
49-
50-
var commandHandlerSource = commandHandlerTemplate.Render(new
51-
{
52-
Model = commandInfo,
53-
Namespace = GeneratedNamespaceName
54-
}, member => member.Name);
5538

56-
context.AddSource($"{commandInfo.CommandTypeName}Impl.g.cs", string.Join("\n", commandHandlerSource));
57-
}
58-
59-
var commandHandlersModuleTemplate = Template.Parse(Content.CommandsModuleContent);
60-
var commandHandlersModuleSource = commandHandlersModuleTemplate.Render(new
61-
{
62-
Model = commandInfos,
63-
Namespace = GeneratedNamespaceName,
64-
InfraNamespaceName,
65-
DependencyInjectionNamespaceName,
66-
CommandNamespaceName
67-
}, member => member.Name);
68-
context.AddSource("CommandsModule.g.cs", string.Join("\n", commandHandlersModuleSource));
69-
70-
var rootCommandHandlerTemplate = Template.Parse(Content.RootCommandImplContent);
71-
var rootCommandHandlerSource = rootCommandHandlerTemplate.Render(new
72-
{
73-
Namespace = GeneratedNamespaceName,
74-
InfraNamespaceName
75-
}, member => member.Name);
76-
context.AddSource("RootCommandImpl.g.cs", string.Join("\n", rootCommandHandlerSource));
77-
}
7839
private static CommandInfo? MapToCommandInfo(ITypeSymbol classSymbol, CancellationToken ct)
7940
{
8041
ct.ThrowIfCancellationRequested();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace GitVersion;
2+
3+
[Generator(LanguageNames.CSharp)]
4+
public class SystemCommandlineGenerator : CommandBaseGenerator
5+
{
6+
private const string GeneratedNamespaceName = "GitVersion.Generated";
7+
private const string InfraNamespaceName = "GitVersion";
8+
private const string DependencyInjectionNamespaceName = "GitVersion.Infrastructure";
9+
private const string CommandNamespaceName = "GitVersion.Commands";
10+
11+
internal override void GenerateSourceCode(SourceProductionContext context, ImmutableArray<CommandInfo?> commandInfos)
12+
{
13+
foreach (var commandInfo in commandInfos)
14+
{
15+
if (commandInfo == null)
16+
continue;
17+
18+
var commandHandlerTemplate = Template.Parse(Content.CommandImplContent);
19+
20+
var commandHandlerSource = commandHandlerTemplate.Render(new
21+
{
22+
Model = commandInfo,
23+
Namespace = GeneratedNamespaceName
24+
}, member => member.Name);
25+
26+
context.AddSource($"{commandInfo.CommandTypeName}Impl.g.cs", string.Join("\n", commandHandlerSource));
27+
}
28+
29+
var commandHandlersModuleTemplate = Template.Parse(Content.CommandsModuleContent);
30+
var commandHandlersModuleSource = commandHandlersModuleTemplate.Render(new
31+
{
32+
Model = commandInfos,
33+
Namespace = GeneratedNamespaceName,
34+
InfraNamespaceName,
35+
DependencyInjectionNamespaceName,
36+
CommandNamespaceName
37+
}, member => member.Name);
38+
context.AddSource("CommandsModule.g.cs", string.Join("\n", commandHandlersModuleSource));
39+
40+
var rootCommandHandlerTemplate = Template.Parse(Content.RootCommandImplContent);
41+
var rootCommandHandlerSource = rootCommandHandlerTemplate.Render(new
42+
{
43+
Namespace = GeneratedNamespaceName,
44+
InfraNamespaceName
45+
}, member => member.Name);
46+
context.AddSource("RootCommandImpl.g.cs", string.Join("\n", rootCommandHandlerSource));
47+
}
48+
}

0 commit comments

Comments
 (0)