Skip to content

Commit 66f823f

Browse files
committed
Refactors container registration to use IServiceCollection
Migrates from a custom `IContainerRegistrar` and `IContainer` to the standard `IServiceCollection` and `ServiceProvider` from Microsoft.Extensions.DependencyInjection. This simplifies dependency injection and improves compatibility with the .NET ecosystem.
1 parent 6a8c6ca commit 66f823f

File tree

16 files changed

+48
-121
lines changed

16 files changed

+48
-121
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.CodeAnalysis.CSharp;
55
using Microsoft.CodeAnalysis.CSharp.Testing;
66
using Microsoft.CodeAnalysis.Testing;
7+
using Microsoft.Extensions.DependencyInjection;
78

89
namespace GitVersion.Cli.Generator.Tests;
910

@@ -97,12 +98,13 @@ Task<int> Run(ParseResult parseResult, CancellationToken cancellationToken)
9798
using {{Content.DependencyInjectionNamespaceName}};
9899
using {{Content.CommandNamespaceName}};
99100
using {{Content.InfraNamespaceName}};
101+
using Microsoft.Extensions.DependencyInjection;
100102
101103
namespace {{Content.GeneratedNamespaceName}};
102104
103105
public class CommandsModule : IGitVersionModule
104106
{
105-
public void RegisterTypes(IContainerRegistrar services)
107+
public void RegisterTypes(IServiceCollection services)
106108
{
107109
services.AddSingleton<RootCommandImpl>();
108110
services.AddSingleton<TestCommand>();
@@ -168,6 +170,7 @@ public async Task ValidateGeneratedCommandImplementation()
168170
AdditionalReferences =
169171
{
170172
MetadataReference.CreateFromFile(typeof(ILogger).Assembly.Location),
173+
MetadataReference.CreateFromFile(typeof(IServiceCollection).Assembly.Location),
171174
MetadataReference.CreateFromFile(typeof(RootCommand).Assembly.Location),
172175
MetadataReference.CreateFromFile(typeof(CommandAttribute).Assembly.Location),
173176
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ private void AddCommand(ICommandImpl command, IDictionary<string, ICommandImpl>
118118
using {{DependencyInjectionNamespaceName}};
119119
using {{CommandNamespaceName}};
120120
using {{InfraNamespaceName}};
121+
using Microsoft.Extensions.DependencyInjection;
121122
122123
namespace {{Namespace}};
123124
124125
public class CommandsModule : IGitVersionModule
125126
{
126-
public void RegisterTypes(IContainerRegistrar services)
127+
public void RegisterTypes(IServiceCollection services)
127128
{
128129
{{- $commands = Model | array.sort "CommandTypeName" }}
129130
services.AddSingleton<RootCommandImpl>();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ internal override void GenerateSourceCode(SourceProductionContext context, Immut
3535
var rootCommandHandlerTemplate = Template.Parse(Content.RootCommandImplContent);
3636
var rootCommandHandlerSource = rootCommandHandlerTemplate.Render(new
3737
{
38-
Namespace = Content.GeneratedNamespaceName, Content.InfraNamespaceName
38+
Namespace = Content.GeneratedNamespaceName,
39+
Content.InfraNamespaceName
3940
}, member => member.Name);
4041
context.AddSource("RootCommandImpl.g.cs", string.Join("\n", rootCommandHandlerSource));
4142
}

new-cli/GitVersion.Cli/Program.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using GitVersion.Git;
44
using GitVersion.Infrastructure;
55
using GitVersion.SystemCommandline;
6+
using Microsoft.Extensions.DependencyInjection;
67

78
var modules = new IGitVersionModule[]
89
{
@@ -14,19 +15,19 @@
1415
var cts = new CancellationTokenSource();
1516
Console.CancelKeyPress += (_, _) => cts.Cancel();
1617

17-
using var serviceProvider = RegisterModules(modules);
18+
await using var serviceProvider = RegisterModules(modules);
1819
var app = serviceProvider.GetRequiredService<IGitVersionAppRunner>();
1920

2021
var result = await app.RunAsync(args, cts.Token).ConfigureAwait(false);
2122
if (!Console.IsInputRedirected) Console.ReadKey();
2223
return result;
2324

24-
static IContainer RegisterModules(IEnumerable<IGitVersionModule> gitVersionModules)
25+
static ServiceProvider RegisterModules(IEnumerable<IGitVersionModule> gitVersionModules)
2526
{
26-
var serviceProvider = new ContainerRegistrar()
27+
var serviceProvider = new ServiceCollection()
2728
.RegisterModules(gitVersionModules)
28-
.AddLogging()
29-
.Build();
29+
.RegisterLogging()
30+
.BuildServiceProvider();
3031

3132
return serviceProvider;
3233
}

new-cli/GitVersion.Cli/SystemCommandline/CliModule.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using GitVersion.Extensions;
22
using GitVersion.Generated;
33
using GitVersion.Infrastructure;
4+
using Microsoft.Extensions.DependencyInjection;
45

56
namespace GitVersion.SystemCommandline;
67

78
public class CliModule : IGitVersionModule
89
{
9-
public void RegisterTypes(IContainerRegistrar services)
10+
public void RegisterTypes(IServiceCollection services)
1011
{
1112
services.RegisterModule(new CommandsModule());
1213
services.AddSingleton<IGitVersionAppRunner, GitVersionAppRunner>();

new-cli/GitVersion.Cli/TestCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace GitVersion;
22

33
[Command("test", "Test command.")]
4-
public class TestCommand: ICommand<TestCommandSettings>
4+
public class TestCommand : ICommand<TestCommandSettings>
55
{
66
public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken cancellationToken = default)
77
{

new-cli/GitVersion.Common/GitVersion.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
34
<PackageReference Include="Polly" />
45
<PackageReference Include="System.IO.Abstractions" />
56
</ItemGroup>

new-cli/GitVersion.Common/Infrastructure/IContainer.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

new-cli/GitVersion.Common/Infrastructure/IContainerRegistrar.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
13
namespace GitVersion.Infrastructure;
24

35
public interface IGitVersionModule
46
{
5-
void RegisterTypes(IContainerRegistrar services);
7+
void RegisterTypes(IServiceCollection services);
68
}

0 commit comments

Comments
 (0)