Skip to content

Commit

Permalink
feat: embed version metadata with git information into assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
lodicolo committed Feb 1, 2025
1 parent 52e9873 commit 55a80e2
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 93 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ permissions:

env:
VERSION_PREFIX: 0.8.0
VERSION_SUFFIX_NAME: beta
VERSION_SUFFIX: -beta

jobs:
Expand Down Expand Up @@ -148,7 +149,7 @@ jobs:

- name: Build solution
if: steps.cache-binaries.outputs.cache-hit != 'true' || inputs.forceBuild == true || inputs.forceRestore == true
run: dotnet publish Intersect.sln --sc -r linux-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }}
run: dotnet publish Intersect.sln --sc -r linux-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }} -p:CommitSha=${{ github.sha }} -p:BuildNumber=${{ github.run_number }} -p:VersionNameSuffix=${{ env.VERSION_SUFFIX_NAME }}

- name: Tarball artifacts to maintain unix file permissions
run: tar -cvf linux.tar Intersect.Client/bin/Release/*/*/publish Intersect.Server/bin/Release/*/*/publish
Expand Down Expand Up @@ -216,7 +217,7 @@ jobs:

- name: Build solution
if: steps.cache-binaries.outputs.cache-hit != 'true' || inputs.forceBuild == true || inputs.forceRestore == true
run: dotnet publish Intersect.sln --sc -r osx-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }}
run: dotnet publish Intersect.sln --sc -r osx-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }} -p:CommitSha=${{ github.sha }} -p:BuildNumber=${{ github.run_number }} -p:VersionNameSuffix=${{ env.VERSION_SUFFIX_NAME }}

- name: Tarball artifacts to maintain unix file permissions
run: tar -cvf osx.tar Intersect.Client/bin/Release/*/*/publish Intersect.Server/bin/Release/*/*/publish
Expand Down Expand Up @@ -285,7 +286,7 @@ jobs:

- name: Build solution
if: steps.cache-binaries.outputs.cache-hit != 'true' || inputs.forceBuild == true || inputs.forceRestore == true
run: dotnet publish Intersect.sln --sc -r win-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }}
run: dotnet publish Intersect.sln --sc -r win-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }} -p:CommitSha=${{ github.sha }} -p:BuildNumber=${{ github.run_number }} -p:VersionNameSuffix=${{ env.VERSION_SUFFIX_NAME }}

- name: Tarball artifacts to maintain unix file permissions
run: tar -cvf win.tar Intersect.Client/bin/Release/*/*/publish Intersect.Editor/bin/Release/*/*/publish Intersect.Server/bin/Release/*/*/publish
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ on:

env:
VERSION_PREFIX: 0.8.0
VERSION_SUFFIX_NAME: pr${{ github.event.number }}
VERSION_SUFFIX: -pr${{ github.event.number }}
PARENT_VERSION_SUFFIX: -beta

Expand Down Expand Up @@ -70,10 +71,10 @@ jobs:
- name: Build solution
if: steps.cache-binaries.outputs.cache-hit != 'true' || inputs.forceBuild == true || inputs.forceRestore == true
run: |
dotnet publish Intersect.sln -r win-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }}
dotnet publish Intersect.sln -r win-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }} -p:CommitSha=${{ github.sha }} -p:BuildNumber=${{ github.run_number }} -p:VersionNameSuffix=${{ env.VERSION_SUFFIX_NAME }}
git apply disable-windows-editor.patch
dotnet publish Intersect.sln -r linux-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }}
dotnet publish Intersect.sln -r osx-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }}
dotnet publish Intersect.sln -r linux-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }} -p:CommitSha=${{ github.sha }} -p:BuildNumber=${{ github.run_number }} -p:VersionNameSuffix=${{ env.VERSION_SUFFIX_NAME }}
dotnet publish Intersect.sln -r osx-x64 -p:Configuration=Release -p:PackageVersion=${{ env.VERSION_PREFIX }}${{ env.VERSION_SUFFIX }}.${{ github.run_number }}+build.${{ github.sha }} -p:Version=${{ env.VERSION_PREFIX }}.${{ github.run_number }} -p:CommitSha=${{ github.sha }} -p:BuildNumber=${{ github.run_number }} -p:VersionNameSuffix=${{ env.VERSION_SUFFIX_NAME }}
- name: Package artifacts
uses: AscensionGameDev/actions@a573cea975b37832c9fe4a9ee880e22af93696de
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
</PackageReference>
<PackageReference Include="NCalcSync" Version="3.8.0" />
<PackageReference Include="Semver" Version="2.3.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
<PackageReference Include="System.IO.Abstractions" Version="21.1.7" />
<PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
Expand Down
13 changes: 9 additions & 4 deletions Framework/Intersect.Framework/Intersect.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
<ItemGroup>
<PackageReference Include="MessagePack.Annotations" Version="2.6.100-alpha" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.14" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="System.IO.Hashing" Version="9.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Diagnostics;
using System.Reflection;
using Intersect.Framework.Reflection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace Intersect.Framework.Logging;

