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 -BuildId (must be numeric) * 'no-of-commits' -> the supplied -CommitCount (must be numeric) * 'date' -> the supplied -Date (must be numeric, e.g. yyyyMMdd) * '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. 'no-of-commits' and 'date' carry no I/O here on purpose: the caller (Set-BcAppVersion) computes the git commit count and the date once and passes them in, exactly as it does for 'build-id', so this function stays pure and deterministic under test. .PARAMETER Current The current four-part version. .PARAMETER Schema The token schema, e.g. 'major.minor.increment.0' or 'major.no-of-commits.build-id.date'. .PARAMETER BuildId The value substituted for the 'build-id' token. Default '0'. .PARAMETER CommitCount The value substituted for the 'no-of-commits' token. Default '0'. .PARAMETER Date The value substituted for the 'date' token (e.g. 'yyyyMMdd'). Default '0'. .OUTPUTS System.Version #> [CmdletBinding()] [OutputType([version])] param( [Parameter(Mandatory)] [version] $Current, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Schema, [string] $BuildId = '0', [string] $CommitCount = '0', [string] $Date = '0' ) # A numeric substitution token ('build-id' / 'no-of-commits' / 'date') must fit a version component # (System.Version components are Int32). yyyyMMdd (e.g. 20260712) and commit counts fit comfortably. $asComponent = { param($TokenName, $RawValue) $parsed = 0 if (-not [int]::TryParse($RawValue, [ref] $parsed) -or $parsed -lt 0) { throw "The '$TokenName' token requires a non-negative numeric value, but got '$RawValue'." } $parsed } $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$' { $result[$i] = & $asComponent 'build-id' $BuildId } '^no-of-commits$' { $result[$i] = & $asComponent 'no-of-commits' $CommitCount } '^date$' { $result[$i] = & $asComponent 'date' $Date } '^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)) } |