-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-release.ps1
More file actions
212 lines (180 loc) · 8.91 KB
/
build-release.ps1
File metadata and controls
212 lines (180 loc) · 8.91 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
<#
.SYNOPSIS
Build a GithubSaver release (publish + MSI installer).
.DESCRIPTION
Automates the full release pipeline:
1. Auto-increments build number (4th digit for MSI upgrade detection)
2. Updates version in GithubSaver.csproj and installer/Package.wxs
3. Runs all tests (C# + JS)
4. Publishes self-contained single-file build
5. Copies .exe to .scr
6. Builds MSI installer with WiX
7. Reports artifact locations and sizes
A build number auto-increments on each build within the same version,
stored in .build-number. This ensures MSI upgrades always detect a new
version without burning real semver numbers on test iterations.
The build number resets to 1 when the base version (MAJOR.MINOR.PATCH)
changes.
.PARAMETER Version
Semantic version string (e.g., "0.2.5-beta", "1.0.0").
Pre-release suffix is stripped for MSI (WiX only supports numeric versions).
Build number is appended as 4th digit automatically.
.PARAMETER SkipTests
Skip running tests. Use only for quick rebuild of a known-good version.
.EXAMPLE
.\build-release.ps1 -Version 0.2.4-beta
# First run: MSI=0.2.4.1 Assembly=0.2.4-beta+1
# Second run: MSI=0.2.4.2 Assembly=0.2.4-beta+2
# After -Version 0.2.5-beta: MSI=0.2.5.1 (resets)
.EXAMPLE
.\build-release.ps1 -Version 1.0.0 -SkipTests
#>
param(
[Parameter(Mandatory = $true)]
[string]$Version,
[switch]$SkipTests
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$RepoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$CsprojPath = Join-Path $RepoRoot 'src\GithubSaver\GithubSaver.csproj'
$WxsPath = Join-Path $RepoRoot 'installer\Package.wxs'
$TestCsproj = Join-Path $RepoRoot 'tests\GithubSaver.Tests\GithubSaver.Tests.csproj'
$BuildNumberFile = Join-Path $RepoRoot '.build-number'
# ── Validate version format ─────────────────────────────────────
if ($Version -notmatch '^\d+\.\d+\.\d+(-[\w.]+)?$') {
Write-Error "Invalid version format: '$Version'. Expected semver (e.g., 0.2.5-beta, 1.0.0)"
exit 1
}
# ── Auto-increment build number ─────────────────────────────────
$BaseVersion = $Version -replace '-.*$', ''
$BuildNumber = 1
if (Test-Path $BuildNumberFile) {
$content = (Get-Content $BuildNumberFile -Raw).Trim()
if ($content -match '^(.+):(\d+)$') {
if ($Matches[1] -eq $BaseVersion) {
$BuildNumber = [int]$Matches[2] + 1
}
}
}
Set-Content -Path $BuildNumberFile -Value "${BaseVersion}:${BuildNumber}" -NoNewline
$MsiVersion = "$BaseVersion.$BuildNumber"
$AssemblyVersion = "$Version+$BuildNumber"
$PublishDir = Join-Path $RepoRoot "releases\v$Version+$BuildNumber"
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " GithubSaver Release Builder" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Version : $Version" -ForegroundColor Cyan
Write-Host " Build : #$BuildNumber" -ForegroundColor Cyan
Write-Host " Assembly : $AssemblyVersion" -ForegroundColor Cyan
Write-Host " MSI : $MsiVersion" -ForegroundColor Cyan
Write-Host " Output : releases\v$Version+$BuildNumber\" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# ── Step 1: Update version in csproj ─────────────────────────────
Write-Host "[1/6] Updating version in GithubSaver.csproj..." -ForegroundColor Yellow
$csproj = Get-Content $CsprojPath -Raw
$csproj = $csproj -replace '<Version>[^<]+</Version>', "<Version>$AssemblyVersion</Version>"
Set-Content -Path $CsprojPath -Value $csproj -NoNewline
Write-Host " -> <Version>$AssemblyVersion</Version>" -ForegroundColor Green
# ── Step 2: Update version in Package.wxs ────────────────────────
Write-Host "[2/6] Updating version in Package.wxs..." -ForegroundColor Yellow
$wxsContent = Get-Content $WxsPath
for ($i = 0; $i -lt $wxsContent.Count; $i++) {
# Match the Package element's Version attribute (contains dots like "0.2.4")
if ($wxsContent[$i] -match '^\s+Version="\d+\.\d+' -and $wxsContent[$i] -notmatch 'Installer') {
$wxsContent[$i] = $wxsContent[$i] -replace 'Version="[\d.]+"', "Version=`"$MsiVersion`""
break
}
}
Set-Content -Path $WxsPath -Value $wxsContent
Write-Host " -> Version=`"$MsiVersion`"" -ForegroundColor Green
# ── Step 3: Run tests ────────────────────────────────────────────
if ($SkipTests) {
Write-Host "[3/6] Skipping tests (-SkipTests)" -ForegroundColor DarkYellow
} else {
Write-Host "[3/6] Running tests..." -ForegroundColor Yellow
Write-Host " -> C# tests..." -ForegroundColor Gray
dotnet test $TestCsproj --verbosity quiet
if ($LASTEXITCODE -ne 0) {
Write-Error "C# tests failed. Aborting release build."
exit 1
}
Write-Host " -> C# tests passed" -ForegroundColor Green
Write-Host " -> JS tests..." -ForegroundColor Gray
Push-Location $RepoRoot
npx jest tests/js/ --silent 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
npx jest tests/js/
Write-Error "JS tests failed. Aborting release build."
Pop-Location
exit 1
}
Pop-Location
Write-Host " -> JS tests passed" -ForegroundColor Green
}
# ── Step 4: Publish ──────────────────────────────────────────────
Write-Host "[4/6] Publishing self-contained build..." -ForegroundColor Yellow
if (Test-Path $PublishDir) {
Remove-Item $PublishDir -Recurse -Force
}
dotnet publish $CsprojPath `
-c Release `
-r win-x64 `
--self-contained `
-p:PublishSingleFile=true `
-p:IncludeNativeLibrariesForSelfExtract=true `
-o $PublishDir `
--verbosity quiet
if ($LASTEXITCODE -ne 0) {
Write-Error "Publish failed. Aborting release build."
exit 1
}
Write-Host " -> Published to releases\v$Version+$BuildNumber\" -ForegroundColor Green
# ── Step 5: Create .scr copy ────────────────────────────────────
Write-Host "[5/6] Creating .scr copy..." -ForegroundColor Yellow
$exePath = Join-Path $PublishDir 'GithubSaver.exe'
$scrPath = Join-Path $PublishDir 'GithubSaver.scr'
Copy-Item $exePath $scrPath -Force
Write-Host " -> GithubSaver.scr created" -ForegroundColor Green
# ── Step 6: Build MSI ────────────────────────────────────────────
Write-Host "[6/6] Building MSI installer..." -ForegroundColor Yellow
# Ensure Windows Installer service is running (required by WiX)
$msiService = Get-Service msiserver -ErrorAction SilentlyContinue
if ($msiService -and $msiService.Status -ne 'Running') {
Start-Service msiserver -ErrorAction SilentlyContinue
}
$msiPath = Join-Path $PublishDir 'GithubSaver.msi'
wix build $WxsPath `
-o $msiPath `
-bindpath "publishDir=$PublishDir" `
-arch x64 2>&1 | ForEach-Object {
if ($_ -match 'error') { Write-Host " $_" -ForegroundColor Red }
}
if ($LASTEXITCODE -ne 0) {
Write-Error "WiX build failed. .scr is still available but MSI was not created."
exit 1
}
Write-Host " -> GithubSaver.msi created" -ForegroundColor Green
# ── Summary ──────────────────────────────────────────────────────
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " Build complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
$scrSize = "{0:N1} MB" -f ((Get-Item $scrPath).Length / 1MB)
$msiSize = "{0:N1} MB" -f ((Get-Item $msiPath).Length / 1MB)
Write-Host " .scr : $scrSize" -ForegroundColor Green
Write-Host " .msi : $msiSize" -ForegroundColor Green
Write-Host " Build: #$BuildNumber ($MsiVersion)" -ForegroundColor Green
Write-Host " Path : releases\v$Version+$BuildNumber\" -ForegroundColor Green
Write-Host "" -ForegroundColor Green
Write-Host " Test:" -ForegroundColor Green
Write-Host " .\releases\v$Version+$BuildNumber\GithubSaver.scr /c" -ForegroundColor Green
Write-Host " .\releases\v$Version+$BuildNumber\GithubSaver.scr /s" -ForegroundColor Green
Write-Host "" -ForegroundColor Green
Write-Host " After testing, to release:" -ForegroundColor Green
Write-Host " git tag v$Version" -ForegroundColor Green
Write-Host " git push origin --tags" -ForegroundColor Green
Write-Host " gh release create v$Version --prerelease" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green