-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathauto_release.ps1
More file actions
299 lines (208 loc) · 5.97 KB
/
Copy pathauto_release.ps1
File metadata and controls
299 lines (208 loc) · 5.97 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
# 统一 UTF8,避免 GitHub tag 中文乱码
param(
[switch]$DryRun
)
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [Console]::OutputEncoding
git config i18n.commitEncoding utf-8 | Out-Null
git config i18n.logOutputEncoding utf-8 | Out-Null
# 版本限制
$MAX_MAJOR = 10
$MAX_MINOR = 10
$MAX_PATCH = 100
function Get-AllTags {
try {
$tags = git tag -l 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ 获取标签失败" -ForegroundColor Red
return @()
}
$tagList = $tags | Where-Object { $_ -match '\S' }
Write-Host "✓ 找到 $($tagList.Count) 个标签" -ForegroundColor Green
return $tagList
}
catch {
Write-Host "✗ 获取标签失败: $_" -ForegroundColor Red
return @()
}
}
function ParseVersion {
param([string]$tag)
if ($tag -match '^v(\d+)\.(\d+)\.(\d+)$') {
$major = [int]$matches[1]
$minor = [int]$matches[2]
$patch = [int]$matches[3]
if ($major -le $MAX_MAJOR -and $minor -le $MAX_MINOR -and $patch -le $MAX_PATCH) {
return @{
Major = $major
Minor = $minor
Patch = $patch
Valid = $true
}
}
}
return @{ Valid = $false }
}
function Find-MaxVersion {
param([array]$tags)
$versions = @()
foreach ($tag in $tags) {
$version = ParseVersion $tag
if ($version.Valid) {
$versions += $version
}
}
if ($versions.Count -eq 0) {
Write-Host "⚠ 未找到有效版本标签" -ForegroundColor Yellow
return $null
}
$maxVersion = $versions |
Sort-Object { $_.Major * 10000 + $_.Minor * 100 + $_.Patch } |
Select-Object -Last 1
Write-Host "✓ 当前最大版本: v$($maxVersion.Major).$($maxVersion.Minor).$($maxVersion.Patch)" -ForegroundColor Green
return $maxVersion
}
function IncrementVersion {
param($version)
if ($null -eq $version) {
return @{ Major = 0; Minor = 1; Patch = 0; Valid = $true }
}
if ($version.Patch -lt $MAX_PATCH) {
return @{
Major = $version.Major
Minor = $version.Minor
Patch = $version.Patch + 1
Valid = $true
}
}
if ($version.Minor -lt $MAX_MINOR) {
return @{
Major = $version.Major
Minor = $version.Minor + 1
Patch = 0
Valid = $true
}
}
if ($version.Major -lt $MAX_MAJOR) {
return @{
Major = $version.Major + 1
Minor = 0
Patch = 0
Valid = $true
}
}
Write-Host "✗ 版本号已达到最大值 v$MAX_MAJOR.$MAX_MINOR.$MAX_PATCH" -ForegroundColor Red
return @{ Valid = $false }
}
function Format-Version {
param($version)
return "v$($version.Major).$($version.Minor).$($version.Patch)"
}
function Test-GitStatus {
$status = git status --porcelain 2>&1
if ($status) {
Write-Host "⚠ 工作区有未提交更改:" -ForegroundColor Yellow
Write-Host $status
$response = Read-Host "是否继续?(y/N)"
return $response -ieq "y"
}
return $true
}
function Get-LatestCommitMessage {
try {
$message = git log -1 --pretty=%B 2>&1
if ($LASTEXITCODE -ne 0) {
return "Release"
}
return $message.Trim()
}
catch {
return "Release"
}
}
function New-GitTag {
param(
[string]$tag,
[string]$message
)
try {
git tag -a $tag -m "$message" 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ 创建标签失败" -ForegroundColor Red
return $false
}
Write-Host "✓ 已创建标签: $tag" -ForegroundColor Green
return $true
}
catch {
Write-Host "✗ 创建标签失败: $_" -ForegroundColor Red
return $false
}
}
function Push-GitTag {
param([string]$tag)
try {
git push origin $tag 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ 推送标签失败" -ForegroundColor Red
return $false
}
Write-Host "✓ 标签已推送到远程: $tag" -ForegroundColor Green
return $true
}
catch {
Write-Host "✗ 推送标签失败: $_" -ForegroundColor Red
return $false
}
}
Write-Host ""
Write-Host ("=" * 60)
Write-Host "自动版本发布脚本" -ForegroundColor Cyan
Write-Host ("=" * 60)
$tags = Get-AllTags
$maxVersion = Find-MaxVersion $tags
$nextVersion = IncrementVersion $maxVersion
if (-not $nextVersion.Valid) {
exit 1
}
$nextTag = Format-Version $nextVersion
Write-Host ""
Write-Host "📦 新版本: $nextTag" -ForegroundColor Cyan
Write-Host ""
if ($DryRun) {
Write-Host "🔍 演习模式:" -ForegroundColor Yellow
Write-Host " 创建标签: $nextTag"
Write-Host " 推送命令: git push origin $nextTag"
exit 0
}
if (-not (Test-GitStatus)) {
exit 1
}
Write-Host "即将发布版本: $nextTag" -ForegroundColor Yellow
$response = Read-Host "确认继续?(Y/n)"
if ($response -ieq "n") {
Write-Host "✗ 操作已取消" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "⟳ 推送最新 commit..." -ForegroundColor Cyan
git push 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ 推送 commit 失败" -ForegroundColor Red
exit 1
}
Write-Host "✓ commit 已推送" -ForegroundColor Green
$message = Get-LatestCommitMessage
if (-not (New-GitTag $nextTag $message)) {
exit 1
}
if (-not (Push-GitTag $nextTag)) {
Write-Host "⚠ 标签已创建但推送失败,可手动执行:" -ForegroundColor Yellow
Write-Host "git push origin $nextTag"
exit 1
}
Write-Host ""
Write-Host ("=" * 60)
Write-Host "✓ 发布成功!版本: $nextTag" -ForegroundColor Green
Write-Host ("=" * 60)