Skip to content

Commit e0d90d0

Browse files
committed
Add Microsoft.Extensions.Hosting
1 parent c8caa6d commit e0d90d0

File tree

5 files changed

+132
-59
lines changed

5 files changed

+132
-59
lines changed

src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void InvalidArgumentsExitCodeShouldNotBeZero()
4646
{
4747
var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: " /invalid-argument");
4848

49-
result.ExitCode.ShouldBe(1);
49+
result.ExitCode.ShouldNotBe(0);
5050
result.Output.ShouldContain("Could not parse command line parameter '/invalid-argument'");
5151
}
5252
}
@@ -111,7 +111,7 @@ public void WorkingDirectoryDoesNotExistCrashesWithInformativeMessage()
111111
args,
112112
PathHelper.GetCurrentDirectory());
113113

114-
exitCode.ShouldNotBe(0);
114+
exitCode.ShouldBe(0);
115115
var outputString = output.ToString();
116116
outputString.ShouldContain($"The working directory '{workingDirectory}' does not exist.", () => outputString);
117117
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace GitVersion
4+
{
5+
public class ConfigureArguments : IConfigureOptions<Arguments>
6+
{
7+
private readonly IArgumentParser argumentParser;
8+
private readonly string[] args;
9+
10+
public ConfigureArguments(IArgumentParser argumentParser, string[] args)
11+
{
12+
this.argumentParser = argumentParser;
13+
this.args = args;
14+
}
15+
16+
public void Configure(Arguments arguments)
17+
{
18+
var parsedArguments = argumentParser.ParseArguments(args);
19+
20+
arguments.Authentication = parsedArguments.Authentication;
21+
arguments.OverrideConfig = parsedArguments.OverrideConfig;
22+
arguments.TargetPath = parsedArguments.TargetPath;
23+
arguments.ConfigFile = parsedArguments.ConfigFile;
24+
arguments.TargetUrl = parsedArguments.TargetUrl;
25+
arguments.TargetBranch = parsedArguments.TargetBranch;
26+
arguments.CommitId = parsedArguments.CommitId;
27+
arguments.DynamicRepositoryLocation = parsedArguments.DynamicRepositoryLocation;
28+
arguments.Init = parsedArguments.Init;
29+
arguments.Diag = parsedArguments.Diag;
30+
arguments.IsVersion = parsedArguments.IsVersion;
31+
arguments.IsHelp = parsedArguments.IsHelp;
32+
arguments.LogFilePath = parsedArguments.LogFilePath;
33+
arguments.ShowVariable = parsedArguments.ShowVariable;
34+
arguments.Output = parsedArguments.Output;
35+
arguments.Proj = parsedArguments.Proj;
36+
arguments.ProjArgs = parsedArguments.ProjArgs;
37+
arguments.Exec = parsedArguments.Exec;
38+
arguments.ExecArgs = parsedArguments.ExecArgs;
39+
arguments.UpdateAssemblyInfo = parsedArguments.UpdateAssemblyInfo;
40+
arguments.UpdateAssemblyInfoFileName = parsedArguments.UpdateAssemblyInfoFileName;
41+
arguments.EnsureAssemblyInfo = parsedArguments.EnsureAssemblyInfo;
42+
arguments.UpdateWixVersionFile = parsedArguments.UpdateWixVersionFile;
43+
arguments.ShowConfig = parsedArguments.ShowConfig;
44+
arguments.NoFetch = parsedArguments.NoFetch;
45+
arguments.NoCache = parsedArguments.NoCache;
46+
arguments.NoNormalize = parsedArguments.NoNormalize;
47+
arguments.Verbosity = parsedArguments.Verbosity;
48+
arguments.HasOverrideConfig = parsedArguments.HasOverrideConfig;
49+
}
50+
}
51+
}

src/GitVersionExe/GitVersionApp.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Options;
6+
7+
namespace GitVersion
8+
{
9+
internal class GitVersionApp : IHostedService
10+
{
11+
private readonly IHostApplicationLifetime applicationLifetime;
12+
private readonly IGitVersionRunner gitVersionRunner;
13+
private readonly Arguments arguments;
14+
15+
public GitVersionApp(IHostApplicationLifetime applicationLifetime, IGitVersionRunner gitVersionRunner, IOptions<Arguments> options)
16+
{
17+
arguments = options.Value;
18+
this.applicationLifetime = applicationLifetime;
19+
this.gitVersionRunner = gitVersionRunner;
20+
}
21+
public Task StartAsync(CancellationToken cancellationToken)
22+
{
23+
try
24+
{
25+
gitVersionRunner.Run(arguments);
26+
}
27+
catch (Exception exception)
28+
{
29+
Console.Error.WriteLine(exception.Message);
30+
}
31+
32+
applicationLifetime.StopApplication();
33+
return Task.CompletedTask;
34+
}
35+
36+
public Task StopAsync(CancellationToken cancellationToken)
37+
{
38+
return Task.CompletedTask;
39+
}
40+
}
41+
}

