64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace Kreta.Core
|
|
{
|
|
public sealed class KretaVersion
|
|
{
|
|
private KretaVersion()
|
|
{
|
|
}
|
|
|
|
private static readonly Lazy<VersionInfo> VersionInfo = new Lazy<VersionInfo>(GetVersionInfo);
|
|
|
|
public static VersionInfo Instance => VersionInfo.Value;
|
|
|
|
private static VersionInfo GetVersionInfo()
|
|
{
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
string assemblyLocation = assembly.Location;
|
|
|
|
DateTime assemblyCreationDateTime = File.GetCreationTime(assemblyLocation);
|
|
var result = new VersionInfo
|
|
{
|
|
AssemblyCreationDateTime = assemblyCreationDateTime
|
|
};
|
|
|
|
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assemblyLocation);
|
|
|
|
string mainVersion = fileVersionInfo.FileVersion;
|
|
if (!string.IsNullOrWhiteSpace(mainVersion))
|
|
{
|
|
result.MainVersion = mainVersion;
|
|
}
|
|
|
|
int fileMajorPart = fileVersionInfo.FileMajorPart;
|
|
int fileMinorPart = fileVersionInfo.FileMinorPart;
|
|
if (fileMajorPart > 0 && fileMinorPart >= 0)
|
|
{
|
|
result.ShortMainVersion = $"{fileMajorPart}.{fileMinorPart}";
|
|
}
|
|
|
|
List<AssemblyMetadataAttribute> customAttributes = assembly.GetCustomAttributes().OfType<AssemblyMetadataAttribute>().ToList();
|
|
|
|
string branchName = customAttributes.SingleOrDefault(x => x.Key == Constants.Version.BranchName)?.Value;
|
|
if (!string.IsNullOrWhiteSpace(branchName))
|
|
{
|
|
result.BranchName = branchName;
|
|
}
|
|
|
|
string commitNumber = customAttributes.SingleOrDefault(x => x.Key == Constants.Version.CommitNumber)?.Value;
|
|
if (!string.IsNullOrWhiteSpace(commitNumber))
|
|
{
|
|
result.CommitNumber = commitNumber;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|