Skip to content

Commit 3a610b0

Browse files
feat(cli): derive version from build
1 parent 56b3b5a commit 3a610b0

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/Arius.Cli/Arius.Cli.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
<UserSecretsId>2c53e63e-555d-44f4-a474-29c01fb8c564</UserSecretsId>
99
<AssemblyName>arius</AssemblyName>
1010
</PropertyGroup>
11+
12+
<!--
13+
Version information
14+
When running in GitHub Actions we want the version number to reflect the run
15+
that produced the build. The GITHUB_RUN_NUMBER variable is provided by the
16+
Actions environment and increases with every run of the workflow. When this
17+
variable is not available (e.g. when building locally) we fall back to a
18+
descriptive "local" suffix.
19+
-->
20+
<PropertyGroup Condition="'$(GITHUB_RUN_NUMBER)' != ''">
21+
<!-- Use the run number as the patch version so the CLI prints e.g. v1.0.123 -->
22+
<Version>1.0.$(GITHUB_RUN_NUMBER)</Version>
23+
</PropertyGroup>
24+
<PropertyGroup Condition="'$(GITHUB_RUN_NUMBER)' == ''">
25+
<Version>1.0.0-local</Version>
26+
</PropertyGroup>
1127
<ItemGroup>
1228
<PackageReference Include="CliFx" />
1329
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" />

src/Arius.Cli/Program.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
66
using Serilog;
7+
using System.Reflection;
78

89
namespace Arius.Cli;
910

@@ -51,23 +52,36 @@ public static CliApplicationBuilder CreateBuilder()
5152
{
5253
var isRunningInContainer = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true";
5354

54-
// Command discovery
55-
//return new CliApplicationBuilder().AddCommandsFromThisAssembly();
55+
var builder = new CliApplicationBuilder()
56+
.SetTitle("arius")
57+
.SetExecutableName("arius")
58+
.SetVersion($"v{GetVersion()}");
5659

5760
if (isRunningInContainer)
5861
{
59-
return new CliApplicationBuilder()
60-
.AddCommands([
61-
typeof(ArchiveDockerCliCommand),
62-
typeof(RestoreDockerCliCommand)]);
62+
builder.AddCommands([
63+
typeof(ArchiveDockerCliCommand),
64+
typeof(RestoreDockerCliCommand)]);
6365
}
6466
else
6567
{
66-
return new CliApplicationBuilder()
67-
.AddCommands([
68-
typeof(ArchiveCliCommand),
69-
typeof(RestoreCliCommand)]);
68+
builder.AddCommands([
69+
typeof(ArchiveCliCommand),
70+
typeof(RestoreCliCommand)]);
7071
}
72+
73+
return builder;
74+
}
75+
76+
private static string GetVersion()
77+
{
78+
// Prefer informational version so that suffixes like "local" are preserved
79+
var informational = typeof(Program).Assembly
80+
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
81+
if (!string.IsNullOrWhiteSpace(informational))
82+
return informational;
83+
84+
return typeof(Program).Assembly.GetName().Version?.ToString() ?? "unknown";
7185
}
7286

7387
public static IServiceProvider CreateServiceProvider()

0 commit comments

Comments
 (0)