-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
144 lines (120 loc) · 5.21 KB
/
build.ps1
File metadata and controls
144 lines (120 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<#
.SYNOPSIS
Cross-platform PowerShell build helper for this repository.
.DESCRIPTION
This script centralizes build metadata emission and compilation. It provides three
modes (use switches):
-EmitMetadata : only write build/ldflags.txt, build/version.txt, build/git_hash.txt, build/build_date.txt
-Build : perform a normal build, outputting a versioned binary
-VSCBuild : build into a stable path for VS Code debugging (workspaceBasename.exe)
Use -Release to enable release flags (strip symbols).
#>
[CmdletBinding()]
param(
[Alias('m')][switch]$EmitMetadata,
[Alias('b')][switch]$Build,
[Alias('v')][switch]$VSCBuild,
[Alias('r')][switch]$Release,
[Alias('g','noGui')][switch]$DisableWindowsGui
)
function Get-GitInfo {
$info = [ordered]@{ Version = 'dev'; GitHash = 'unknown' }
if (Get-Command git -ErrorAction SilentlyContinue) {
try {
$gitHash = git rev-parse --short HEAD 2>$null
if ($gitHash) { $info.GitHash = $gitHash.Trim() }
$descr = git describe --tags --always 2>$null
if ($descr) { $info.Version = $descr.Trim() }
} catch {
# keep defaults
}
}
return $info
}
function Sanitize-VersionForFileName($v) {
if ([string]::IsNullOrWhiteSpace($v)) { return 'dev' }
$v2 = $v -replace '^v','' -replace '[^A-Za-z0-9._-]','-'
if ([string]::IsNullOrWhiteSpace($v2)) { return 'dev' }
return $v2
}
# Build a short flags string for compact prompts (e.g. -m -b -r -g)
$PSBound = $PSBoundParameters.Keys
# If the user did not explicitly pass -DisableWindowsGui, choose defaults per mode:
# - In VSCBuild mode, default DisableWindowsGui = $true (i.e., disable GUI by default)
# - In Build mode, default DisableWindowsGui = $false (i.e., keep GUI by default)
if (-not $PSBound.Contains('DisableWindowsGui')) {
if ($VSCBuild) { $DisableWindowsGui = $true }
elseif ($Build) { $DisableWindowsGui = $false }
}
# Build a short flags string for compact prompts (e.g. -m -b -r -g)
$shortFlags = @()
if ($EmitMetadata) { $shortFlags += '-m' }
if ($Build) { $shortFlags += '-b' }
if ($VSCBuild) { $shortFlags += '-v' }
if ($Release) { $shortFlags += '-r' }
if ($DisableWindowsGui) { $shortFlags += '-g' }
if ($shortFlags.Count -eq 0) { $shortFlags = @('-') }
Write-Host "Starting build.ps1 [flags: $($shortFlags -join ' ')]"
$repoRoot = Get-Location
$repoName = Split-Path -Leaf $repoRoot
# Reliable Windows detection for both Windows PowerShell and PowerShell Core
$isWindowsRuntime = ($env:OS -eq 'Windows_NT')
$git = Get-GitInfo
$version = $git.Version
$gitHash = $git.GitHash
$buildDate = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
# Normalize version string for metadata/ldflags: remove leading 'v' and unsafe chars
$version = Sanitize-VersionForFileName $version
if ($Release) { $stripFlags = '-s -w' } else { $stripFlags = '' }
$modeFlag = if ($Release) { '-X main.mod=release' } else { '-X main.mod=debug' }
$windowsGuiFlag = if ($DisableWindowsGui) { '' } else { '-H=windowsgui' }
$ldflags = "$stripFlags $windowsGuiFlag -extldflags=-static -X main.version=$version -X main.buildDate=$buildDate -X main.gitHash=$gitHash $modeFlag -X main.APP_VERSION=$version"
New-Item -ItemType Directory -Force -Path build | Out-Null
Set-Content -Path build/ldflags.txt -Value $ldflags -NoNewline
Set-Content -Path build/flags.txt -Value ($shortFlags -join ' ') -NoNewline
Write-Host "Wrote metadata to build/ (version=$version git=$gitHash date=$buildDate)"
Write-Host " short flags: $($shortFlags -join ' ')"
Write-Host " ldflags: $ldflags"
if ($EmitMetadata) {
exit 0
}
function Do-GoBuild($outPath) {
Write-Host "Running go mod tidy"
& go mod tidy
if ($LASTEXITCODE -ne 0) { throw "go mod tidy failed" }
$gcflags = if ($Release) { '' } else { 'all=-N -l' }
if ([string]::IsNullOrEmpty($gcflags)) {
Write-Host "go build -ldflags '$ldflags' -o '$outPath' ."
& go build -ldflags $ldflags -o $outPath .
} else {
Write-Host "go build -gcflags '$gcflags' -ldflags '$ldflags' -o '$outPath' ."
& go build -gcflags $gcflags -ldflags $ldflags -o $outPath .
}
if ($LASTEXITCODE -ne 0) { throw "go build failed" }
Write-Host "Built: $outPath"
}
if ($Build) {
# Build only the stable filename (e.g. ImGUIKKCardTools.exe).
# Do NOT create an extra versioned binary (repoName-version.exe).
# Determine runtime and stable name, then build directly to it.
$isWindowsRuntime = ($env:OS -eq 'Windows_NT')
$stable = if ($isWindowsRuntime) { "$repoName.exe" } else { $repoName }
$outPath = Join-Path $repoRoot $stable
Do-GoBuild $outPath
Write-Host "Built stable filename: $stable"
exit 0
}
if ($VSCBuild) {
# Ensure metadata exists; if not, emit it first
if (-not (Test-Path -Path build/ldflags.txt)) {
Write-Host 'Metadata missing, emitting first...'
# write previously computed metadata (already done above), so continue
}
# Use same reliable Windows detection as above
$stable = if ($isWindowsRuntime) { "$repoName.exe" } else { $repoName }
$outPath = Join-Path $repoRoot $stable
Do-GoBuild $outPath
exit 0
}
Write-Host "No action requested. Use -EmitMetadata, -Build or -VSCBuild."
exit 2