using System;
using System.Reflection;
using System.Text.RegularExpressions;
namespace FastGithub.Upgrade
{
///
/// 表示产品版本
///
public class ProductionVersion : IComparable
{
///
/// 版本
///
public Version Version { get; }
///
/// 子版本
///
public string SubVersion { get; }
///
/// 产品版本
///
///
///
public ProductionVersion(Version version, string subVersion)
{
this.Version = version;
this.SubVersion = subVersion;
}
///
/// 比较版本
///
///
///
public int CompareTo(ProductionVersion? other)
{
var x = this;
var y = other;
if (y == null)
{
return 1;
}
var value = x.Version.CompareTo(y.Version);
if (value == 0)
{
value = CompareSubVerson(x.SubVersion, y.SubVersion);
}
return value;
static int CompareSubVerson(string subX, string subY)
{
if (subX.Length == 0 && subY.Length == 0)
{
return 0;
}
if (subX.Length == 0)
{
return 1;
}
if (subY.Length == 0)
{
return -1;
}
return StringComparer.OrdinalIgnoreCase.Compare(subX, subY);
}
}
public override string ToString()
{
return $"{Version}{SubVersion}";
}
///
/// 解析
///
///
///
public static ProductionVersion Parse(string productionVersion)
{
const string VERSION = @"^\d+\.(\d+.){0,2}\d+";
var verion = Regex.Match(productionVersion, VERSION).Value;
var subVersion = productionVersion[verion.Length..];
return new ProductionVersion(Version.Parse(verion), subVersion);
}
///
/// 获取当前应用程序的产品版本
///
///
public static ProductionVersion? GetAppVersion()
{
var version = Assembly
.GetEntryAssembly()?
.GetCustomAttribute()?
.InformationalVersion;
return version == null ? null : Parse(version);
}
}
}