This commit is contained in:
Pilzinsel64
2025-12-09 08:52:40 +01:00
parent 061eccf846
commit 7e21064b8b
3 changed files with 79 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Text.RegularExpressions;
namespace Pilz; namespace Pilz;
@@ -29,13 +30,26 @@ public class AppVersion(Version version, int build, AppChannel channel) : ICompa
{ {
string version = Version.ToString(); string version = Version.ToString();
if (Channel != AppChannel.Stable || Build != 1)
version += "-" + Channel.ToString().ToLowerInvariant();
if (Build != 1)
version += "." + Build;
return version;
}
public string ToHumanString()
{
string version = Version.ToString();
if (Channel != AppChannel.Stable || Build != 1) if (Channel != AppChannel.Stable || Build != 1)
version = $"{version} {Channel} {Build}"; version = $"{version} {Channel} {Build}";
return version; return version;
} }
public virtual string ToString(int fieldCount) public virtual string ToHumanString(int fieldCount)
{ {
string version = Version.ToString(fieldCount); string version = Version.ToString(fieldCount);
@@ -45,7 +59,7 @@ public class AppVersion(Version version, int build, AppChannel channel) : ICompa
return version; return version;
} }
public virtual string ToShortString() public virtual string ToShortHumanString()
{ {
string version; string version;
@@ -92,22 +106,70 @@ public class AppVersion(Version version, int build, AppChannel channel) : ICompa
public static AppVersion Parse(string input) public static AppVersion Parse(string input)
{ {
if (string.IsNullOrWhiteSpace(input)) if (string.IsNullOrWhiteSpace(input))
throw new FormatException(); throw new FormatException("App version is empty!");
// Remove version prefix
if (input.StartsWith("version", StringComparison.InvariantCultureIgnoreCase)) if (input.StartsWith("version", StringComparison.InvariantCultureIgnoreCase))
input = input.Substring(7); input = input.Substring(7);
else if (input.StartsWith("v", StringComparison.InvariantCultureIgnoreCase))
input = input.Substring(1);
var splitted = input.Trim().Split(' '); string? buildStr;
string? channelStr;
string? versionStr;
if (splitted.Length < 1 || !Version.TryParse(splitted[0], out Version? version) || version == null) // 1.14.2-beta.1
throw new FormatException(); if (Regex.Match(input, @"(?<v>[0-9.]+)-(?<c>[a-zA-Z]+)\.(?<n>\d+)$") is { Success: true } matchTN)
{
versionStr = matchTN.Groups["v"].Value;
channelStr = matchTN.Groups["c"].Value;
buildStr = matchTN.Groups["b"].Value;
}
// 1.14.2-beta
else if (Regex.Match(input, @"(?<v>[0-9.]+)-(?<c>[a-zA-Z]+)$") is { Success: true } matchT)
{
versionStr = matchT.Groups["v"].Value;
channelStr = matchT.Groups["c"].Value;
buildStr = null;
}
// 1.14.2 Beta 1
else if (Regex.Match(input, @"(?<v>[0-9.]+)-(?<c>[a-zA-Z]+)\.(?<n>\d+)$") is { Success: true } matchDN)
{
versionStr = matchDN.Groups["v"].Value;
channelStr = matchDN.Groups["c"].Value;
buildStr = matchDN.Groups["b"].Value;
}
// 1.14.2 Beta
else if (Regex.Match(input, @"(?<v>[0-9.]+)-(?<c>[a-zA-Z]+)$") is { Success: true } matchD)
{
versionStr = matchD.Groups["v"].Value;
channelStr = matchD.Groups["c"].Value;
buildStr = null;
}
// 1.14.2
else if (Regex.Match(input, @"(?<v>[0-9.]+)$") is { Success: true } matchV)
{
versionStr = matchV.Groups["v"].Value;
channelStr = null;
buildStr = null;
}
// Invalid format
else
throw new FormatException("Bad app version format! " + input);
if (splitted.Length < 2 || !Enum.TryParse(splitted[1], out AppChannel channel)) // Parse version
if (!Version.TryParse(versionStr, out Version? version) || version == null)
throw new FormatException("Bad version format: " + versionStr);
// Parse channel
if (!Enum.TryParse(channelStr, out AppChannel channel))
channel = AppChannel.Stable; channel = AppChannel.Stable;
if (splitted.Length < 3 || !int.TryParse(splitted[2], out int build)) // Parse build
if (!int.TryParse(buildStr, out var build))
build = 1; build = 1;
// Return new appversin
return new AppVersion(version, build, channel); return new AppVersion(version, build, channel);
} }

View File

@@ -5,21 +5,25 @@ namespace Pilz;
[AttributeUsage(AttributeTargets.Assembly)] [AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyAppVersionAttribute : Attribute public class AssemblyAppVersionAttribute : Attribute
{ {
public const string EntryAssemblyVersionKey = "{EntryAssemblyVersion}";
public AppVersion Version { get; } public AppVersion Version { get; }
public AssemblyAppVersionAttribute()
{
Version = AppVersion.Parse(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
}
[Obsolete("Use parameterless constructor and Version attribute instead.")]
public AssemblyAppVersionAttribute(string version, int build, AppChannel channel) public AssemblyAppVersionAttribute(string version, int build, AppChannel channel)
{ {
if (version.Contains(EntryAssemblyVersionKey))
version = version.Replace(EntryAssemblyVersionKey, Assembly.GetEntryAssembly().GetName().Version.ToString());
Version = new(new Version(version), build, channel); Version = new(new Version(version), build, channel);
} }
[Obsolete("Use parameterless constructor and Version attribute instead.")]
public AssemblyAppVersionAttribute(string version) : this(version, 1) public AssemblyAppVersionAttribute(string version) : this(version, 1)
{ {
} }
[Obsolete("Use parameterless constructor and Version attribute instead.")]
public AssemblyAppVersionAttribute(string version, int build) : this(version, build, AppChannel.Stable) public AssemblyAppVersionAttribute(string version, int build) : this(version, build, AppChannel.Stable)
{ {
} }

View File

@@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable> <Nullable>annotations</Nullable>
<Version>2.6.2</Version> <Version>2.7.0</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">