Skip to content

Instantly share code, notes, and snippets.

@ElanHasson
Last active August 12, 2022 19:06
Show Gist options
  • Select an option

  • Save ElanHasson/30384431651534b215e754a554b5b6ae to your computer and use it in GitHub Desktop.

Select an option

Save ElanHasson/30384431651534b215e754a554b5b6ae to your computer and use it in GitHub Desktop.

Revisions

  1. ElanHasson revised this gist Aug 12, 2022. 2 changed files with 19 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions AssemblyInformation.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    using System.Reflection;

    public record class AssemblyInformation(string Product, string Description, string Version, string InformationalVersion)
    {
    public static readonly AssemblyInformation Current = new(typeof(AssemblyInformation).Assembly);

    public AssemblyInformation(Assembly assembly)
    : this(
    assembly.GetCustomAttribute<AssemblyProductAttribute>()!.Product,
    assembly.GetCustomAttribute<AssemblyDescriptionAttribute>()!.Description,
    assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()!.Version,
    assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion)
    {
    }
    }
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    This gets the commit hash from the current `HEAD` of the branch.

    You can use it in your applcation via `AssemblyInformation.Current.InformationalVersion`.

  2. ElanHasson created this gist Aug 12, 2022.
    33 changes: 33 additions & 0 deletions Directory.Build.props
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <Project>
    <Choose>
    <When Condition="'$(CI)' == 'true'">
    <!-- On non-official builds we don't burn in a git sha. In large part because it
    hurts our determinism efforts as binaries which should be the same between
    builds will not (due to developers building against different HEAD
    values -->
    <PropertyGroup>
    <GitHeadSha>&lt;developer build&gt;</GitHeadSha>
    </PropertyGroup>
    </When>
    <When Condition="'$(GITHUB_SHA)' != ''">
    <PropertyGroup>
    <GitHeadSha>$(GITHUB_SHA)</GitHeadSha>
    </PropertyGroup>
    </When>
    <When Condition="'$(GitHeadSha)' == ''">
    <PropertyGroup>
    <GitHeadSha>Not found</GitHeadSha>
    <DotGitDir>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory).git'))</DotGitDir>
    <HeadFileContent Condition="Exists('$(DotGitDir)/HEAD')">$([System.IO.File]::ReadAllText('$(DotGitDir)/HEAD').Trim())</HeadFileContent>
    <RefPath Condition="$(HeadFileContent.StartsWith('ref: '))">$(DotGitDir)/$(HeadFileContent.Substring(5))</RefPath>
    <GitHeadSha Condition="'$(RefPath)' != '' AND Exists('$(RefPath)')">$([System.IO.File]::ReadAllText('$(RefPath)').Trim())</GitHeadSha>
    <GitHeadSha Condition="'$(HeadFileContent)' != '' AND '$(RefPath)' == ''">$(HeadFileContent)</GitHeadSha>
    </PropertyGroup>
    </When>
    <Otherwise>
    <PropertyGroup>
    <GitHeadSha>Not found</GitHeadSha>
    </PropertyGroup>
    </Otherwise>
    </Choose>
    </Project>
    5 changes: 5 additions & 0 deletions Directory.Build.targets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <Project>
    <PropertyGroup>
    <InformationalVersion>$(GitHeadSha)</InformationalVersion>
    </PropertyGroup>
    </Project>