From 479057fdcc6faab8abb4612fe8a0e5efdaa86285 Mon Sep 17 00:00:00 2001 From: jvyden Date: Fri, 19 Nov 2021 00:18:21 -0500 Subject: [PATCH] Add ability for git version/branch to be embedded in builds of lighthouse --- .gitignore | 4 +- ProjectLighthouse/Helpers/GitVersionHelper.cs | 39 +++++++++++++++++++ ProjectLighthouse/Program.cs | 2 + ProjectLighthouse/ProjectLighthouse.csproj | 14 ++++++- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 ProjectLighthouse/Helpers/GitVersionHelper.cs diff --git a/.gitignore b/.gitignore index 585487d8..d03fbb3d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,6 @@ riderModule.iml /ProjectLighthouse/logs/* /ProjectLighthouse/ProjectLighthouse.csproj.user .vs/ -lighthouse.config.json \ No newline at end of file +lighthouse.config.json +gitBranch.txt +gitVersion.txt \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/GitVersionHelper.cs b/ProjectLighthouse/Helpers/GitVersionHelper.cs new file mode 100644 index 00000000..ecfe9d78 --- /dev/null +++ b/ProjectLighthouse/Helpers/GitVersionHelper.cs @@ -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; } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 8c2c4e6d..030f20d2 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -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); diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index 51f3117d..082c9930 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -21,7 +21,19 @@ - + + + Always + + + + Always + + + + + +