Add ability for git version/branch to be embedded in builds of lighthouse

This commit is contained in:
jvyden 2021-11-19 00:18:21 -05:00
parent f636fcb3d7
commit 479057fdcc
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 57 additions and 2 deletions

4
.gitignore vendored
View file

@ -12,4 +12,6 @@ riderModule.iml
/ProjectLighthouse/logs/*
/ProjectLighthouse/ProjectLighthouse.csproj.user
.vs/
lighthouse.config.json
lighthouse.config.json
gitBranch.txt
gitVersion.txt

View file

@ -0,0 +1,39 @@
using System;
using System.IO;
using Kettu;
using LBPUnion.ProjectLighthouse.Logging;
namespace LBPUnion.ProjectLighthouse.Helpers
{
public static class GitVersionHelper
{
static GitVersionHelper()
{
try
{
CommitHash = readManifestFile("gitVersion.txt");
Branch = readManifestFile("gitBranch.txt");
CanCheckForUpdates = true;
}
catch
{
Logger.Log("Project Lighthouse was built incorrectly. Please make sure git is available when building.", LoggerLevelStartup.Instance);
CommitHash = "invalid";
Branch = "invalid";
CanCheckForUpdates = false;
}
}
private static string readManifestFile(string fileName)
{
using Stream stream = typeof(Program).Assembly.GetManifestResourceStream($"{typeof(Program).Namespace}.{fileName}");
using StreamReader reader = new(stream ?? throw new Exception("The assembly or manifest resource is null."));
return reader.ReadToEnd().Trim();
}
public static string CommitHash { get; set; }
public static string Branch { get; set; }
public static bool CanCheckForUpdates { get; set; }
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using Kettu;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Settings;
using Microsoft.AspNetCore.Hosting;
@ -28,6 +29,7 @@ namespace LBPUnion.ProjectLighthouse
Logger.AddLogger(new LighthouseFileLogger());
Logger.Log("Welcome to Project Lighthouse!", LoggerLevelStartup.Instance);
Logger.Log($"Running {ServerStatics.ServerName} {GitVersionHelper.CommitHash}@{GitVersionHelper.Branch}", LoggerLevelStartup.Instance);
// This loads the config, see ServerSettings.cs for more information
Logger.Log("Loaded config file version " + ServerSettings.Instance.ConfigVersion, LoggerLevelStartup.Instance);

View file

@ -21,7 +21,19 @@
</ItemGroup>
<ItemGroup>
<Compile Remove="Types\SlotXsd.cs"/>
<None Remove="gitVersion.txt"/>
<EmbeddedResource Include="gitVersion.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitBranch.txt"/>
<EmbeddedResource Include="gitBranch.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;"/>
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;"/>
</Target>
</Project>