mirror of
https://github.com/bitwarden/server
synced 2025-12-22 19:23:45 +00:00
Switch to using built in source link feature (#6297)
* Switch to using built in source link feature * Switch to using types assembly * Formatting * Make version retrieval safer * Add debug message * Apply suggestions from code review Co-authored-by: Matt Bishop <mbishop@bitwarden.com> --------- Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
This commit is contained in:
@@ -1,46 +1,46 @@
|
||||
// FIXME: Update this file to be null safe and then delete the line below
|
||||
#nullable disable
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Bit.Core.Utilities;
|
||||
|
||||
public static class AssemblyHelpers
|
||||
{
|
||||
private static readonly IEnumerable<AssemblyMetadataAttribute> _assemblyMetadataAttributes;
|
||||
private static readonly AssemblyInformationalVersionAttribute _assemblyInformationalVersionAttributes;
|
||||
private const string GIT_HASH_ASSEMBLY_KEY = "GitHash";
|
||||
private static string _version;
|
||||
private static string _gitHash;
|
||||
private static string? _version;
|
||||
private static string? _gitHash;
|
||||
|
||||
static AssemblyHelpers()
|
||||
{
|
||||
_assemblyMetadataAttributes = Assembly.GetEntryAssembly().GetCustomAttributes<AssemblyMetadataAttribute>();
|
||||
_assemblyInformationalVersionAttributes = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>();
|
||||
}
|
||||
|
||||
public static string GetVersion()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_version))
|
||||
var assemblyInformationalVersionAttribute = typeof(AssemblyHelpers).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
|
||||
if (assemblyInformationalVersionAttribute == null)
|
||||
{
|
||||
_version = _assemblyInformationalVersionAttributes.InformationalVersion;
|
||||
Debug.Fail("The AssemblyInformationalVersionAttribute is expected to exist in this assembly, possibly its generation was turned off.");
|
||||
return;
|
||||
}
|
||||
|
||||
var informationalVersion = assemblyInformationalVersionAttribute.InformationalVersion.AsSpan();
|
||||
|
||||
if (!informationalVersion.TrySplitBy('+', out var version, out var gitHash))
|
||||
{
|
||||
// Treat the whole thing as the version
|
||||
_version = informationalVersion.ToString();
|
||||
return;
|
||||
}
|
||||
|
||||
_version = version.ToString();
|
||||
if (gitHash.Length < 8)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_gitHash = gitHash[..8].ToString();
|
||||
}
|
||||
|
||||
public static string? GetVersion()
|
||||
{
|
||||
return _version;
|
||||
}
|
||||
|
||||
public static string GetGitHash()
|
||||
public static string? GetGitHash()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_gitHash))
|
||||
{
|
||||
try
|
||||
{
|
||||
_gitHash = _assemblyMetadataAttributes.Where(i => i.Key == GIT_HASH_ASSEMBLY_KEY).First().Value;
|
||||
}
|
||||
catch (Exception)
|
||||
{ }
|
||||
}
|
||||
|
||||
return _gitHash;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user