Files

23 lines
1.3 KiB
PowerShell

function Get-linkportBuildInfo {
param([string]$Version, [switch]$Development)
$repoRoot = Split-Path $PSScriptRoot -Parent
if (!$Version) { $Version = (Get-Content -LiteralPath (Join-Path $repoRoot 'VERSION') -Raw).Trim() }
if ($Version -notmatch '^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$') { throw 'Invalid version' }
$revision = 'unknown'
$dirty = $false
if ((Test-Path -LiteralPath (Join-Path $repoRoot '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) {
$head = & git -C $repoRoot rev-parse --verify HEAD 2>$null
if ($LASTEXITCODE -eq 0) {
$revision = $head.Trim()
$dirty = [bool](& git -C $repoRoot status --porcelain)
}
}
$commit = if ($dirty) { "$revision-dirty" } else { $revision }
# A changing timestamp forces the linker to run even when all sources are cached.
$buildTime = if ($Development) { 'unknown' } else { [DateTime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ssZ') }
return [PSCustomObject]@{
Version = $Version; Commit = $commit; Revision = $revision; Dirty = $dirty; BuildTime = $buildTime
LDFlags = "-s -w -X linkport/internal/buildinfo.Version=$Version -X linkport/internal/buildinfo.Commit=$commit -X linkport/internal/buildinfo.BuildTime=$buildTime"
}
}