Repository navigation
Expand file tree
/
Copy pathrelease.ps1
More file actions
248 lines (180 loc) · 7.23 KB
/
Copy pathrelease.ps1
File metadata and controls
248 lines (180 loc) · 7.23 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
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Version,
[switch]$NoPush
)
$ErrorActionPreference = "Stop"
# ---------------------------------------------------------------------------
# Locate repository
# ---------------------------------------------------------------------------
$RepoRoot = (& git rev-parse --show-toplevel 2>$null)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($RepoRoot)) {
throw "Run this script inside the DCCExpressHub Git repository."
}
$RepoRoot = $RepoRoot.Trim()
Set-Location $RepoRoot
# ---------------------------------------------------------------------------
# Validate version
# ---------------------------------------------------------------------------
$Version = $Version.Trim()
if ($Version.StartsWith("v", [System.StringComparison]::OrdinalIgnoreCase)) {
$Version = $Version.Substring(1)
}
if ($Version -notmatch '^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$') {
throw "Invalid version '$Version'. Example: 0.1.0-alpha.2 or 0.1.0"
}
$Tag = "v$Version"
# ---------------------------------------------------------------------------
# Require a clean working tree
# ---------------------------------------------------------------------------
$status = (& git status --porcelain)
if ($LASTEXITCODE -ne 0) {
throw "Could not read git status."
}
if ($status) {
Write-Host ""
Write-Host "Working tree is not clean:" -ForegroundColor Red
$status | ForEach-Object {
Write-Host " $_" -ForegroundColor Yellow
}
throw "Commit or stash current changes before creating a release."
}
# ---------------------------------------------------------------------------
# Determine current branch
# ---------------------------------------------------------------------------
$branch = (& git branch --show-current)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($branch)) {
throw "Could not determine current branch. Do not release from detached HEAD."
}
$branch = $branch.Trim()
# ---------------------------------------------------------------------------
# Refuse an already published tag
# ---------------------------------------------------------------------------
$remoteTag = (& git ls-remote --tags origin "refs/tags/$Tag" 2>$null)
if ($LASTEXITCODE -ne 0) {
throw "Could not query remote tags from origin."
}
if ($remoteTag) {
throw "Remote tag $Tag already exists."
}
# ---------------------------------------------------------------------------
# Update VERSION (single source of truth)
# ---------------------------------------------------------------------------
[System.IO.File]::WriteAllText(
(Join-Path $RepoRoot "VERSION"),
"$Version`n",
(New-Object System.Text.UTF8Encoding($false))
)
Write-Host "VERSION -> $Version" -ForegroundColor Green
# ---------------------------------------------------------------------------
# Synchronize npm metadata from VERSION
#
# IMPORTANT:
# Use npm.cmd explicitly on Windows and place --prefix BEFORE the subcommand.
# This avoids PowerShell argument forwarding problems.
# ---------------------------------------------------------------------------
$npmCommand = $null
if (Get-Command npm.cmd -ErrorAction SilentlyContinue) {
$npmCommand = "npm.cmd"
} elseif (Get-Command npm -ErrorAction SilentlyContinue) {
$npmCommand = "npm"
} else {
throw "npm was not found in PATH."
}
& $npmCommand `
--prefix web-ui `
version $Version `
--no-git-tag-version `
--allow-same-version
if ($LASTEXITCODE -ne 0) {
throw "npm version synchronization failed with exit code $LASTEXITCODE"
}
# ---------------------------------------------------------------------------
# Stage only version-related files
# ---------------------------------------------------------------------------
& git add VERSION web-ui/package.json
if ($LASTEXITCODE -ne 0) {
throw "git add VERSION web-ui/package.json failed."
}
$packageLock = Join-Path $RepoRoot "web-ui\package-lock.json"
if (Test-Path -LiteralPath $packageLock) {
& git add web-ui/package-lock.json
if ($LASTEXITCODE -ne 0) {
throw "git add web-ui/package-lock.json failed."
}
}
# ---------------------------------------------------------------------------
# Commit version changes if needed
# ---------------------------------------------------------------------------
& git diff --cached --quiet
$hasStagedChanges = ($LASTEXITCODE -ne 0)
if ($hasStagedChanges) {
& git commit -m "Release $Tag"
if ($LASTEXITCODE -ne 0) {
throw "Release commit failed."
}
Write-Host "Created release commit: $Tag" -ForegroundColor Green
} else {
Write-Host "Version files already match $Version; no release commit needed." -ForegroundColor DarkGray
}
# ---------------------------------------------------------------------------
# Determine current HEAD
# ---------------------------------------------------------------------------
$currentCommit = (& git rev-parse HEAD)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($currentCommit)) {
throw "Could not determine current HEAD commit."
}
$currentCommit = $currentCommit.Trim()
# ---------------------------------------------------------------------------
# Create/reuse local annotated tag
# ---------------------------------------------------------------------------
$localTagName = (& git tag --list $Tag)
if ($LASTEXITCODE -ne 0) {
throw "Could not query local tags."
}
if (-not [string]::IsNullOrWhiteSpace($localTagName)) {
$localTagCommit = (& git rev-list -n 1 $Tag)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($localTagCommit)) {
throw "Could not resolve existing local tag $Tag."
}
$localTagCommit = $localTagCommit.Trim()
if ($localTagCommit -ne $currentCommit) {
throw "Local tag $Tag already exists and points to another commit."
}
Write-Host "Local tag $Tag already points to HEAD; reusing it." -ForegroundColor DarkGray
} else {
& git tag -a $Tag -m "DCCExpressHub $Tag"
if ($LASTEXITCODE -ne 0) {
throw "Could not create annotated tag $Tag."
}
Write-Host "Created annotated tag: $Tag" -ForegroundColor Green
}
# ---------------------------------------------------------------------------
# Optional local-only mode
# ---------------------------------------------------------------------------
if ($NoPush) {
Write-Host ""
Write-Host "NoPush requested. Nothing was pushed." -ForegroundColor Yellow
Write-Host ""
Write-Host "When ready:" -ForegroundColor Cyan
Write-Host " git push origin $branch"
Write-Host " git push origin $Tag"
exit 0
}
# ---------------------------------------------------------------------------
# Push branch, then tag
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host "Pushing branch '$branch'..." -ForegroundColor Cyan
& git push origin $branch
if ($LASTEXITCODE -ne 0) {
throw "Could not push branch '$branch'."
}
Write-Host "Pushing tag '$Tag'..." -ForegroundColor Cyan
& git push origin $Tag
if ($LASTEXITCODE -ne 0) {
throw "Could not push tag '$Tag'."
}
Write-Host ""
Write-Host "Release tag pushed successfully." -ForegroundColor Green
Write-Host "GitHub Actions will build the official DCC-EX firmware release for $Tag." -ForegroundColor Green