fix(releases): strip prerelease and build metadata before version compare
System.Version can't parse SemVer prerelease ("-alpha") or MinVer build
metadata ("+sha") suffixes, so an installed 1.0.2-alpha was treated as
unparseable. Reduce both sides to their numeric core before comparing.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -6,13 +6,16 @@ public static class VersionComparer
|
||||
{
|
||||
public static VersionCompareResult Compare(string latest, string current)
|
||||
{
|
||||
var latestTrimmed = (latest ?? "").TrimStart('v', 'V');
|
||||
var currentTrimmed = (current ?? "").TrimStart('v', 'V');
|
||||
|
||||
var unparseable = !Version.TryParse(latestTrimmed, out var lv)
|
||||
| !Version.TryParse(currentTrimmed, out var cv);
|
||||
var unparseable = !Version.TryParse(CoreVersion(latest), out var lv)
|
||||
| !Version.TryParse(CoreVersion(current), out var cv);
|
||||
|
||||
if (unparseable) return new VersionCompareResult(false, true);
|
||||
return new VersionCompareResult(lv > cv, false);
|
||||
}
|
||||
|
||||
// Reduce a tag/version to its numeric core: drop a leading "v", MinVer build
|
||||
// metadata ("+sha"), and any SemVer prerelease suffix ("-alpha") — none of
|
||||
// which System.Version can parse. So "v1.0.2-alpha+abc" -> "1.0.2".
|
||||
private static string CoreVersion(string value)
|
||||
=> (value ?? "").TrimStart('v', 'V').Split('+')[0].Split('-')[0];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user