Skip to content

Commit

Permalink
Add links to license and source code in footer (#909)
Browse files Browse the repository at this point in the history
* Display link to source code and license in footer

* Remove " (fetch)" suffix in remote URL

* Use split instead of replace for getting the clean remote URL

* Open license link in new tab as well

* LINQ-ify things and handle potential errors

* Properly detect remote URL type and parse accordingly

Squashed 3 commits:

* Properly detect remote URL type and parse accordingly
* Actually fix broken source code link...maybe
* Attempt to fix broken source code link

* Fix a kokoism

Perhaps we should actually utilize the firstRemoteUnparsedUrl variable instead of getting the FirstOrDefault remote over and over

* Fix missing slash when using an SSH remote

* Assign null to parsed URL variable by default

Thank you clyde discord

* Only perform the null check on the unparsed URL once

Thank you again clyde discord

* Handle exception when there are no defined remotes

* 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

* Add param to include HTTPS prefix in DetermineRemoteUrl method

* Remove warnings from XML doc

Not needed as of c63284c

* Properly insert HTTPS prefix into SSH remotes

* Fix license layout and improve git remote parsing

---------

Co-authored-by: Slendy <josh@slendy.pw>
  • Loading branch information
sudokoko and Slendy authored Oct 4, 2023
1 parent c314e8d commit 248f141
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
24 changes: 22 additions & 2 deletions ProjectLighthouse.Servers.Website/Pages/Layouts/BaseLayout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,28 @@
<footer>
<div class="ui black attached inverted segment">
<div class="ui container">

<p>@Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion)</p>
@{
string? remoteUrl = VersionHelper.DetermineRemoteUrl();
}
<span>
@Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion)
</span>
<span style="float: right;">
<a href="https://github.com/LBPUnion/ProjectLighthouse/blob/main/LICENSE" target="_blank">
AGPL-3.0 License
</a>
&centerdot;
@if (!string.IsNullOrWhiteSpace(remoteUrl))
{
<a href="https://@remoteUrl" target="_blank">
Source Code
</a>
}
else
{
<span class="ui text red">Cannot Detect Source Code</span>
}
</span>
@if (VersionHelper.IsDirty)
{
<p>@Model.Translate(BaseLayoutStrings.GeneratedModified)</p>
Expand Down
64 changes: 42 additions & 22 deletions ProjectLighthouse/Helpers/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Linq;
using System.Text.RegularExpressions;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Logging;

namespace LBPUnion.ProjectLighthouse.Helpers;

public static class VersionHelper
public static partial class VersionHelper
{
static VersionHelper()
{
Expand All @@ -32,49 +33,68 @@ static VersionHelper()
}
catch
{
Logger.Error
(
"Project Lighthouse was built incorrectly. Please make sure git is available when building.",
LogArea.Startup
);
Logger.Error("Project Lighthouse was built incorrectly. Please make sure git is available when building.",
LogArea.Startup);
CommitHash = "invalid";
Branch = "invalid";
CanCheckForUpdates = false;
}

if (!IsDirty) return;

Logger.Warn
(
"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.",
LogArea.Startup
);
Logger.Warn("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.",
LogArea.Startup);
CanCheckForUpdates = false;
}

[GeneratedRegex(@"((git|ssh|http(s)?)|(git@[\w\.-]+))(:(\/\/)?)([\w\.@\:\/\-~]+)((\.git)(\/))?( .+)?")]
private static partial Regex GitRemoteRegex();

#nullable enable
/// <summary>
/// Determines the URL of the git remote.
/// </summary>
public static string? DetermineRemoteUrl()
{
string? remote = Remotes?.FirstOrDefault();
if (remote == null) return null;

Match match = GitRemoteRegex().Match(remote);

if (!match.Success || match.Groups.Count != 12) return null;

return match.Groups[7].Value;
}
#nullable disable

public static string CommitHash { get; set; }
public static string Branch { get; set; }
/// <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>
public static string FullRevision { get; set; }
private static string FullRevision { get; set; }
/// <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>
public static string EnvVer => $"{ServerConfiguration.Instance.Customization.EnvironmentName} {FullRevision}";
public static string FullVersion => $"Project Lighthouse {ServerConfiguration.Instance.Customization.EnvironmentName} {Branch}@{CommitHash} {Build}";
public static bool IsDirty => CommitHash.EndsWith("-dirty") || CommitsOutOfDate != 1 || CommitHash == "invalid" || Branch == "invalid";
public static string FullVersion =>
$"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 bool CanCheckForUpdates { get; set; }
public static string[] Remotes { get; set; }

public const string Build =
#if DEBUG
#if DEBUG
"Debug";
#elif RELEASE
#elif RELEASE
"Release";
#else
"Unknown";
#endif
#else
"Unknown";
#endif
}

0 comments on commit 248f141

Please sign in to comment.