ps/Modules/Alkami.PowerShell.Common/Public/Get-VersionPSObject.ps1
2023-05-30 22:51:22 -07:00

42 lines
1.7 KiB
PowerShell

function Get-VersionPSObject {
<#
.SYNOPSIS
Parse a function into a semver compatible version object instead of the naive .NET implementation.
The .NET implementation existed before the semver formalization, so it does not implement all of the same properties/metadata concepts.
While a third-party library may exist that neatly handles these edge-cases, we do not use those in PowerShell due to availability concerns.
.PARAMETER Version
[string] The value to parse.
#>
[CmdletBinding()]
[OutputType([object])]
param(
[Parameter(Mandatory=$true)]
[string]$Version
)
# region Semversion variables
$AllowFourPartsVersion = "(?<Version>\d+(\s*\.\s*\d+){0,3})";
# For some reason Chocolatey version uses "-" instead of "+" for the build metadata. Here change it to "-"
$ReleasePatternDash = "(?<Release>-[A-Z0-9a-z]+(\.[A-Z0-9a-z]+)*)?";
$BuildPatternDash = "(?<Build>\-[A-Z0-9a-z\-]+(\.[A-Z0-9a-z\-]+)*)?";
# we use this one because Chocolatey uses <version>-<release>-<build> format
$SemanticVersionPatternDash = "^" + $AllowFourPartsVersion + $ReleasePatternDash + $BuildPatternDash + "$"
$isMatch=$Version.Trim() -match $SemanticVersionPatternDash
if( $isMatch ) {
if ($Matches.Version) {$v = $Matches.Version.Trim()} else {$v = $Matches.Version}
if ($Matches.Release) {$r = $Matches.Release.Trim("-, +")} else {$r = $Matches.Release}
if ($Matches.Build) {$b = $Matches.Build.Trim("-, +")} else {$b = $Matches.Build}
return New-Object PSObject -Property @{
Version = $v
Release = $r
Build = $b
}
} else {
Write-Error "Could not determine semantic version of $Version"
}
}