build: default version from Directory.Build.props; allow -Version override

Remove git tag fallback and read AppVersion/PrereleaseLabel from props. Only pass -p:Version when override is provided; otherwise defer to MSBuild evaluation.
This commit is contained in:
Codex CLI 2025-09-01 04:16:29 -05:00
commit 8c165c90a3

View file

@ -5,7 +5,7 @@
pwsh ./build.ps1 # build all
pwsh ./build.ps1 -Targets win # just Windows
pwsh ./build.ps1 -Targets web,android # pick targets
pwsh ./build.ps1 -Version 1.2.3 # override version (else tries git tag)
pwsh ./build.ps1 -Version 1.2.3 # override version (default comes from Directory.Build.props)
#>
param(
@ -60,17 +60,40 @@ function Invoke-InProjectDir {
}
}
# --- Version (git tag 'vX.Y.Z' > explicit > empty) ---
if (-not $Version) {
# --- Version (default from Directory.Build.props unless overridden) ---
function Get-VersionFromProps {
param([Parameter(Mandatory=$true)][string]$PropsPath)
if (-not (Test-Path $PropsPath)) { return $null }
try {
$tag = (git describe --tags --abbrev=0) 2>$null
if ($LASTEXITCODE -eq 0 -and $tag) { $Version = $tag.TrimStart('v') }
} catch { }
$xml = [xml](Get-Content -Raw -Path $PropsPath)
$groups = @($xml.Project.PropertyGroup)
$app = ($groups | ForEach-Object { $_.AppVersion } | Where-Object { $_ -and $_.Trim() -ne '' } | Select-Object -First 1)
$pre = ($groups | ForEach-Object { $_.PrereleaseLabel } | Where-Object { $_ -and $_.Trim() -ne '' } | Select-Object -First 1)
if ($app) {
if ($pre) { return "$app-$pre" }
return $app
}
if ($Version) {
} catch { }
return $null
}
$ExplicitVersion = $false
if ($PSBoundParameters.ContainsKey('Version') -and $Version) { $ExplicitVersion = $true }
$DerivedVersion = $null
if (-not $ExplicitVersion) {
$DerivedVersion = Get-VersionFromProps (Join-Path $Root 'Directory.Build.props')
}
if ($ExplicitVersion) {
# Normalize to SemVer with 3 numeric segments (X.Y.Z)
if ($Version -match '^(\d+)\.(\d+)$') { $Version = "$Version.0" }
Write-Host "Using Version: $Version" -ForegroundColor Yellow
Write-Host "Using Version (override): $Version" -ForegroundColor Yellow
} elseif ($DerivedVersion) {
if ($DerivedVersion -match '^(\d+)\.(\d+)$') { $DerivedVersion = "$DerivedVersion.0" }
Write-Host "Using Version (from Directory.Build.props): $DerivedVersion" -ForegroundColor Yellow
} else {
Write-Host "No explicit version provided; MSBuild will compute Version from Directory.Build.props." -ForegroundColor Yellow
}
# --- Ensure SDK/workloads present ---
@ -90,7 +113,7 @@ if ($Targets -contains 'win') {
'-p:IncludeNativeLibrariesForSelfExtract=true',
'-o', $OutWin
)
if ($Version) { $args += "-p:Version=$Version" }
if ($ExplicitVersion) { $args += "-p:Version=$Version" }
# Prevent '+<sha>' being appended to informational version in artifacts
$args += '-p:IncludeSourceRevisionInInformationalVersion=false'
Invoke-InProjectDir -ProjectPath $ProjDesktop @args
@ -105,7 +128,7 @@ if ($Targets -contains 'web') {
'--self-contained', 'true',
'-o', $OutWeb
)
if ($Version) { $args += "-p:Version=$Version" }
if ($ExplicitVersion) { $args += "-p:Version=$Version" }
$args += '-p:IncludeSourceRevisionInInformationalVersion=false'
Invoke-InProjectDir -ProjectPath $ProjWeb @args
@ -125,7 +148,7 @@ if ($Targets -contains 'android') {
'-p:AndroidPackageFormat=apk',
'-o', $OutDroid
)
if ($Version) { $baseArgs += "-p:Version=$Version" }
if ($ExplicitVersion) { $baseArgs += "-p:Version=$Version" }
$baseArgs += '-p:IncludeSourceRevisionInInformationalVersion=false'
# Unsigned build