diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs index 0b1afedf..ca903afa 100644 --- a/Netch/Controllers/UpdateChecker.cs +++ b/Netch/Controllers/UpdateChecker.cs @@ -46,7 +46,7 @@ namespace Netch.Controllers var json = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(url)); var releases = JsonSerializer.Deserialize>(json)!; - LatestRelease = VersionUtil.GetLatestRelease(releases, isPreRelease); + LatestRelease = GetLatestRelease(releases, isPreRelease); Global.Logger.Info($"Github 最新发布版本: {LatestRelease.tag_name}"); if (VersionUtil.CompareVersion(LatestRelease.tag_name, Version) > 0) { @@ -104,5 +104,14 @@ namespace Netch.Controllers return sb.ToString(); } + + public static Release GetLatestRelease(IEnumerable releases, bool isPreRelease) + { + if (!isPreRelease) + releases = releases.Where(release => !release.prerelease); + + var ordered = releases.OrderByDescending(release => release.tag_name, new VersionUtil.VersionComparer()); + return ordered.ElementAt(0); + } } -} +} \ No newline at end of file diff --git a/Netch/Models/GitHubRelease/SuffixVersion.cs b/Netch/Models/GitHubRelease/SuffixVersion.cs index 92164863..caaf5f26 100644 --- a/Netch/Models/GitHubRelease/SuffixVersion.cs +++ b/Netch/Models/GitHubRelease/SuffixVersion.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Text.RegularExpressions; namespace Netch.Models.GitHubRelease { @@ -8,28 +9,53 @@ namespace Netch.Models.GitHubRelease { public Version Version { get; } - public string Suffix { get; } + public string? Suffix { get; } - public SuffixVersion(Version version, string suffix) + public int SuffixNum { get; } + + private SuffixVersion(Version version) + { + Version = version; + Suffix = null; + SuffixNum = 0; + } + + private SuffixVersion(Version version, string suffix, int suffixNum) { Version = version; Suffix = suffix; + SuffixNum = suffixNum; } - public static SuffixVersion Parse(string? input) + public static SuffixVersion Parse(string? value) { - if (input == null) - throw new ArgumentNullException(nameof(input)); + if (value == null) + throw new ArgumentNullException(nameof(value)); - var split = input.Split('-'); - var dotNetVersion = Version.Parse(split[0]); - var preRelease = split.ElementAtOrDefault(1) ?? string.Empty; + var strings = value.Split('-'); - return new SuffixVersion(dotNetVersion, preRelease); + var version = Version.Parse(strings[0]); + var suffix = strings.ElementAtOrDefault(1)?.Trim(); + switch (suffix) + { + case null: + return new SuffixVersion(version); + case "": + throw new Exception("suffix WhiteSpace"); + default: + { + var match = Regex.Match(suffix, @"(?\D+)(?\d+)"); + if (!match.Success) + throw new Exception(); + + return new SuffixVersion(version, match.Groups["suffix"].Value, int.Parse(match.Groups["num"].Value)); + } + } } - public static bool TryParse(string input, out SuffixVersion result) + public static bool TryParse(string? input, out SuffixVersion result) { + result = default; try { result = Parse(input); @@ -37,7 +63,6 @@ namespace Netch.Models.GitHubRelease } catch (Exception) { - result = default; return false; } } @@ -62,21 +87,22 @@ namespace Netch.Models.GitHubRelease if (versionComparison != 0) return versionComparison; - if (Suffix == string.Empty) - return other.Suffix == string.Empty ? 0 : 1; - - if (other.Suffix == string.Empty) - return -1; + var suffixExistComparison = (Suffix != null ? 1 : 0) - (other.Suffix != null ? 1 : 0); + if (suffixExistComparison != 0) + return suffixExistComparison; var suffixComparison = string.Compare(Suffix, other.Suffix, StringComparison.OrdinalIgnoreCase); - return suffixComparison; + if (suffixComparison != 0) + return suffixComparison; + + return SuffixNum - other.SuffixNum; } public override string ToString() { var s = Version.ToString(); - if (Suffix != string.Empty) - s += $"-{Suffix}"; + if (Suffix != null) + s += $"-{Suffix}{SuffixNum}"; return s; } diff --git a/Netch/Models/GitHubRelease/VersionComparer.cs b/Netch/Models/GitHubRelease/VersionComparer.cs deleted file mode 100644 index 2cd67630..00000000 --- a/Netch/Models/GitHubRelease/VersionComparer.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Netch.Models.GitHubRelease -{ - public class VersionComparer : IComparer - { - public int Compare(object? x, object? y) - { - if (x == null) - throw new ArgumentNullException(nameof(x)); - - if (y == null) - throw new ArgumentNullException(nameof(y)); - - return VersionUtil.CompareVersion(x.ToString(), y.ToString()); - } - } -} \ No newline at end of file diff --git a/Netch/Models/GitHubRelease/VersionUtil.cs b/Netch/Models/GitHubRelease/VersionUtil.cs index 39eb2f46..451a46c3 100644 --- a/Netch/Models/GitHubRelease/VersionUtil.cs +++ b/Netch/Models/GitHubRelease/VersionUtil.cs @@ -1,33 +1,37 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace Netch.Models.GitHubRelease { public static class VersionUtil { - public static Release GetLatestRelease(IEnumerable releases, bool isPreRelease) - { - if (!isPreRelease) - releases = releases.Where(release => !release.prerelease); + private static VersionComparer instance = new(); - releases = releases.Where(release => IsVersionString(release.tag_name)); - var ordered = releases.OrderByDescending(release => release.tag_name, new VersionComparer()); - return ordered.ElementAt(0); + public static int CompareVersion(string x, string y) + { + return instance.Compare(x, y); } - private static bool IsVersionString(string str) + public class VersionComparer : IComparer { - return SuffixVersion.TryParse(str, out _); - } + /// + /// Greater than 0 newer + /// + /// + /// + /// + public int Compare(string? x, string? y) + { + var xResult = SuffixVersion.TryParse(x, out var version1) ? 1 : 0; + var yResult = SuffixVersion.TryParse(y, out var version2) ? 1 : 0; - /// =0:versions are equal - /// >0:version1 is greater - /// <0:version2 is greater - public static int CompareVersion(string? v1, string? v2) - { - var version1 = SuffixVersion.Parse(v1); - var version2 = SuffixVersion.Parse(v2); - return version1.CompareTo(version2); + var parseResult = xResult - yResult; + if (parseResult != 0) + return parseResult; + + return version1.CompareTo(version2); + } } } } \ No newline at end of file