public static class LoggerConfigurationExtensions
{
public static (ILoggerFactory, ILogger) CreateLoggerForIntersect(
this LoggerConfiguration loggerConfiguration,
Assembly forAssembly,
string categoryName,
LoggingLevelSwitch? parentLoggingLevelSwitch = null
)
{
var currentProcess = Process.GetCurrentProcess();
var processStartTime = currentProcess.StartTime;
var executableName = Path.GetFileNameWithoutExtension(
currentProcess.MainModule?.FileName ?? forAssembly.GetName().Name
);

LoggingLevelSwitch loggingLevelSwitch =
new(Debugger.IsAttached ? LogEventLevel.Debug : LogEventLevel.Information);

if (parentLoggingLevelSwitch is not null)
{
parentLoggingLevelSwitch.MinimumLevelChanged +=
(_, args) => loggingLevelSwitch.MinimumLevel = args.NewLevel;
}

LoggingLevelSwitch errorLevelSwitch = new();

var serilogLogger = loggerConfiguration
.MinimumLevel.ControlledBy(loggingLevelSwitch)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(
Path.Combine(
"logs",
$"{executableName}-{processStartTime:yyyy_MM_dd-HH_mm_ss_fff}.log"
),
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
)
.WriteTo.File(
Path.Combine("logs", $"errors-{executableName}.log"),
levelSwitch: errorLevelSwitch,
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
)
.CreateLogger();

var loggerFactory = new SerilogLoggerFactory(serilogLogger, dispose: true);
var logger = loggerFactory.CreateLogger(categoryName);

logger.LogInformation(
"Starting {AssemblyName} v{AssemblyVersion}",
forAssembly.GetName().Name,
forAssembly.GetMetadataVersion()
);

errorLevelSwitch.MinimumLevel = LogEventLevel.Error;

return (loggerFactory, logger);
}
}
47 changes: 47 additions & 0 deletions Framework/Intersect.Framework/Reflection/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Resources;
// ReSharper disable MemberCanBePrivate.Global
Expand Down Expand Up @@ -49,6 +50,52 @@ params object[] args
);
}

public static string GetMetadataVersion(this Assembly assembly, bool excludeCommitSha = false)
{
var assemblyName = assembly.GetName();
return GetMetadataVersionFrom(assembly, assemblyName, excludeCommitSha);
}

private static string GetMetadataVersionFrom(Assembly assembly, AssemblyName assemblyName, bool excludeCommitSha)
{
var assemblyVersion = assemblyName.Version ?? new Version(0, 0, 0, 0);
var assemblyMetadata = assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.ToDictionary(attribute => attribute.Key, attribute => attribute.Value);

if (assemblyMetadata.TryGetValue("BuildNumber", out var rawBuildNumber))
{
if (int.TryParse(rawBuildNumber, CultureInfo.InvariantCulture, out var buildNumber))
{
assemblyVersion = new Version(
assemblyVersion.Major,
assemblyVersion.Minor,
assemblyVersion.Build,
buildNumber
);
}
}

var metadataVersion = $"{assemblyVersion.ToString()}";
if (assemblyMetadata.TryGetValue("VersionNameSuffix", out var versionNameSuffix))
{
metadataVersion = $"{metadataVersion}-{versionNameSuffix}";
}

if (!excludeCommitSha && assemblyMetadata.TryGetValue("CommitSha", out var commitSha))
{
metadataVersion = $"{metadataVersion}+{commitSha}";
}

return metadataVersion;
}

public static string GetMetadataName(this Assembly assembly, bool excludeCommitSha = false)
{
var assemblyName = assembly.GetName();
var prettyName = $"{assemblyName.Name} v{GetMetadataVersionFrom(assembly, assemblyName, excludeCommitSha)}";
return prettyName;
}

