Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools repository (#10761)
Browse files Browse the repository at this point in the history
  • Loading branch information
azure-sdk authored Apr 9, 2020
1 parent cce69b6 commit 3a6676a
Showing 1 changed file with 37 additions and 8 deletions.
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

0 comments on commit 3a6676a

Please sign in to comment.