Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync eng/common directory with azure-sdk-tools repository #10761

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions eng/common/scripts/SemVer.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<#
.DESCRIPTION
Parses a semver version string into its components and supports operations around it.
Parses a semver version string into its components and supports operations around it that we use for versioning our packages.

See https://azure.github.io/azure-sdk/policies_releases.html#package-versioning

Example: 1.2.3-preview.4
Components: Major.Minor.Patch-PrereleaseLabel.PrereleaseNumber

Note: A builtin Powershell version of SemVer exists in 'System.Management.Automation'. At this time, it does not parsing of PrereleaseNumber. It's name is also type accelerated to 'SemVer'.
#>

class SemVer {
class AzureEngSemanticVersion {
[int] $Major
[int] $Minor
[int] $Patch
Expand All @@ -14,9 +19,18 @@ class SemVer {
[bool] $IsPrerelease
[string] $RawVersion

SemVer(
[string] $versionString
){

static [AzureEngSemanticVersion] ParseVersionString([string] $versionString)
{
try {
return [AzureEngSemanticVersion]::new($versionString)
}
catch {
return $null
}
}

AzureEngSemanticVersion([string] $versionString){
# Regex inspired but simplifie from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
$SEMVER_REGEX = "^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-?(?<prelabel>[a-zA-Z-]*)(?:\.?(?<prenumber>0|[1-9]\d*)))?$"

Expand All @@ -33,7 +47,6 @@ class SemVer {
$isPre = $true;
}


$this.Major = [int]$matches.Major
$this.Minor = [int]$matches.Minor
$this.Patch = [int]$matches.Patch
Expand All @@ -44,10 +57,27 @@ class SemVer {
}
else
{
throw "Invalid version string: $versionString"
throw "Invalid version string: '$versionString'"
}
}

# If a prerelease label exists, it must be 'preview', and similar semantics used in our release guidelines
# See https://azure.github.io/azure-sdk/policies_releases.html#package-versioning
[bool] HasValidPrereleaseLabel(){
if ($this.IsPrerelease -eq $true) {
if ($this.PrereleaseLabel -ne 'preview') {
Write-Error "Unexpected pre-release identifier '$this.PrereleaseLabel', should be 'preview'"
return $false;
}
if ($this.PrereleaseNumber -lt 1)
{
Write-Error "Unexpected pre-release version '$this.PrereleaseNumber', should be >= '1'"
return $false;
}
}
return $true;
}

[string] ToString(){
if ($this.IsPrerelease -eq $false)
{
Expand All @@ -59,7 +89,6 @@ class SemVer {
}
return $versionString;
}


[void] IncrementAndSetToPrerelease(){
if ($this.IsPrerelease -eq $false)
Expand Down