Skip to content

Commit ec1a617

Browse files
committed
Refactor namespace constants into Content class
Moved namespace-related constants from multiple files into the `Content` class for better centralization and consistency. Updated references across the project to use the newly centralized constants, simplifying maintainability. Added `InternalsVisibleTo` in the project file to support unit tests.
1 parent 3dde520 commit ec1a617

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

new-cli/GitVersion.Cli.Generator.Tests/SystemCommandlineGeneratorTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public class SystemCommandlineGeneratorTests
1111
{
1212
/*language=cs*/
1313
private const string TestCommandSourceCode =
14-
"""
15-
using GitVersion.Infrastructure;
14+
$$"""
15+
using {{Content.DependencyInjectionNamespaceName}};
1616
17-
namespace GitVersion.Commands;
17+
namespace {{Content.CommandNamespaceName}};
1818
1919
public record TestCommandSettings
2020
{
@@ -53,9 +53,9 @@ public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken can
5353
using System.CommandLine;
5454
using System.CommandLine.Binding;
5555
56-
using GitVersion.Commands;
56+
using {{Content.CommandNamespaceName}};
5757
58-
namespace GitVersion.Generated;
58+
namespace {{Content.GeneratedNamespaceName}};
5959
6060
public class TestCommandImpl : Command, ICommandImpl
6161
{
@@ -94,11 +94,11 @@ Task<int> Run(ParseResult parseResult, CancellationToken cancellationToken)
9494
$$"""
9595
{{Content.GeneratedHeader}}
9696
using System.CommandLine;
97-
using GitVersion.Infrastructure;
98-
using GitVersion.Commands;
99-
using GitVersion;
97+
using {{Content.DependencyInjectionNamespaceName}};
98+
using {{Content.CommandNamespaceName}};
99+
using {{Content.InfraNamespaceName}};
100100
101-
namespace GitVersion.Generated;
101+
namespace {{Content.GeneratedNamespaceName}};
102102
103103
public class CommandsModule : IGitVersionModule
104104
{
@@ -117,8 +117,8 @@ public void RegisterTypes(IContainerRegistrar services)
117117
{{Content.GeneratedHeader}}
118118
using System.CommandLine;
119119
120-
using GitVersion;
121-
namespace GitVersion.Generated;
120+
using {{Content.InfraNamespaceName}};
121+
namespace {{Content.GeneratedNamespaceName}};
122122
123123
public class RootCommandImpl : RootCommand
124124
{

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

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

55
public abstract class CommandBaseGenerator : IIncrementalGenerator
66
{
7-
private const string InfraNamespaceName = "GitVersion";
8-
private const string CommandInterfaceFullName = $"{InfraNamespaceName}.ICommand<T>";
9-
private const string CommandAttributeFullName = $"{InfraNamespaceName}.CommandAttribute";
10-
private const string CommandAttributeGenericFullName = $"{InfraNamespaceName}.CommandAttribute<T>";
11-
private const string OptionAttributeFullName = $"{InfraNamespaceName}.OptionAttribute";
7+
private const string CommandInterfaceFullName = $"{Content.InfraNamespaceName}.ICommand<T>";
8+
private const string CommandAttributeFullName = $"{Content.InfraNamespaceName}.CommandAttribute";
9+
private const string CommandAttributeGenericFullName = $"{Content.InfraNamespaceName}.CommandAttribute<T>";
10+
private const string OptionAttributeFullName = $"{Content.InfraNamespaceName}.OptionAttribute";
1211

1312
public void Initialize(IncrementalGeneratorInitializationContext context)
1413
{

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ namespace GitVersion;
22

33
public static class Content
44
{
5+
internal const string GeneratedNamespaceName = "GitVersion.Generated";
6+
internal const string InfraNamespaceName = "GitVersion";
7+
internal const string DependencyInjectionNamespaceName = "GitVersion.Infrastructure";
8+
internal const string CommandNamespaceName = "GitVersion.Commands";
9+
510
/*language=cs*/
6-
public const string GeneratedHeader = """
11+
internal const string GeneratedHeader = """
712
//------------------------------------------------------------------------------
813
// <auto-generated>
914
// This code was generated by a tool.

new-cli/GitVersion.Cli.Generator/GitVersion.Cli.Generator.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
<PackageReference Include="Scriban" IncludeAssets="Build" />
1616
<PackageReference Include="System.Text.Json" />
1717
</ItemGroup>
18+
19+
<ItemGroup>
20+
<InternalsVisibleTo Include="GitVersion.Cli.Generator.Tests" />
21+
</ItemGroup>
1822
</Project>

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ namespace GitVersion;
33
[Generator(LanguageNames.CSharp)]
44
public class SystemCommandlineGenerator : CommandBaseGenerator
55
{
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-
116
internal override void GenerateSourceCode(SourceProductionContext context, ImmutableArray<CommandInfo?> commandInfos)
127
{
138
foreach (var commandInfo in commandInfos)
@@ -20,7 +15,7 @@ internal override void GenerateSourceCode(SourceProductionContext context, Immut
2015
var commandHandlerSource = commandHandlerTemplate.Render(new
2116
{
2217
Model = commandInfo,
23-
Namespace = GeneratedNamespaceName
18+
Namespace = Content.GeneratedNamespaceName
2419
}, member => member.Name);
2520

2621
context.AddSource($"{commandInfo.CommandTypeName}Impl.g.cs", string.Join("\n", commandHandlerSource));
@@ -30,18 +25,17 @@ internal override void GenerateSourceCode(SourceProductionContext context, Immut
3025
var commandHandlersModuleSource = commandHandlersModuleTemplate.Render(new
3126
{
3227
Model = commandInfos,
33-
Namespace = GeneratedNamespaceName,
34-
InfraNamespaceName,
35-
DependencyInjectionNamespaceName,
36-
CommandNamespaceName
28+
Namespace = Content.GeneratedNamespaceName,
29+
Content.InfraNamespaceName,
30+
Content.DependencyInjectionNamespaceName,
31+
Content.CommandNamespaceName
3732
}, member => member.Name);
3833
context.AddSource("CommandsModule.g.cs", string.Join("\n", commandHandlersModuleSource));
3934

4035
var rootCommandHandlerTemplate = Template.Parse(Content.RootCommandImplContent);
4136
var rootCommandHandlerSource = rootCommandHandlerTemplate.Render(new
4237
{
43-
Namespace = GeneratedNamespaceName,
44-
InfraNamespaceName
38+
Namespace = Content.GeneratedNamespaceName, Content.InfraNamespaceName
4539
}, member => member.Name);
4640
context.AddSource("RootCommandImpl.g.cs", string.Join("\n", rootCommandHandlerSource));
4741
}

0 commit comments

Comments
 (0)