Skip to content

Commit

Permalink
Merge pull request #932 from gordon-cs/s23-fix-githash
Browse files Browse the repository at this point in the history
re-implement git hash to version route
  • Loading branch information
russtuck authored Jul 10, 2023
2 parents b6e2819 + 655ee0d commit 3f39a66
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
22 changes: 13 additions & 9 deletions Gordon360/Controllers/VersionController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Linq;
using System.Reflection;
using Gordon360.Models.ViewModels;
using System;
using Gordon360.Extensions.System;

namespace Gordon360.Controllers
{
Expand All @@ -12,18 +15,19 @@ namespace Gordon360.Controllers
[Route("api/[controller]")]
public class VersionController : ControllerBase
{

[HttpGet]
[Route("")]
public ActionResult<string> Get()
public ActionResult<VersionViewModel> Get()
{
string gitVersion = string.Empty;
using (Stream stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("Gordon360." + "version.txt"))
using (StreamReader reader = new StreamReader(stream))
var asm = typeof(VersionController).Assembly;
var attrs = asm.GetCustomAttributes<AssemblyMetadataAttribute>();
DateTime bt = DateTime.Parse(attrs.FirstOrDefault(a => a.Key == "BuildTime")?.Value);
return new VersionViewModel
{
gitVersion = reader.ReadLine();
}
return Ok(gitVersion);
GitHash = attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value,
BuildTime = bt.ToString("yyyy/MM/dd HH:mm:ss UTC"),
};
}
}
}
40 changes: 39 additions & 1 deletion Gordon360/Gordon360.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<Target Name="GetGitHash" BeforeTargets="WriteGitHash" Condition="'$(BuildHash)' == ''">
<PropertyGroup>
<VerFile>$(IntermediateOutputPath)gitver</VerFile>
<BuildFile>$(IntermediateOutputPath)buildtime</BuildFile>
</PropertyGroup>
<Exec Command="git -C $(ProjectDir) describe --long --always &gt; $(VerFile)" />
<Exec Command="echo $([System.DateTime]::UtcNow) &gt; $(BuildFile)" />
<ReadLinesFromFile File="$(VerFile)">
<Output TaskParameter="Lines" ItemName="GitVersion" />
</ReadLinesFromFile>
<ReadLinesFromFile File="$(BuildFile)">
<Output TaskParameter="Lines" ItemName="BuildTime" />
</ReadLinesFromFile>
<PropertyGroup>
<BuildHash>@(GitVersion)</BuildHash>
<BuildTime>@(BuildTime)</BuildTime>
</PropertyGroup>
</Target>

<Target Name="WriteGitHash" BeforeTargets="CoreCompile">
<PropertyGroup>
<CustomAssemblyInfoFile>$(IntermediateOutputPath)CustomAssemblyInfo.cs</CustomAssemblyInfoFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CustomAssemblyInfoFile)" />
</ItemGroup>
<ItemGroup>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(BuildHash)</_Parameter2>
</AssemblyAttributes>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>BuildTime</_Parameter1>
<_Parameter2>$(BuildTime)</_Parameter2>
</AssemblyAttributes>
</ItemGroup>
<WriteCodeFragment Language="C#" OutputFile="$(CustomAssemblyInfoFile)" AssemblyAttributes="@(AssemblyAttributes)" />
</Target>
<PropertyGroup>
<OutputType>Exe</OutputType>
<Configurations>Debug;Release</Configurations>
</PropertyGroup>
Expand Down
12 changes: 12 additions & 0 deletions Gordon360/Models/ViewModels/VersionViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Gordon360.Models.ViewModels
{
public class VersionViewModel
{
public string GitHash { get; set; }
public string BuildTime { get; set; }
}


}

0 comments on commit 3f39a66

Please sign in to comment.