Skip to content

Commit fc3c3da

Browse files
committed
improves command registration in IoC container
Ensures correct command registration in the IoC container by including the namespace when the command type namespace differs from the main command namespace. Also, adjust the tests to reflect this change.
1 parent 2d242d3 commit fc3c3da

File tree

4 files changed

+49
-37
lines changed

4 files changed

+49
-37
lines changed

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,6 @@ namespace GitVersion.Cli.Generator.Tests;
1111

1212
public class SystemCommandlineGeneratorTests
1313
{
14-
/*language=cs*/
15-
private const string TestCommandSourceCode =
16-
$$"""
17-
using {{Content.InfrastructureNamespaceName}};
18-
using Microsoft.Extensions.Logging;
19-
20-
namespace {{Content.CommandNamespaceName}};
21-
22-
public record TestCommandSettings
23-
{
24-
[Option("--output-file", "The output file")]
25-
public required string OutputFile { get; init; }
26-
}
27-
28-
[CommandAttribute("test", "Test description.")]
29-
public class TestCommand(ILogger logger): ICommand<TestCommandSettings>
30-
{
31-
public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken cancellationToken = default)
32-
{
33-
return Task.FromResult(0);
34-
}
35-
}
36-
37-
""";
38-
3914
/*language=cs*/
4015
private const string GlobalUsingsCode =
4116
"""
@@ -147,6 +122,41 @@ private void AddCommand(ICommandImpl command, IDictionary<string, ICommandImpl>
147122
}
148123
}
149124
}
125+
""";
126+
127+
/*language=cs*/
128+
private const string TestCommandSourceCode =
129+
$$"""
130+
using {{Content.InfrastructureNamespaceName}};
131+
using Microsoft.Extensions.Logging;
132+
133+
namespace {{Content.CommandNamespaceName}};
134+
135+
[CommandAttribute("test", "Test description.")]
136+
public class TestCommand(ILogger logger): ICommand<TestCommandSettings>
137+
{
138+
public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken cancellationToken = default)
139+
{
140+
return Task.FromResult(0);
141+
}
142+
}
143+
144+
""";
145+
146+
/*language=cs*/
147+
private const string TestCommandSettingsSourceCode =
148+
$$"""
149+
using {{Content.InfrastructureNamespaceName}};
150+
using Microsoft.Extensions.Logging;
151+
152+
namespace {{Content.CommandNamespaceName}};
153+
154+
public record TestCommandSettings
155+
{
156+
[Option("--output-file", "The output file")]
157+
public required string OutputFile { get; init; }
158+
}
159+
150160
""";
151161

152162
[Test]
@@ -160,7 +170,8 @@ public async Task ValidateGeneratedCommandImplementation()
160170
Sources =
161171
{
162172
(generatorType, "GlobalUsings.cs", GlobalUsingsCode),
163-
(generatorType, "TestCommand.cs", TestCommandSourceCode)
173+
(generatorType, "TestCommand.cs", TestCommandSourceCode),
174+
(generatorType, "TestCommandSettings.cs", TestCommandSettingsSourceCode)
164175
},
165176
GeneratedSources =
166177
{

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public static class Content
2727
using System.CommandLine.Binding;
2828
2929
using {{Model.CommandTypeNamespace}};
30-
{{- if Model.SettingsTypeNamespace != Model.CommandTypeNamespace }}
31-
using {{Model.SettingsTypeNamespace}};{{ end }}
3230
3331
namespace {{GeneratedNamespaceName}};
3432
@@ -66,7 +64,7 @@ public class {{Model.CommandTypeName}}Impl : Command, ICommandImpl
6664
6765
Task<int> Run(ParseResult parseResult, CancellationToken cancellationToken)
6866
{
69-
var settings = new {{Model.SettingsTypeName}}
67+
var settings = new {{ if Model.SettingsTypeNamespace != Model.CommandTypeNamespace }}{{Model.SettingsTypeNamespace}}.{{ end }}{{Model.SettingsTypeName}}
7068
{
7169
{{~ for $prop in $settingsProperties ~}}
7270
{{$prop.Name}} = parseResult.GetValue({{$prop.Name}}Option){{ if $prop.Required }}!{{ end}},
@@ -129,7 +127,7 @@ public void RegisterTypes(IServiceCollection services)
129127
{{- $commands = Model | array.sort "CommandTypeName" }}
130128
services.AddSingleton<RootCommandImpl>();
131129
{{~ for $command in $commands ~}}
132-
services.AddSingleton<{{$command.CommandTypeName}}>();
130+
services.AddSingleton<{{ if $command.CommandTypeNamespace != CommandNamespaceName }}{{$command.CommandTypeNamespace}}.{{ end }}{{$command.CommandTypeName}}>();
133131
services.AddSingleton<ICommandImpl, {{$command.CommandTypeName}}Impl>();
134132
{{~ end ~}}
135133
}

new-cli/GitVersion.Cli/TestCommand.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace GitVersion;
1+
using GitVersion.Commands.Test.Settings;
2+
3+
namespace GitVersion.Commands.Test;
24

35
[Command("test", "Test command.")]
46
public class TestCommand : ICommand<TestCommandSettings>
@@ -9,9 +11,3 @@ public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken can
911
return Task.FromResult(0);
1012
}
1113
}
12-
13-
public record TestCommandSettings : GitVersionSettings
14-
{
15-
[Option("--input-file", "The input version file")]
16-
public required string InputFile { get; init; }
17-
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GitVersion.Commands.Test.Settings;
2+
3+
public record TestCommandSettings : GitVersionSettings
4+
{
5+
[Option("--input-file", "The input version file")]
6+
public required string InputFile { get; init; }
7+
}

0 commit comments

Comments
 (0)