diff --git a/Pilz/AppVersion.cs b/Pilz/AppVersion.cs index db9530c..1b8f50d 100644 --- a/Pilz/AppVersion.cs +++ b/Pilz/AppVersion.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using System.Text.RegularExpressions; namespace Pilz; @@ -29,13 +30,26 @@ public class AppVersion(Version version, int build, AppChannel channel) : ICompa { 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) version = $"{version} {Channel} {Build}"; return version; } - public virtual string ToString(int fieldCount) + public virtual string ToHumanString(int fieldCount) { string version = Version.ToString(fieldCount); @@ -45,7 +59,7 @@ public class AppVersion(Version version, int build, AppChannel channel) : ICompa return version; } - public virtual string ToShortString() + public virtual string ToShortHumanString() { string version; @@ -92,22 +106,70 @@ public class AppVersion(Version version, int build, AppChannel channel) : ICompa public static AppVersion Parse(string input) { if (string.IsNullOrWhiteSpace(input)) - throw new FormatException(); + throw new FormatException("App version is empty!"); + // Remove version prefix if (input.StartsWith("version", StringComparison.InvariantCultureIgnoreCase)) 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) - throw new FormatException(); + // 1.14.2-beta.1 + if (Regex.Match(input, @"(?[0-9.]+)-(?[a-zA-Z]+)\.(?\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, @"(?[0-9.]+)-(?[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, @"(?[0-9.]+)-(?[a-zA-Z]+)\.(?\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, @"(?[0-9.]+)-(?[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, @"(?[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; - if (splitted.Length < 3 || !int.TryParse(splitted[2], out int build)) + // Parse build + if (!int.TryParse(buildStr, out var build)) build = 1; + // Return new appversin return new AppVersion(version, build, channel); } diff --git a/Pilz/AssemblyAppVersion.cs b/Pilz/AssemblyAppVersion.cs index 9f65787..08081f2 100644 --- a/Pilz/AssemblyAppVersion.cs +++ b/Pilz/AssemblyAppVersion.cs @@ -5,21 +5,25 @@ namespace Pilz; [AttributeUsage(AttributeTargets.Assembly)] public class AssemblyAppVersionAttribute : Attribute { - public const string EntryAssemblyVersionKey = "{EntryAssemblyVersion}"; - public AppVersion Version { get; } + public AssemblyAppVersionAttribute() + { + Version = AppVersion.Parse(Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion); + } + + [Obsolete("Use parameterless constructor and Version attribute instead.")] 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); } + [Obsolete("Use parameterless constructor and Version attribute instead.")] 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) { } diff --git a/Pilz/Pilz.csproj b/Pilz/Pilz.csproj index 00c9d20..70a73ea 100644 --- a/Pilz/Pilz.csproj +++ b/Pilz/Pilz.csproj @@ -5,7 +5,7 @@ latest enable annotations - 2.6.2 + 2.7.0