From 8f6c9e6aeb82cee66f1df65bd6535163bd7f7389 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Tue, 21 Jan 2025 10:55:50 -0800 Subject: [PATCH 1/2] Support incrementing semver prereleases with 'zero' versions --- eng/common/scripts/SemVer.ps1 | 104 +++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 19 deletions(-) diff --git a/eng/common/scripts/SemVer.ps1 b/eng/common/scripts/SemVer.ps1 index 6459f252a67c..50c75e944533 100644 --- a/eng/common/scripts/SemVer.ps1 +++ b/eng/common/scripts/SemVer.ps1 @@ -78,13 +78,10 @@ class AzureEngSemanticVersion : IComparable { if ($null -eq $matches['prelabel']) { - # artifically provide these values for non-prereleases to enable easy sorting of them later than prereleases. - $this.PrereleaseLabel = "zzz" - $this.PrereleaseNumber = 99999999 $this.IsPrerelease = $false $this.VersionType = "GA" if ($this.Major -eq 0) { - # Treat initial 0 versions as a prerelease beta's + # Treat initial 0 versions as a prerelease beta's $this.VersionType = "Beta" $this.IsPrerelease = $true } @@ -116,7 +113,7 @@ class AzureEngSemanticVersion : IComparable { # See https://azure.github.io/azure-sdk/policies_releases.html#package-versioning [bool] HasValidPrereleaseLabel() { - if ($this.IsPrerelease -eq $true) { + if ($this.PrereleaseLabel) { if ($this.PrereleaseLabel -ne $this.DefaultPrereleaseLabel -and $this.PrereleaseLabel -ne $this.DefaultAlphaReleaseLabel) { Write-Host "Unexpected pre-release identifier '$($this.PrereleaseLabel)', "` "should be '$($this.DefaultPrereleaseLabel)' or '$($this.DefaultAlphaReleaseLabel)'" @@ -136,7 +133,7 @@ class AzureEngSemanticVersion : IComparable { { $versionString = "{0}.{1}.{2}" -F $this.Major, $this.Minor, $this.Patch - if ($this.IsPrerelease -and $this.PrereleaseLabel -ne "zzz") + if ($this.PrereleaseLabel) { $versionString += $this.PrereleaseLabelSeparator + $this.PrereleaseLabel + ` $this.PrereleaseNumberSeparator + $this.PrereleaseNumber @@ -147,24 +144,42 @@ class AzureEngSemanticVersion : IComparable { return $versionString; } - [void] IncrementAndSetToPrerelease() { - if ($this.IsPrerelease -eq $false) + [void] IncrementAndSetToPrerelease($Segment) { + if ($this.BuildNumber) { - $this.PrereleaseLabel = $this.DefaultPrereleaseLabel - $this.PrereleaseNumber = 1 - $this.Minor++ - $this.Patch = 0 - $this.IsPrerelease = $true + throw "Cannot increment releases tagged with azure pipelines build numbers" + } + + if ($this.PrereleaseLabel) + { + $this.PrereleaseNumber++ } else { - if ($this.BuildNumber) { - throw "Cannot increment releases tagged with azure pipelines build numbers" + $this.$Segment++ + if($Segment -eq "Major") { + $this.Minor = 0 + $this.Patch = 0 } - $this.PrereleaseNumber++ + if($Segment -eq "Minor") { + $this.Patch = 0 + } + + # If the major version is 0, we don't need a prerelease label + if ($this.Major -ne 0) + { + $this.PrereleaseLabel = $this.DefaultPrereleaseLabel + $this.PrereleaseNumber = 1 + } + + $this.IsPrerelease = $true } } + [void] IncrementAndSetToPrerelease() { + $this.IncrementAndSetToPrerelease("Minor") + } + [void] SetupPythonConventions() { # Python uses no separators and "b" for beta so this sets up the the object to work with those conventions @@ -198,11 +213,30 @@ class AzureEngSemanticVersion : IComparable { $ret = $this.Patch.CompareTo($other.Patch) if ($ret) { return $ret } + # provide artificial prerelease values for non-prereleases to sort them later than prereleases. + if ($this.PrereleaseLabel) { + $thisPrereleaseLabel = $this.PrereleaseLabel + $thisPrereleaseNumber = $this.PrereleaseNumber + } + else { + $thisPrereleaseLabel = "zzz" + $thisPrereleaseNumber = 99999999 + } + + if ($other.PrereleaseLabel) { + $otherPrereleaseLabel = $other.PrereleaseLabel + $otherPrereleaseNumber = $other.PrereleaseNumber + } + else { + $otherPrereleaseLabel = "zzz" + $otherPrereleaseNumber = 99999999 + } + # Mimic PowerShell that uses case-insensitive comparisons by default. - $ret = [string]::Compare($this.PrereleaseLabel, $other.PrereleaseLabel, $true) + $ret = [string]::Compare($thisPrereleaseLabel, $otherPrereleaseLabel, $true) if ($ret) { return $ret } - $ret = $this.PrereleaseNumber.CompareTo($other.PrereleaseNumber) + $ret = $thisPrereleaseNumber.CompareTo($otherPrereleaseNumber) if ($ret) { return $ret } return ([int] $this.BuildNumber).CompareTo([int] $other.BuildNumber) @@ -360,6 +394,38 @@ class AzureEngSemanticVersion : IComparable { Write-Host "Error: Python beta string did not correctly increment" } + $versionString = "0.1.2" + $version = [AzureEngSemanticVersion]::ParseVersionString($versionString) + $version.IncrementAndSetToPrerelease() + $expected = "0.2.0" + if ($expected -ne $version.ToString()) { + Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" + } + + $version.IncrementAndSetToPrerelease("patch") + $expected = "0.2.1" + if ($expected -ne $version.ToString()) { + Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" + } + + $version.IncrementAndSetToPrerelease("minor") + $expected = "0.3.0" + if ($expected -ne $version.ToString()) { + Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" + } + + $version.IncrementAndSetToPrerelease("major") + $expected = "1.0.0-beta.1" + if ($expected -ne $version.ToString()) { + Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" + } + + $version.IncrementAndSetToPrerelease() + $expected = "1.0.0-beta.2" + if ($expected -ne $version.ToString()) { + Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" + } + Write-Host "QuickTests done" } -} +} \ No newline at end of file From 1088516150fbaa5a72639c85c6094c38d8fb1f16 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Tue, 21 Jan 2025 13:47:44 -0800 Subject: [PATCH 2/2] Make tests more explicit --- eng/common/scripts/SemVer.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/eng/common/scripts/SemVer.ps1 b/eng/common/scripts/SemVer.ps1 index 50c75e944533..689a70e778c4 100644 --- a/eng/common/scripts/SemVer.ps1 +++ b/eng/common/scripts/SemVer.ps1 @@ -394,32 +394,35 @@ class AzureEngSemanticVersion : IComparable { Write-Host "Error: Python beta string did not correctly increment" } - $versionString = "0.1.2" - $version = [AzureEngSemanticVersion]::ParseVersionString($versionString) + $version = [AzureEngSemanticVersion]::ParseVersionString("0.1.2") $version.IncrementAndSetToPrerelease() $expected = "0.2.0" if ($expected -ne $version.ToString()) { Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" } + $version = [AzureEngSemanticVersion]::ParseVersionString("0.1.2") $version.IncrementAndSetToPrerelease("patch") - $expected = "0.2.1" + $expected = "0.1.3" if ($expected -ne $version.ToString()) { Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" } + $version = [AzureEngSemanticVersion]::ParseVersionString("0.1.2") $version.IncrementAndSetToPrerelease("minor") - $expected = "0.3.0" + $expected = "0.2.0" if ($expected -ne $version.ToString()) { Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" } + $version = [AzureEngSemanticVersion]::ParseVersionString("0.1.2") $version.IncrementAndSetToPrerelease("major") $expected = "1.0.0-beta.1" if ($expected -ne $version.ToString()) { Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" } + $version = [AzureEngSemanticVersion]::ParseVersionString("1.0.0-beta.1") $version.IncrementAndSetToPrerelease() $expected = "1.0.0-beta.2" if ($expected -ne $version.ToString()) {