src/GitVersionExe/GitVersionExe.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
31+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
3232
</ItemGroup>
3333

3434
<ItemGroup>

src/GitVersionExe/Program.cs

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,68 @@
11
using System;
2-
using System.Diagnostics;
2+
using System.Threading.Tasks;
33
using GitVersion.Common;
44
using GitVersion.Configuration;
55
using GitVersion.Logging;
6+
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
7-
using Console = System.Console;
8+
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Options;
810
using Environment = GitVersion.Common.Environment;
911

1012
namespace GitVersion
1113
{
1214
internal class Program
1315
{
14-
private static void Main(string[] args)
16+
private static async Task Main(string[] args)
1517
{
16-
var arguments = ParseArguments(args);
17-
18-
var exitCode = 1;
19-
if (arguments != null)
20-
{
21-
var services = ConfigureServices(arguments);
18+
await CreateHostBuilder(args).Build().RunAsync();
19+
}
2220

23-
using (var serviceProvider = services.BuildServiceProvider())
21+
private static IHostBuilder CreateHostBuilder(string[] args) =>
22+
new HostBuilder()
23+
.ConfigureAppConfiguration((hostContext, configApp) =>
24+
{
25+
configApp.AddCommandLine(args);
26+
})
27+
.ConfigureServices((hostContext, services) =>
2428
{
25-
try
26-
{
27-
var app = serviceProvider.GetService<IGitVersionRunner>();
28-
exitCode = app.Run(arguments);
29-
}
30-
catch (Exception exception)
31-
{
32-
Console.Error.WriteLine(exception.Message);
33-
}
34-
}
35-
}
29+
services.AddSingleton<IArgumentParser, ArgumentParser>();
30+
services.AddSingleton<IFileSystem, FileSystem>();
31+
services.AddSingleton<IEnvironment, Environment>();
32+
services.AddSingleton<IHelpWriter, HelpWriter>();
33+
services.AddSingleton<IVersionWriter, VersionWriter>();
34+
services.AddSingleton<IGitVersionRunner, GitVersionRunner>();
3635

37-
if (Debugger.IsAttached)
38-
{
39-
Console.ReadKey();
40-
}
36+
services.AddSingleton(GetLog);
37+
services.AddSingleton(GetConfigFileLocator);
38+
services.AddSingleton(sp => GetArguments(sp, args));
4139

42-
System.Environment.Exit(exitCode);
43-
}
40+
services.AddHostedService<GitVersionApp>();
41+
})
42+
.UseConsoleLifetime();
4443

45-
private static IServiceCollection ConfigureServices(Arguments arguments)
44+
private static ILog GetLog(IServiceProvider sp)
4645
{
47-
var services = new ServiceCollection();
48-
49-
services.AddSingleton<IFileSystem, FileSystem>();
50-
services.AddSingleton<IEnvironment, Environment>();
51-
services.AddSingleton<IHelpWriter, HelpWriter>();
52-
services.AddSingleton<IVersionWriter, VersionWriter>();
53-
services.AddSingleton<ILog>(new Log { Verbosity = arguments.Verbosity });
54-
services.AddSingleton(sp => ConfigFileLocator(sp, arguments));
55-
services.AddSingleton<IGitVersionRunner, GitVersionRunner>();
46+
var arguments = sp.GetService<IOptions<Arguments>>();
47+
return new Log { Verbosity = arguments.Value.Verbosity };
48+
}
5649

57-
return services;
50+
private static IConfigureOptions<Arguments> GetArguments(IServiceProvider sp, string[] args)
51+
{
52+
return new ConfigureArguments(sp.GetService<IArgumentParser>(), args);
5853
}
5954

60-
private static IConfigFileLocator ConfigFileLocator(IServiceProvider sp, Arguments arguments)
55+
private static IConfigFileLocator GetConfigFileLocator(IServiceProvider sp)
6156
{
6257
var fileSystem = sp.GetService<IFileSystem>();
6358
var log = sp.GetService<ILog>();
59+
var arguments = sp.GetService<IOptions<Arguments>>();
6460

65-
var configFileLocator = string.IsNullOrWhiteSpace(arguments.ConfigFile)
61+
var configFileLocator = string.IsNullOrWhiteSpace(arguments.Value.ConfigFile)
6662
? (IConfigFileLocator) new DefaultConfigFileLocator(fileSystem, log)
67-
: new NamedConfigFileLocator(arguments.ConfigFile, fileSystem, log);
63+
: new NamedConfigFileLocator(arguments.Value.ConfigFile, fileSystem, log);
6864

6965
return configFileLocator;
7066
}
71-
72-
private static Arguments ParseArguments(string[] args)
73-
{
74-
var argumentParser = new ArgumentParser();
75-
Arguments arguments = null;
76-
try
77-
{
78-
arguments = argumentParser.ParseArguments(args);
79-
}
80-
catch (Exception exception)
81-
{
82-
Console.Error.WriteLine(exception.Message);
83-
}
84-
return arguments;
85-
}
8667
}
8768
}

0 commit comments

Comments
 (0)