1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00
Teknik/Utilities/TagHelpers/VersionHelper.cs
2021-11-15 20:17:42 -08:00

48 lines
1.5 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
namespace Teknik.Utilities.TagHelpers
{
[HtmlTargetElement("version")]
public class VersionHelper : TagHelper
{
private const string _verFile = "version.json";
private readonly IWebHostEnvironment _env;
public string Source { get; set; }
public VersionHelper(IWebHostEnvironment env)
{
_env = env;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
// Clear the initial wrap tag
output.TagName = string.Empty;
// Get the version file info
string dataDir = (string)AppDomain.CurrentDomain.GetData("DataDirectory");
string fullPath = Path.Combine(dataDir, _verFile);
if (File.Exists(fullPath))
{
using (StreamReader file = File.OpenText(fullPath))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject res = (JObject)JToken.ReadFrom(reader);
string commitVer = res["version"].ToString();
string commitHash = res["hash"].ToString();
output.Content.AppendHtml($"Version: {commitVer} - Hash: <a href=\"{Source}{commitHash}\">{commitHash.Truncate(10)}</a> | {_env.EnvironmentName}");
}
}
}
}
}