Modules/businessdev.ALbuild.Apps/Private/Get-BcSchemaVersion.ps1
|
function Get-BcSchemaVersion { <# .SYNOPSIS Computes a new four-part version from a current version and a token schema. .DESCRIPTION Internal, pure helper (no I/O) so the version math is unit-testable. Each dotted position of the schema is one token, mapped against the corresponding component of the current version: * a number -> that literal value * 'increment' -> current component + 1 * 'build-id' -> the supplied build id (must be numeric) * 'major'/'minor'/'build'/'revision'/'latest'/'keep' -> keep the current component A schema with fewer than four positions keeps the remaining (lower) components unchanged. Matching is case-insensitive. An unknown or empty token throws. .PARAMETER Current The current four-part version. .PARAMETER Schema The token schema, e.g. 'major.minor.increment.0' or 'major.minor.build-id.0'. .PARAMETER BuildId The value substituted for the 'build-id' token. Default '0'. .OUTPUTS System.Version #> [CmdletBinding()] [OutputType([version])] param( [Parameter(Mandatory)] [version] $Current, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Schema, [string] $BuildId = '0' ) $existing = @($Current.Major, $Current.Minor, [Math]::Max($Current.Build, 0), [Math]::Max($Current.Revision, 0)) $tokens = $Schema.Split('.') $result = @(0, 0, 0, 0) for ($i = 0; $i -lt 4; $i++) { $token = if ($i -lt $tokens.Count) { $tokens[$i].Trim() } else { 'latest' } switch -Regex ($token) { '^\d+$' { $result[$i] = [int]$token } '^build-id$' { $parsed = 0 if (-not [int]::TryParse($BuildId, [ref] $parsed)) { throw "The 'build-id' token requires a numeric build id, but got '$BuildId'." } $result[$i] = $parsed } '^increment$' { $result[$i] = $existing[$i] + 1 } '^(major|minor|build|revision|latest|keep)$' { $result[$i] = $existing[$i] } default { throw "Unknown version token '$token' in schema '$Schema'." } } } return [version]([string]::Join('.', $result)) } |