-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
458 additions
and
182 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# Author: Thomas Démoulins <[email protected]> | ||
# Author: Thomas Démoulins <[email protected]> | ||
|
||
<# | ||
.SYNOPSIS | ||
Parses a semver-like object from a string in a flexible manner. | ||
Parses a SemVer-like object from a string in a flexible manner. | ||
.DESCRIPTION | ||
This function parses a string containing a semver-like version | ||
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. | ||
|
@@ -16,26 +16,48 @@ | |
- extra spaces are ignored | ||
- optional delimiters can be provided to help parsing the string | ||
Parameter -SemVer allows to specify the max supported SemVer version: | ||
V1 (default) or V2 (requires choco v2.0.0). EnhancedV2 is about | ||
transforming a SemVer1-like version into a SemVer2-like one when | ||
possible (e.g. 1.61.0-beta.0 instead of 1.61.0-beta0). | ||
Resulting version is normalized the same way chocolatey/nuget does. | ||
See https://learn.microsoft.com/en-us/nuget/concepts/package-versioning#normalized-version-numbers | ||
.EXAMPLE | ||
Get-Version 'Current version 2.1.1 beta 2.' | ||
Returns 2.1.1-beta2 | ||
.EXAMPLE | ||
Get-Version -SemVer V2 'Current version 2.1.1 beta 2.' | ||
Returns 2.1.1-beta.2 | ||
.EXAMPLE | ||
Get-Version 'Last version: 1.2.3 beta 3.' | ||
Get-Version -SemVer V2 '4.0.3Beta1' | ||
Returns 1.2.3-beta.3 | ||
Returns 4.0.3-Beta1 | ||
.EXAMPLE | ||
Get-Version 'https://github.com/atom/atom/releases/download/v1.24.0-beta2/AtomSetup.exe' | ||
Get-Version -SemVer EnhancedV2 '4.0.3Beta1' | ||
Return 1.24.0-beta.2 | ||
Returns 4.0.3-Beta.1 | ||
.EXAMPLE | ||
Get-Version 'http://mirrors.kodi.tv/releases/windows/win32/kodi-17.6-Krypton-x86.exe' -Delimiter '-' | ||
Get-Version 'https://dl.airserver.com/pc32/AirServer-5.6.3-x86.msi' -Delimiter '-' | ||
Return 17.6 | ||
Returns 5.6.3 | ||
#> | ||
function Get-Version { | ||
[CmdletBinding()] | ||
param( | ||
# Supported SemVer version: V1 (default) or V2 (requires choco v2.0.0). | ||
# EnhancedV2 allows to transform a SemVer1-like version into SemVer2-like one (e.g. 1.2.0-rc.3 instead of 1.2.0-rc3) | ||
[ValidateSet('V1', 'V2', 'EnhancedV2')] | ||
[string] $SemVer = 'V1', | ||
# Version string to parse. | ||
[Parameter(Mandatory=$true)] | ||
[Parameter(Mandatory, Position=0)] | ||
[string] $Version, | ||
# Optional delimiter(s) to help locate the version in the string: the version must start and end with one of these chars. | ||
[char[]] $Delimiter | ||
|
@@ -46,10 +68,10 @@ function Get-Version { | |
$regex = $Version | Select-String -Pattern "[$delimiters](\d+\.\d+[^$delimiters]*)[$delimiters]" -AllMatches | ||
foreach ($match in $regex.Matches) { | ||
$reference = [ref] $null | ||
if ([AUVersion]::TryParse($match.Groups[1], $reference, $false)) { | ||
if ([AUVersion]::TryParse($match.Groups[1], $reference, $false, $SemVer)) { | ||
return $reference.Value | ||
} | ||
} | ||
} | ||
return [AUVersion]::Parse($Version, $false) | ||
return [AUVersion]::Parse($Version, $false, $SemVer) | ||
} |
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.