diff --git a/eng/common/scripts/SemVer.ps1 b/eng/common/scripts/SemVer.ps1 index 6459f252a67..689a70e778c 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,41 @@ class AzureEngSemanticVersion : IComparable { Write-Host "Error: Python beta string did not correctly increment" } + $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.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.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()) { + Write-Host "Error: version string did not correctly increment. Expected: $expected, Actual: $version" + } + Write-Host "QuickTests done" } -} +} \ No newline at end of file