public static string GetVersionName(this Assembly assembly)
{
var version = assembly.GetName().Version;
Expand Down
4 changes: 0 additions & 4 deletions Intersect (Core)/Intersect.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<PackageReference Include="Microsoft.Win32.Primitives" Version="4.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.Buffers" Version="4.6.0" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
Expand Down
36 changes: 9 additions & 27 deletions Intersect.Client.Core/Core/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Intersect.Configuration;
using Intersect.Core;
using Intersect.Factories;
using Intersect.Framework.Logging;
using Intersect.Framework.Reflection;
using Intersect.Network;
using Intersect.Plugins;
using Intersect.Plugins.Contexts;
Expand All @@ -13,6 +15,7 @@
using Serilog.Core;
using Serilog.Events;
using Serilog.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace Intersect.Client.Core;

Expand All @@ -39,36 +42,15 @@ public static void Start(params string[] args)
var commandLineOptions = parser.ParseArguments<ClientCommandLineOptions>(args)
.MapResult(HandleParsedArguments, HandleParserErrors);

var executableName = Path.GetFileNameWithoutExtension(
Process.GetCurrentProcess().MainModule?.FileName ?? Assembly.GetExecutingAssembly().GetName().Name
);

LoggingLevelSwitch loggingLevelSwitch =
new(Debugger.IsAttached ? LogEventLevel.Debug : LogEventLevel.Information);

var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.ControlledBy(loggingLevelSwitch)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(
Path.Combine(
"logs",
$"{executableName}-{Process.GetCurrentProcess().StartTime:yyyy_MM_dd-HH_mm_ss_fff}.log"
),
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
)
.WriteTo.File(
Path.Combine("logs", $"errors-{executableName}.log"),
restrictedToMinimumLevel: LogEventLevel.Error,
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
);

var logger = new SerilogLoggerFactory(loggerConfiguration.CreateLogger()).CreateLogger("Client");

var assemblyName = Assembly.GetExecutingAssembly().GetName();
logger.LogCritical("Starting {AssemblyName} v{Version}", assemblyName.Name, assemblyName.Version);
var executingAssembly = Assembly.GetExecutingAssembly();
var (_, logger) = new LoggerConfiguration().CreateLoggerForIntersect(
executingAssembly,
"Client",
loggingLevelSwitch
);

var packetTypeRegistry = new PacketTypeRegistry(logger, typeof(SharedConstants).Assembly);
if (!packetTypeRegistry.TryRegisterBuiltIn())
Expand Down
6 changes: 1 addition & 5 deletions Intersect.Client.Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ internal static void Main(string[] args)

try
{
var type = Type.GetType("Intersect.Client.Core.Bootstrapper", true);
Debug.Assert(type != null, "type != null");
var method = type.GetMethod("Start");
Debug.Assert(method != null, "method != null");
method.Invoke(null, new object[] { args });
Bootstrapper.Start(args);
}
catch (NoSuitableGraphicsDeviceException noSuitableGraphicsDeviceException)
{
Expand Down
5 changes: 5 additions & 0 deletions Intersect.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
using System.Reflection;
using Intersect.Framework.Reflection;

Console.WriteLine($"Starting {Assembly.GetExecutingAssembly().GetMetadataName()}...");

Intersect.Client.Core.Program.Main(args);
30 changes: 8 additions & 22 deletions Intersect.Editor/Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,15 @@ public static partial class Program

static Program()
{
var executableName = Path.GetFileNameWithoutExtension(
Process.GetCurrentProcess().MainModule?.FileName ?? Assembly.GetExecutingAssembly().GetName().Name
LoggingLevelSwitch loggingLevelSwitch =

Check failure on line 22 in Intersect.Editor/Core/Program.cs

View workflow job for this annotation

GitHub Actions / Build for Windows

The type or namespace name 'LoggingLevelSwitch' could not be found (are you missing a using directive or an assembly reference?)
new(Debugger.IsAttached ? LogEventLevel.Debug : LogEventLevel.Information);

var executingAssembly = Assembly.GetExecutingAssembly();
var (_, logger) = new LoggerConfiguration().CreateLoggerForIntersect(

Check failure on line 26 in Intersect.Editor/Core/Program.cs

View workflow job for this annotation

GitHub Actions / Build for Windows

'LoggerConfiguration' does not contain a definition for 'CreateLoggerForIntersect' and no accessible extension method 'CreateLoggerForIntersect' accepting a first argument of type 'LoggerConfiguration' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 26 in Intersect.Editor/Core/Program.cs

View workflow job for this annotation

GitHub Actions / Build for Windows

Cannot infer the type of implicitly-typed deconstruction variable '_'.

Check failure on line 26 in Intersect.Editor/Core/Program.cs

View workflow job for this annotation

GitHub Actions / Build for Windows

Cannot infer the type of implicitly-typed discard.

Check failure on line 26 in Intersect.Editor/Core/Program.cs

View workflow job for this annotation

GitHub Actions / Build for Windows

Cannot infer the type of implicitly-typed deconstruction variable 'logger'.
executingAssembly,
"Editor",
loggingLevelSwitch
);
var loggerConfiguration = new LoggerConfiguration().MinimumLevel
.Is(Debugger.IsAttached ? LogEventLevel.Debug : LogEventLevel.Information).Enrich.FromLogContext().WriteTo
.Console().WriteTo.File(
Path.Combine(
"logs",
$"{executableName}-{Process.GetCurrentProcess().StartTime:yyyy_MM_dd-HH_mm_ss_fff}.log"
),
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
).WriteTo.File(
Path.Combine("logs", $"errors-{executableName}.log"),
restrictedToMinimumLevel: LogEventLevel.Error,
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
);

var logger = new SerilogLoggerFactory(loggerConfiguration.CreateLogger()).CreateLogger("Editor");

var assemblyName = Assembly.GetExecutingAssembly().GetName();
logger.LogCritical("Starting {AssemblyName} v{Version}", assemblyName.Name, assemblyName.Version);

ApplicationContext.Context.Value =
new FakeApplicationContextForThisGarbageWinFormsEditorThatIHateAndWishItWouldBurnInAFireContext(logger);
Expand Down
Loading

0 comments on commit 55a80e2

Please sign in to comment.