forked from majkinetor/au
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle multiple streams as per majkinetor#74
- Loading branch information
1 parent
696bf40
commit 7201c2b
Showing
8 changed files
with
271 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
class AUVersion : System.IComparable { | ||
[version] $Version | ||
[string] $Prerelease | ||
[string] $BuildMetadata | ||
|
||
AUVersion([version] $version, [string] $prerelease, [string] $buildMetadata) { | ||
if (!$version) { throw 'Version cannot be null.' } | ||
$this.Version = $version | ||
$this.Prerelease = $prerelease | ||
$this.BuildMetadata = $buildMetadata | ||
} | ||
|
||
static [AUVersion] Parse([string] $input) { return [AUVersion]::Parse($input, $true) } | ||
|
||
static [AUVersion] Parse([string] $input, [bool] $strict) { | ||
if (!$input) { throw 'Version cannot be null.' } | ||
$reference = [ref] $null | ||
if (![AUVersion]::TryParse($input, $reference, $strict)) { throw "Invalid version: $input." } | ||
return $reference.Value | ||
} | ||
|
||
static [bool] TryParse([string] $input, [ref] $result) { return [AUVersion]::TryParse($input, $result, $true) } | ||
|
||
static [bool] TryParse([string] $input, [ref] $result, [bool] $strict) { | ||
$result.Value = [AUVersion] $null | ||
if (!$input) { return $false } | ||
$pattern = [AUVersion]::GetPattern($strict) | ||
if ($input -notmatch $pattern) { return $false } | ||
$reference = [ref] $null | ||
if (![version]::TryParse($Matches['version'], $reference)) { return $false } | ||
$result.Value = [AUVersion]::new($reference.Value, $Matches['prerelease'], $Matches['buildMetadata']) | ||
return $true | ||
} | ||
|
||
hidden static [string] GetPattern([bool] $strict) { | ||
$versionPattern = '(?<version>\d+(?:\.\d+){0,3})' | ||
$identifierPattern = "[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*" | ||
if ($strict) { | ||
return "^$versionPattern(?:-(?<prerelease>$identifierPattern))?(?:\+(?<buildMetadata>$identifierPattern))?`$" | ||
} else { | ||
return "^v?\s*$versionPattern(?:-?(?<prerelease>$identifierPattern))?(?:\+(?<buildMetadata>$identifierPattern))?\s*`$" | ||
} | ||
} | ||
|
||
[int] CompareTo($obj) { | ||
if ($obj -eq $null) { return 1 } | ||
if ($obj -isnot [AUVersion]) { throw "AUVersion expected: $($obj.GetType())" } | ||
if ($obj.Version -ne $this.Version) { return $this.Version.CompareTo($obj.Version) } | ||
if ($obj.Prerelease -and $this.Prerelease) { return $this.Prerelease.CompareTo($obj.Prerelease) } | ||
if (!$obj.Prerelease -and !$this.Prerelease) { return 0 } | ||
if ($obj.Prerelease) { return 1 } | ||
return -1 | ||
} | ||
|
||
[bool] Equals($obj) { return $obj -is [AUVersion] -and $obj -and $this.ToString().Equals($obj.ToString()) } | ||
|
||
[int] GetHashCode() { return $this.ToString().GetHashCode() } | ||
|
||
[string] ToString() { | ||
$result = $this.Version.ToString() | ||
if ($this.Prerelease) { $result += "-$($this.Prerelease)" } | ||
if ($this.BuildMetadata) { $result += "+$($this.BuildMetadata)" } | ||
return $result | ||
} | ||
|
||
[string] ToString([int] $fieldCount) { | ||
if ($fieldCount -eq -1) { return $this.Version.ToString() } | ||
return $this.Version.ToString($fieldCount) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
# Returns [bool] | ||
function is_version( [string] $Version ) { | ||
$re = '^(\d{1,16})\.(\d{1,16})\.*(\d{1,16})*\.*(\d{1,16})*(-[^.-]+)*$' | ||
if ($Version -notmatch $re) { return $false } | ||
|
||
$v = $Version -replace '-.+' | ||
return [version]::TryParse($v, [ref]($__)) | ||
return [AUVersion]::TryParse($Version, [ref]($__)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Author: Thomas Démoulins <[email protected]> | ||
|
||
<# | ||
.SYNOPSIS | ||
Get a semver-like object from a given version string. | ||
.DESCRIPTION | ||
This function parses a string containing a semver-like version | ||
and returns an object that represents both the version (with up to 4 parts) | ||
and optionally a pre-release and a build metadata. | ||
The parsing is quite flexible: | ||
- the string can starts with a 'v' | ||
- there can be no hyphen between the version and the pre-release | ||
- extra spaces (between any parts of the semver-like version) are ignored | ||
#> | ||
function Get-Version { | ||
[CmdletBinding()] | ||
param( | ||
# Version string to parse. | ||
[string] $Version | ||
) | ||
return [AUVersion]::Parse($Version, $false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.