private/Test-ValidVersion.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<#
.SYNOPSIS Validates the passed version to be valid as a chocolatey version .DESCRIPTION This functions takes the specified $version variable, and first test if it is a valid stable version '0.4.3.2', if it isn't it then tries to check if it is a valid pre-release version '0.5.4-beta' .PARAMETER version The version to test if it is valid. .PARAMETER stableOnly Only test if the specified version is a stable version. .PARAMETER preReleaseOnly Only test if the specified version is a pre-release version. .OUTPUTS Outputs $true if the version is valid; otherwise $false #> function Test-ValidVersion { param( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "All")] [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "Stable")] [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "PreRelease")] [string]$version, [Parameter(Mandatory = $true, ParameterSetName = 'Stable')] [switch]$stableOnly, [Parameter(Mandatory = $true, ParameterSetName = 'PreRelease')] [switch]$preReleaseOnly ) $stableFormat = '\d+(\.\d+){1,3}' if (!$preReleaseOnly -and $version -match "^$stableFormat$") { return $true } if (!$stableOnly -and $version -match "^${stableFormat}(\-[a-z\d]+){1,2}$") { return $true } return $false } |