-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbuild.ps1
More file actions
300 lines (246 loc) · 9.74 KB
/
build.ps1
File metadata and controls
300 lines (246 loc) · 9.74 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#Requires -Version 5.1
param(
[ValidateSet('Debug', 'Release', 'Beta')]
[string] $Configuration = 'Release',
[string] $AppId = '45156332-3408-47B7-B5D2-2567E5888F64',
[ValidateSet('win-x64', 'win-x86', 'win-arm64')]
[string[]] $Platforms = @('win-x64', 'win-x86', 'win-arm64'),
[ValidateSet('self-contained', 'framework-dependent')]
[string[]] $DistributionModes = @('self-contained', 'framework-dependent'),
[int] $MaxParallel = 3
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function ConvertTo-ProcessArgument {
param([Parameter(Mandatory)] [string] $Value)
if ($Value -notmatch '[\s"]') {
return $Value
}
return '"' + $Value.Replace('"', '\"') + '"'
}
function New-TargetName {
param(
[Parameter(Mandatory)] [object] $Task
)
return "$($Task.Kind)-$($Task.Platform)-$($Task.DistributionMode)"
}
function Start-BuildTargetProcess {
param(
[Parameter(Mandatory)] [object] $Task,
[Parameter(Mandatory)] [string] $PowerShellPath,
[Parameter(Mandatory)] [string] $BuildTargetScript,
[Parameter(Mandatory)] [string] $Configuration,
[Parameter(Mandatory)] [string] $AppId,
[Parameter(Mandatory)] [string] $Version,
[Parameter(Mandatory)] [string] $LogPath,
[Parameter(Mandatory)] [string] $PublishRoot,
[Parameter(Mandatory)] [string] $DistRoot
)
$arguments = @(
'-NoLogo',
'-ExecutionPolicy', 'Bypass',
'-File', $BuildTargetScript,
'-Kind', $Task.Kind,
'-Configuration', $Configuration,
'-Platform', $Task.Platform,
'-DistributionMode', $Task.DistributionMode,
'-AppId', $AppId,
'-Version', $Version,
'-LogPath', $LogPath,
'-PublishRoot', $PublishRoot,
'-DistRoot', $DistRoot
) | ForEach-Object { ConvertTo-ProcessArgument -Value ([string] $_) }
$process = Start-Process `
-FilePath $PowerShellPath `
-ArgumentList $arguments `
-WindowStyle Hidden `
-PassThru
return $process
}
function Stop-ProcessTree {
param([Parameter(Mandatory)] [int] $ProcessId)
$children = @(Get-CimInstance Win32_Process -Filter "ParentProcessId=$ProcessId" -ErrorAction SilentlyContinue)
foreach ($child in $children) {
Stop-ProcessTree -ProcessId ([int] $child.ProcessId)
}
Stop-Process -Id $ProcessId -Force -ErrorAction SilentlyContinue
}
function Stop-StaleBuildTargetProcesses {
param(
[Parameter(Mandatory)] [string] $RepoRoot,
[Parameter(Mandatory)] [int] $CurrentProcessId
)
$buildTargetMarker = (Join-Path $RepoRoot 'Scripts\Build-Target.ps1')
$candidates = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue |
Where-Object {
$_.ProcessId -ne $CurrentProcessId -and
$_.Name -in @('pwsh.exe', 'powershell.exe') -and
$_.CommandLine -and
$_.CommandLine.IndexOf($buildTargetMarker, [System.StringComparison]::OrdinalIgnoreCase) -ge 0
})
foreach ($candidate in $candidates) {
Write-Host "Stopping stale build target process $($candidate.ProcessId)." -ForegroundColor DarkGray
Stop-ProcessTree -ProcessId ([int] $candidate.ProcessId)
}
}
$scriptDir = $PSScriptRoot
if (-not $scriptDir) {
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
}
$repoRoot = (Resolve-Path -LiteralPath $scriptDir).Path
$scriptsRoot = Join-Path $repoRoot 'Scripts'
$buildTargetScript = Join-Path $scriptsRoot 'Build-Target.ps1'
$commonModule = Join-Path $scriptsRoot 'Build.Common.psm1'
Import-Module $commonModule -Force -DisableNameChecking
Set-Location -LiteralPath $repoRoot
if ($MaxParallel -lt 1) {
throw 'MaxParallel must be 1 or greater.'
}
$solutionPath = Join-Path $repoRoot 'ContextMenuMgr.slnx'
$frontendProject = Join-Path $repoRoot 'ContextMenuMgr.Frontend\ContextMenuMgr.Frontend.csproj'
$installerIss = Join-Path $repoRoot 'Installer\build_Installer.iss'
$publishRunName = "run-$([System.DateTime]::UtcNow.ToString('yyyyMMdd-HHmmss'))-$PID"
$publishRoot = Join-Path $repoRoot (Join-Path 'build\publish-runs' $publishRunName)
$distRoot = Join-Path $repoRoot 'build\dist'
$logsRoot = Join-Path $repoRoot 'build\logs'
Ensure-FileExists -Path $solutionPath -Description 'Solution'
Ensure-FileExists -Path $frontendProject -Description 'Frontend project'
Ensure-FileExists -Path $installerIss -Description 'Inno Setup script'
Ensure-FileExists -Path $buildTargetScript -Description 'Build target script'
Stop-StaleBuildTargetProcesses -RepoRoot $repoRoot -CurrentProcessId $PID
Write-Host 'Stopping dotnet build servers...' -ForegroundColor DarkGray
$buildServerOutput = & dotnet build-server shutdown 2>&1
foreach ($line in @($buildServerOutput)) {
Write-Host $line
}
if ($LASTEXITCODE -ne 0) {
Write-Warning 'dotnet build-server shutdown failed; continuing with build output cleanup.'
}
Write-Host 'Cleaning build output...' -ForegroundColor DarkGray
Remove-DirectoryIfExists -Path $publishRoot
Remove-DirectoryIfExists -Path $distRoot
Remove-DirectoryIfExists -Path $logsRoot
New-Item -ItemType Directory -Path $publishRoot -Force | Out-Null
New-Item -ItemType Directory -Path $distRoot -Force | Out-Null
New-Item -ItemType Directory -Path $logsRoot -Force | Out-Null
$version = Get-BuildVersion -ProjectPath $frontendProject -Configuration $Configuration
$tasks = New-Object System.Collections.Generic.List[object]
foreach ($distributionMode in $DistributionModes) {
foreach ($platform in $Platforms) {
$tasks.Add([pscustomobject]@{
Kind = 'installer'
Platform = $platform
DistributionMode = $distributionMode
}) | Out-Null
}
}
$tasks.Add([pscustomobject]@{
Kind = 'portable'
Platform = 'anycpu'
DistributionMode = 'framework-dependent'
}) | Out-Null
$pwshCommand = Get-Command pwsh -ErrorAction SilentlyContinue
if ($pwshCommand -and $pwshCommand.Source) {
$powerShellPath = $pwshCommand.Source
}
else {
$powerShellPath = (Get-Command powershell -ErrorAction Stop).Source
}
Write-Host "Version: $version"
Write-Host "Publish workspace: $publishRoot"
Write-Host "Logs: $logsRoot"
Write-Host "Starting $($tasks.Count) build targets with MaxParallel=$MaxParallel..."
$pending = New-Object System.Collections.Generic.Queue[object]
foreach ($task in $tasks) {
$pending.Enqueue($task)
}
$running = New-Object System.Collections.Generic.List[object]
$completed = New-Object System.Collections.Generic.List[object]
$stopScheduling = $false
while ($pending.Count -gt 0 -or $running.Count -gt 0) {
while (-not $stopScheduling -and $pending.Count -gt 0 -and $running.Count -lt $MaxParallel) {
$task = $pending.Dequeue()
$targetName = New-TargetName -Task $task
$logPath = Join-Path $logsRoot "$targetName.log"
Write-Host "Starting $targetName"
$process = Start-BuildTargetProcess `
-Task $task `
-PowerShellPath $powerShellPath `
-BuildTargetScript $buildTargetScript `
-Configuration $Configuration `
-AppId $AppId `
-Version $version `
-LogPath $logPath `
-PublishRoot $publishRoot `
-DistRoot $distRoot
$running.Add([pscustomobject]@{
Task = $task
TargetName = $targetName
Process = $process
LogPath = $logPath
}) | Out-Null
}
$finished = @($running | Where-Object { $_.Process.HasExited })
foreach ($item in $finished) {
$exitCode = $item.Process.ExitCode
if ($exitCode -eq 0) {
Write-Host "Completed $($item.TargetName)"
}
else {
Write-Host "Failed $($item.TargetName) (ExitCode=$exitCode). Log: $($item.LogPath)" -ForegroundColor Red
$stopScheduling = $true
$pending.Clear()
}
$completed.Add([pscustomobject]@{
TargetName = $item.TargetName
ExitCode = $exitCode
LogPath = $item.LogPath
}) | Out-Null
$running.Remove($item) | Out-Null
}
if ($stopScheduling -and $running.Count -gt 0) {
foreach ($item in @($running)) {
if (-not $item.Process.HasExited) {
Write-Host "Stopping $($item.TargetName) after build failure. Log: $($item.LogPath)" -ForegroundColor Yellow
Stop-ProcessTree -ProcessId $item.Process.Id
$item.Process.WaitForExit()
}
$completed.Add([pscustomobject]@{
TargetName = $item.TargetName
ExitCode = -1
LogPath = $item.LogPath
}) | Out-Null
$running.Remove($item) | Out-Null
}
}
if ($running.Count -gt 0 -or $pending.Count -gt 0) {
Start-Sleep -Seconds 1
}
}
$failedTargets = @($completed | Where-Object { $_.ExitCode -ne 0 })
if ($failedTargets.Count -gt 0) {
Write-Host ''
Write-Host 'One or more build targets failed:' -ForegroundColor Red
foreach ($failedTarget in $failedTargets) {
Write-Host " $($failedTarget.TargetName): $($failedTarget.LogPath)" -ForegroundColor Red
}
throw 'Build failed.'
}
$artifacts = @(Get-ChildItem -LiteralPath $distRoot -File |
Where-Object { $_.Extension -in '.exe', '.zip' } |
Sort-Object Name |
ForEach-Object { $_.FullName })
if ($artifacts.Count -eq 0) {
throw "No artifacts were produced in $distRoot."
}
$manifestPath = Join-Path $distRoot 'artifacts.txt'
$artifacts | Set-Content -LiteralPath $manifestPath -Encoding UTF8
Write-Host ''
Write-Host 'Build completed successfully.' -ForegroundColor Green
Write-Host "Configuration: $Configuration"
Write-Host "Version: $version"
Write-Host "AppId: $AppId"
Write-Host 'Artifacts:'
foreach ($artifact in $artifacts) {
Write-Host " $artifact"
}