Skip to content

Commit c3cee98

Browse files
committed
Add CLI module and refactor app initialization
Introduce `CliModule` for dependency registration, implementing an `IGitVersionApp` interface to decouple app logic. Refactor `Program.cs` to use the new interface and modularize service initialization, improving maintainability and scalability.
1 parent ec1a617 commit c3cee98

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

new-cli/GitVersion.Cli/CliModule.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using GitVersion.Infrastructure;
2+
using GitVersion.SystemCommandline;
3+
4+
namespace GitVersion;
5+
6+
public class CliModule : IGitVersionModule
7+
{
8+
public void RegisterTypes(IContainerRegistrar services)
9+
=> services
10+
.AddSingleton<IGitVersionAppRunner, GitVersionAppRunner>()
11+
.AddLogging();
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GitVersion;
2+
3+
internal interface IGitVersionAppRunner
4+
{
5+
Task<int> RunAsync(string[] args, CancellationToken cancellationToken);
6+
}

new-cli/GitVersion.Cli/Program.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,25 @@
88
{
99
new CoreModule(),
1010
new LibGit2SharpCoreModule(),
11-
new CommandsModule()
11+
new CommandsModule(),
12+
new CliModule()
1213
};
1314

1415
var cts = new CancellationTokenSource();
1516
Console.CancelKeyPress += (_, _) => cts.Cancel();
1617

1718
using var serviceProvider = RegisterModules(modules);
18-
var app = serviceProvider.GetRequiredService<GitVersionApp>();
19-
var result = await app.RunAsync(args, cts.Token).ConfigureAwait(false);
19+
var app = serviceProvider.GetRequiredService<IGitVersionAppRunner>();
2020

21+
var result = 0;
22+
result = await app.RunAsync(args, cts.Token).ConfigureAwait(false);
2123
if (!Console.IsInputRedirected) Console.ReadKey();
22-
2324
return result;
2425

2526
static IContainer RegisterModules(IEnumerable<IGitVersionModule> gitVersionModules)
2627
{
2728
var serviceProvider = new ContainerRegistrar()
2829
.RegisterModules(gitVersionModules)
29-
.AddSingleton<GitVersionApp>()
30-
.AddLogging()
3130
.Build();
3231

3332
return serviceProvider;

new-cli/GitVersion.Cli/GitVersionApp.cs renamed to new-cli/GitVersion.Cli/SystemCommandline/GitVersionAppRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using GitVersion.Infrastructure;
44
using Serilog.Events;
55

6-
namespace GitVersion;
6+
namespace GitVersion.SystemCommandline;
77

88
// ReSharper disable once ClassNeverInstantiated.Global
9-
internal class GitVersionApp(RootCommandImpl rootCommand)
9+
internal class GitVersionAppRunner(RootCommandImpl rootCommand) : IGitVersionAppRunner
1010
{
1111
private readonly RootCommandImpl _rootCommand = rootCommand.NotNull();
1212

0 commit comments

Comments
 (0)