<# .SYNOPSIS Computes the build version: a release's X.Y.Z, or X.Y.Z.N for a build N commits after the last release (README, Versioning). .DESCRIPTION Order of precedence: 1. -Version (explicit), then the UNIFIED_VERSION environment variable: a release run hands in the version semantic-release computed (scripts/Get-NextRelease.ps1); CI hands one value to every step. 2. git: the newest release tag vX.Y.Z reachable from HEAD and the commits since it (git describe). 1.4.0.7 is seven commits after 1.4.0; a build of the tagged commit itself is 1.4.0. 3. No release tag reachable: 0.0.0.. No git: 0.0.0.0. Release tags have a 1-3 digit major, so the calendar-era tags (v2026.10.10.0140) never match. Every version is 3 or 4 numeric fields (no -prerelease labels) so assembly, file and MSI versions accept it, and is refused when it does not fit an MSI ProductVersion (major and minor up to 255, the rest up to 65535) or has a calendar-era major (2000 or more). .PARAMETER Format Version (default): 1.4.0 or 1.4.0.7. FileVersion: always four fields (1.4.0.0), for assembly/file/MSI versions. Informational: 1.4.0.7+. Short: the short commit hash (empty without git). MSBuild: -p:UnifiedVersion=... and -p:GitCommitShort=... lines. Env: UNIFIED_VERSION=... and GIT_COMMIT_SHORT=... lines (for $GITHUB_ENV). Object: all of the above. .PARAMETER Describe What `git describe --tags --long` would print (v1.4.0-7-g1a2b3c4), in place of git; an empty string means no release tag (0.0.0.<-Commits>). For tests. .EXAMPLE ./scripts/Get-Version.ps1 # 0.0.0.253, 1.4.0, 1.4.0.7 ./scripts/Get-Version.ps1 -Format FileVersion # 1.4.0.0 ./scripts/Get-Version.ps1 -Version 1.5.0 # validated and echoed #> [CmdletBinding()] param( [string]$Version, [ValidateSet('Version', 'FileVersion', 'Informational', 'Short', 'MSBuild', 'Env', 'Object')] [string]$Format = 'Version', [string]$Root = (Split-Path -Parent $PSScriptRoot), [string]$Describe, [int]$Commits = 0 ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version 3.0 # Majors from here up are the calendar era (yyyy.MM.dd.HHmm), never a semantic version. $calendarEraMajor = 2000 function Assert-Fields([long[]]$Fields, [string]$Text) { if ($Fields[0] -ge $calendarEraMajor) { throw "Version '$Text' is a calendar-era version (major $($Fields[0])), not a semantic one." } if ($Fields[0] -gt 255 -or $Fields[1] -gt 255 -or @($Fields | Select-Object -Skip 2 | Where-Object { $_ -gt 65535 }).Count -gt 0) { throw "Version '$Text' does not fit an MSI ProductVersion (major and minor up to 255, the rest up to 65535)." } } $git = [bool](Get-Command git -ErrorAction SilentlyContinue) $short = '' if ($git) { $short = "$(& git -C $Root rev-parse --short HEAD 2>$null)".Trim() if ($LASTEXITCODE -ne 0) { $short = '' } } if (-not $Version) { $Version = $env:UNIFIED_VERSION } if ($Version) { if ($Version -notmatch '^(\d{1,9})\.(\d{1,9})\.(\d{1,9})(?:\.(\d{1,9}))?$') { throw "Version '$Version' is not major.minor.patch or major.minor.patch.build (numbers only, no -label)." } $base = [long[]]@($Matches[1], $Matches[2], $Matches[3]) $since = if ($Matches[4]) { [long]$Matches[4] } else { 0 } if ($env:GIT_COMMIT_SHORT) { $short = $env:GIT_COMMIT_SHORT } } else { if (-not $PSBoundParameters.ContainsKey('Describe')) { $Describe = '' if ($git -and $short) { # One to three digits of major: v2026.10.10.0140 (calendar era) never matches. $Describe = "$(& git -C $Root describe --tags --long --match 'v[0-9].[0-9]*.[0-9]*' --match 'v[0-9][0-9].[0-9]*.[0-9]*' --match 'v[0-9][0-9][0-9].[0-9]*.[0-9]*' 2>$null)".Trim() if ($LASTEXITCODE -ne 0) { $Describe = '' } $Commits = [int]"$(& git -C $Root rev-list --count HEAD 2>$null)".Trim() } } if ($Describe -match '^v(\d{1,3})\.(\d{1,9})\.(\d{1,9})-(\d+)-g[0-9a-f]+$') { $base = [long[]]@($Matches[1], $Matches[2], $Matches[3]) $since = [long]$Matches[4] } elseif ($Describe) { throw "git describe returned '$Describe', which is not v..--g." } else { $base = [long[]]@(0, 0, 0) $since = [long]$Commits } } $semantic = $base -join '.' $text = if ($since -gt 0) { "$semantic.$since" } else { $semantic } Assert-Fields ($base + $since) $(if ($Version) { $Version } else { $text }) $result = [pscustomobject]@{ Version = $text FileVersion = "$semantic.$since" Short = $short Informational = if ($short) { "$text+$short" } else { $text } } switch ($Format) { 'Version' { $result.Version } 'FileVersion' { $result.FileVersion } 'Informational' { $result.Informational } 'Short' { $result.Short } 'MSBuild' { "-p:UnifiedVersion=$($result.Version)"; if ($result.Short) { "-p:GitCommitShort=$($result.Short)" } } 'Env' { "UNIFIED_VERSION=$($result.Version)"; "GIT_COMMIT_SHORT=$($result.Short)" } default { $result } }