Move remote determination to VersionHelper and format file

Squashed 4 commits:

* Nitpick variable naming and add XML doc
* Use preferred braces style
* Return null directly if unparsed URL is null
* Move remote determination to VersionHelper and format file
This commit is contained in:
koko 2023-09-30 22:46:37 -04:00
commit 7edef5010d
No known key found for this signature in database
GPG key ID: 48BB40C0F649D603
2 changed files with 50 additions and 43 deletions

View file

@ -192,25 +192,7 @@
<div class="ui black attached inverted segment"> <div class="ui black attached inverted segment">
<div class="ui container"> <div class="ui container">
@{ @{
string? firstRemoteUnparsedUrl = null; string? remoteUrl = VersionHelper.DetermineRemoteUrl();
string? firstRemoteParsedUrl = null;
if (VersionHelper.Remotes != null)
{
firstRemoteUnparsedUrl = VersionHelper.Remotes.FirstOrDefault();
}
if (firstRemoteUnparsedUrl != null)
{
if (firstRemoteUnparsedUrl.Contains("git@"))
{
firstRemoteParsedUrl = firstRemoteUnparsedUrl.Replace("git@", "").Replace(":", "/").Split(".git").FirstOrDefault();
}
else if (firstRemoteUnparsedUrl.Contains("https://"))
{
firstRemoteParsedUrl = firstRemoteUnparsedUrl.Replace("https://", "").Split(".git").FirstOrDefault();
}
}
} }
<span> <span>
@Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion) @Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion)
@ -224,9 +206,9 @@
AGPL-3.0 License AGPL-3.0 License
</a> </a>
&centerdot; &centerdot;
@if (!string.IsNullOrWhiteSpace(firstRemoteParsedUrl)) @if (!string.IsNullOrWhiteSpace(remoteUrl))
{ {
<a href="https://@firstRemoteParsedUrl" target="_blank"> <a href="https://@remoteUrl" target="_blank">
Source Code Source Code
</a> </a>
} }

View file

@ -32,11 +32,8 @@ public static class VersionHelper
} }
catch catch
{ {
Logger.Error Logger.Error("Project Lighthouse was built incorrectly. Please make sure git is available when building.",
( LogArea.Startup);
"Project Lighthouse was built incorrectly. Please make sure git is available when building.",
LogArea.Startup
);
CommitHash = "invalid"; CommitHash = "invalid";
Branch = "invalid"; Branch = "invalid";
CanCheckForUpdates = false; CanCheckForUpdates = false;
@ -44,27 +41,55 @@ public static class VersionHelper
if (!IsDirty) return; if (!IsDirty) return;
Logger.Warn Logger.Warn("This is a modified version of Project Lighthouse. " +
(
"This is a modified version of Project Lighthouse. " +
"Please make sure you are properly disclosing the source code to any users who may be using this instance.", "Please make sure you are properly disclosing the source code to any users who may be using this instance.",
LogArea.Startup LogArea.Startup);
);
CanCheckForUpdates = false; CanCheckForUpdates = false;
} }
#nullable enable
/// <summary>
/// Determines the URL of the git remote. This doesn't return a complete URL and it's only really used for the
/// "Source Code" hyperlink in the BaseLayout. Use this with caution.
/// </summary>
public static string? DetermineRemoteUrl()
{
string? remoteUnparsedUrl = null;
string? remoteParsedUrl = null;
if (Remotes != null) remoteUnparsedUrl = Remotes.FirstOrDefault();
if (remoteUnparsedUrl == null) return null;
if (remoteUnparsedUrl.Contains("git@"))
{
remoteParsedUrl = remoteUnparsedUrl.Replace("git@", "").Replace(":", "/").Split(".git").FirstOrDefault();
}
else if (remoteUnparsedUrl.Contains("https://"))
{
remoteParsedUrl = remoteUnparsedUrl.Replace("https://", "").Split(".git").FirstOrDefault();
}
return remoteParsedUrl;
}
#nullable disable
public static string CommitHash { get; set; } public static string CommitHash { get; set; }
public static string Branch { get; set; } public static string Branch { get; set; }
/// <summary> /// <summary>
/// The full revision string. States current revision hash and, if not main, the branch. /// The full revision string. States current revision hash and, if not main, the branch.
/// </summary> /// </summary>
public static string FullRevision { get; set; } private static string FullRevision { get; set; }
/// <summary> /// <summary>
/// The server's branding (environment version) to show to LBP clients. Shows the environment name next to the revision. /// The server's branding (environment version) to show to LBP clients. Shows the environment name next to the
/// revision.
/// </summary> /// </summary>
public static string EnvVer => $"{ServerConfiguration.Instance.Customization.EnvironmentName} {FullRevision}"; public static string EnvVer => $"{ServerConfiguration.Instance.Customization.EnvironmentName} {FullRevision}";
public static string FullVersion => $"Project Lighthouse {ServerConfiguration.Instance.Customization.EnvironmentName} {Branch}@{CommitHash} {Build}"; public static string FullVersion =>
public static bool IsDirty => CommitHash.EndsWith("-dirty") || CommitsOutOfDate != 1 || CommitHash == "invalid" || Branch == "invalid"; $"Project Lighthouse {ServerConfiguration.Instance.Customization.EnvironmentName} {Branch}@{CommitHash} {Build}";
public static bool IsDirty => CommitHash.EndsWith("-dirty") ||
CommitsOutOfDate != 1 ||
CommitHash == "invalid" ||
Branch == "invalid";
public static int CommitsOutOfDate { get; set; } public static int CommitsOutOfDate { get; set; }
public static bool CanCheckForUpdates { get; set; } public static bool CanCheckForUpdates { get; set; }
public static string[] Remotes { get; set; } public static string[] Remotes { get; set; }