Skip to content

Commit 03ac81a

Browse files
committed
feat: support proper sorting for prereleases
1 parent 7f18300 commit 03ac81a

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

updater/scripts/sort-versions.ps1

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,14 @@ param(
44

55
Set-StrictMode -Version latest
66

7-
function GetComparablePart([Parameter(Mandatory = $true)][string] $value)
8-
{
9-
$value.PadLeft(10, '0')
10-
}
11-
127
function GetComparableVersion([Parameter(Mandatory = $true)][string] $value)
138
{
149
$value = $value -replace '^v', ''
15-
$output = ''
16-
$buffer = ''
17-
for ($i = 0; $i -lt $value.Length; $i++)
18-
{
19-
$char = $value[$i]
20-
if ("$char" -match '[^a-zA-Z0-9]')
21-
{
22-
# Found a separtor, update the current buffer
23-
$output += GetComparablePart $buffer
24-
$output += $char
25-
$buffer = ''
26-
}
27-
else
28-
{
29-
$buffer += $char
30-
}
10+
try {
11+
[System.Management.Automation.SemanticVersion]::Parse($value)
12+
} catch {
13+
Write-Error "Failed to parse semantic version '$value': $_"
3114
}
32-
if ($buffer.Length -gt 0)
33-
{
34-
$output += GetComparablePart $buffer
35-
}
36-
37-
$output
3815
}
3916

4017
$List | Sort-Object -Property @{Expression = { GetComparableVersion $_ } }

updater/tests/sort-versions.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ RunTest "sort standard versions v2" {
2020
AssertEqual @('v1.2.3', '3.0.0', '5.4.1', '5.4.11', '5.5', 'v6.0') $sorted
2121
}
2222

23-
# TODO, currently doesn't respect (order) stuff like RC, Beta, etc.
24-
# RunTest "sort with pre-releases" {
25-
# $sorted = SortVersions @('3.0.0', '5.4.11', 'v1.2.3', '5.4.1', '5.4.11-rc.0')
26-
# AssertEqual @('v1.2.3', '3.0.0', '5.4.1', '5.4.11-rc.0', '5.4.11') $sorted
27-
# }
23+
# https://semver.org/#spec-item-11
24+
RunTest "sort with pre-releases" {
25+
$sorted = SortVersions @('1.0.0-rc.1', '1.0.0', '1.0.0-beta.11', '1.0.0-alpha.1', '1.0.0-beta', '1.0.0-alpha.beta', '1.0.0-alpha', '1.0.0-beta.2')
26+
AssertEqual @('1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-alpha.beta', '1.0.0-beta', '1.0.0-beta.2', '1.0.0-beta.11', '1.0.0-rc.1', '1.0.0') $sorted
27+
}

0 commit comments

Comments
 (0)