-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-release.ps1
More file actions
244 lines (201 loc) · 7.35 KB
/
Copy pathbuild-release.ps1
File metadata and controls
244 lines (201 loc) · 7.35 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
#Requires -Version 5.1
<#
.SYNOPSIS
Build CLI and GUI release zips for Windows and macOS.
.EXAMPLE
.\build-release.ps1
.\build-release.ps1 -Version v4.0
.\build-release.ps1 -SkipWindows
.\build-release.ps1 -SkipMac
#>
[CmdletBinding()]
param(
[string]$Version = 'v4.0',
[string]$OutputDir = 'release',
[switch]$SkipWindows,
[switch]$SkipMac
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$Root = $PSScriptRoot
$ElectronDir = Join-Path $Root 'electron'
$ReleaseDir = Join-Path $Root $OutputDir
$Script:OnMacOS = if ($PSVersionTable.PSVersion.Major -ge 6) { $IsMacOS } else { $false }
if (-not $Script:OnMacOS) {
$uname = Get-Command uname -ErrorAction SilentlyContinue
if ($uname) { $Script:OnMacOS = (& uname) -eq 'Darwin' }
}
$Script:BuiltArtifacts = [System.Collections.Generic.List[string]]::new()
function Write-Step([string]$Message) {
Write-Host ""
Write-Host "==> $Message" -ForegroundColor Cyan
}
function Ensure-Command([string]$Name) {
if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
throw "Required command not found: $Name"
}
}
function Get-TempRoot {
if (-not [string]::IsNullOrWhiteSpace($env:TEMP)) { return $env:TEMP.TrimEnd('/', '\') }
if (-not [string]::IsNullOrWhiteSpace($env:TMPDIR)) { return $env:TMPDIR.TrimEnd('/', '\') }
return [System.IO.Path]::GetTempPath().TrimEnd('/', '\')
}
function New-EmptyDirectory([string]$Path) {
if ([string]::IsNullOrWhiteSpace($Path)) {
throw 'Directory path is null or empty.'
}
if (Test-Path -LiteralPath $Path) {
Remove-Item -LiteralPath $Path -Recurse -Force
}
New-Item -ItemType Directory -Path $Path -Force | Out-Null
}
function Get-MacBuildArch {
$machine = (& uname -m).Trim()
if ($machine -eq 'arm64') { return 'arm64' }
return 'x86_64'
}
function Stop-BlockingAppProcesses {
param([string]$ElectronRoot)
$resolvedRoot = (Resolve-Path -LiteralPath $ElectronRoot).Path
$processNames = @('appUnblocker', 'electron')
foreach ($name in $processNames) {
Get-Process -Name $name -ErrorAction SilentlyContinue | ForEach-Object {
$procPath = $_.Path
$shouldStop = $name -eq 'appUnblocker'
if (-not $shouldStop -and $procPath) {
$shouldStop = $procPath -like "*$resolvedRoot*"
}
if ($shouldStop) {
Write-Host "Stopping $($_.ProcessName) (PID $($_.Id))..."
Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
}
}
}
Start-Sleep -Milliseconds 500
}
function Get-ElectronBuildOutputDir {
param([string]$Label)
Join-Path $ElectronDir ("dist-build-{0}-{1}" -f $Label, (Get-Date -Format 'yyyyMMddHHmmss'))
}
function Invoke-ElectronBuilder {
param(
[string]$Label,
[string[]]$BuilderArgs
)
Ensure-Command 'npm'
Stop-BlockingAppProcesses -ElectronRoot $ElectronDir
$buildOutputDir = Get-ElectronBuildOutputDir -Label $Label
$buildOutputName = Split-Path -Leaf $buildOutputDir
Push-Location $ElectronDir
try {
if (-not (Test-Path -LiteralPath (Join-Path $ElectronDir 'node_modules'))) {
Write-Host "Running npm install..."
& npm install
if ($LASTEXITCODE -ne 0) { throw "npm install failed (exit $LASTEXITCODE)" }
}
$args = @(
'electron-builder',
"--config.directories.output=$buildOutputName"
) + $BuilderArgs
Write-Host "Running npx $($args -join ' ')..."
& npx @args 2>&1 | ForEach-Object { Write-Host $_.ToString() }
if ($LASTEXITCODE -ne 0) { throw "electron-builder failed (exit $LASTEXITCODE)" }
}
finally {
Pop-Location
}
return ,$buildOutputDir
}
function Publish-GuiZip {
param(
[string]$DistDir,
[string]$DestZipPath,
[string]$ZipFilter = '*.zip'
)
$builtZip = Get-ChildItem -Path $DistDir -Filter $ZipFilter -File |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if (-not $builtZip) {
throw "No zip matching '$ZipFilter' found in $DistDir"
}
if (Test-Path -LiteralPath $DestZipPath) {
Remove-Item -LiteralPath $DestZipPath -Force
}
Copy-Item -LiteralPath $builtZip.FullName -Destination $DestZipPath -Force
Write-Host "Created $DestZipPath (from $($builtZip.Name))" -ForegroundColor Green
$Script:BuiltArtifacts.Add($DestZipPath) | Out-Null
}
function Build-CliZip {
$cliZipName = "appunblocker-$Version-cli.zip"
$cliZipPath = Join-Path $ReleaseDir $cliZipName
Write-Step "Packaging CLI ($cliZipName)"
$stage = Join-Path (Get-TempRoot) "appunblocker-cli-stage-$([guid]::NewGuid())"
New-EmptyDirectory $stage
try {
$cliFiles = @(
(Join-Path $Root 'appunblocker.ps1'),
(Join-Path $Root 'LICENSE'),
(Join-Path $Root 'README.md')
)
foreach ($file in $cliFiles) {
if (-not (Test-Path -LiteralPath $file)) {
throw "Missing required file: $file"
}
Copy-Item -LiteralPath $file -Destination $stage
}
$archiveItems = @(Get-ChildItem -LiteralPath $stage -Force)
if ($archiveItems.Count -eq 0) {
throw "CLI stage folder is empty: $stage"
}
Compress-Archive -Path ($archiveItems | ForEach-Object { $_.FullName }) -DestinationPath $cliZipPath -CompressionLevel Optimal -Force
Write-Host "Created $cliZipPath" -ForegroundColor Green
$Script:BuiltArtifacts.Add($cliZipPath) | Out-Null
}
finally {
Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue
}
}
function Build-WindowsGuiZip {
$guiZipName = "appunblocker-$Version-win-x86_64-gui.zip"
$guiZipPath = Join-Path $ReleaseDir $guiZipName
Write-Step "Building GUI (Windows x64) -> $guiZipName"
$distDir = Invoke-ElectronBuilder -Label 'win' -BuilderArgs @('--win', 'zip', '--x64')
Publish-GuiZip -DistDir $distDir -DestZipPath $guiZipPath
}
function Build-MacGuiZip {
$macArch = Get-MacBuildArch
$electronArchFlag = if ($macArch -eq 'arm64') { '--arm64' } else { '--x64' }
$guiZipName = "appunblocker-$Version-macos-$macArch-gui.zip"
$guiZipPath = Join-Path $ReleaseDir $guiZipName
Write-Step "Building GUI (macOS $macArch) -> $guiZipName"
$distDir = Invoke-ElectronBuilder -Label "mac-$macArch" -BuilderArgs @('--mac', 'zip', $electronArchFlag)
Publish-GuiZip -DistDir $distDir -DestZipPath $guiZipPath -ZipFilter '*-mac.zip'
}
Write-Step "appUnblocker release build ($Version)"
if (-not $Script:OnMacOS -and -not $SkipWindows) {
Write-Host "Note: Windows GUI can be cross-built from macOS. macOS GUI requires building on a Mac." -ForegroundColor Yellow
}
New-EmptyDirectory $ReleaseDir
Build-CliZip
if (-not $SkipWindows) {
Build-WindowsGuiZip
}
else {
Write-Host "Skipping Windows GUI (-SkipWindows)." -ForegroundColor Yellow
}
if (-not $SkipMac) {
if ($Script:OnMacOS) {
Build-MacGuiZip
}
else {
Write-Host "Skipping macOS GUI (build must run on macOS)." -ForegroundColor Yellow
}
}
else {
Write-Host "Skipping macOS GUI (-SkipMac)." -ForegroundColor Yellow
}
Write-Host ""
Write-Host "Done. Release files:" -ForegroundColor Green
foreach ($artifact in $Script:BuiltArtifacts) {
Write-Host " $artifact"
}