Repository navigation
Expand file tree
/
Copy pathRelease.ps1
More file actions
499 lines (420 loc) · 15.6 KB
/
Copy pathRelease.ps1
File metadata and controls
499 lines (420 loc) · 15.6 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<#
.SYNOPSIS
构建模组并发布到 NexusMods 和/或 GitHub Release。
.DESCRIPTION
1. 从 Plugin.cs 读取模组信息(Name, Version, GUID)
2. 构建项目
3. 收集文件并压缩为 {Name}-v{Version}.zip
4. 上传到 NexusMods (通过 API)
5. 上传到 GitHub Release (通过 gh CLI)
.PARAMETER Configuration
构建配置 (Debug/Release),默认 Release。
.PARAMETER NexusApiKey
NexusMods API Key。也可通过环境变量 NEXUS_API_KEY 设置。
.PARAMETER GamePath
游戏路径,用于收集 BepInEx/plugins 下的部署文件。
如果不指定,只从项目目录收集。
.PARAMETER SkipBuild
跳过构建步骤。
.PARAMETER SkipNexus
跳过 NexusMods 上传。
.PARAMETER SkipGitHub
跳过 GitHub Release。
.PARAMETER ReleaseNotes
GitHub Release 说明内容。
.PARAMETER Prerelease
GitHub 标记为预发布。
.EXAMPLE
.\Release.ps1
.EXAMPLE
.\Release.ps1 -SkipNexus -ReleaseNotes "修复了若干问题。"
.NOTES
GitHub Release 需要安装 gh CLI: winget install GitHub.cli
#>
param(
[string]$ModNamespace = "Bark",
[string]$ModDisplayName = "Bark",
[string]$ModVersion = "2.4.3",
[int]$NexusModId = 362,
[string]$Configuration = "Release",
[string]$NexusApiKey = $env:NEXUS_API_KEY,
[string]$GamePath,
[switch]$SkipBuild,
[switch]$SkipNexus,
[switch]$SkipGitHub,
[string]$ReleaseNotes,
[switch]$Prerelease
)
$ErrorActionPreference = "Stop"
$scriptDir = $PSScriptRoot
# ============================================================
# 辅助函数
# ============================================================
function Write-Step
{
param([string]$Message, [string]$Color = "Cyan")
Write-Host ""
Write-Host ">>> $Message" -ForegroundColor $Color
}
function Write-OK
{
param([string]$Message)
Write-Host " OK: $Message" -ForegroundColor Green
}
function Write-Fail
{
param([string]$Message)
Write-Host " FAIL: $Message" -ForegroundColor Red
}
function Convert-MarkdownToNexusBBCode
{
<# 将 Markdown 转换为 NexusMods BBCode 格式 #>
param([string]$Markdown)
$result = $Markdown
# 标题 ## → [size=4][b][/b][/size], # → [size=5][b][/b][/size]
$result = $result -replace '(?m)^###\s+(.+)$', '[size=3][b]$1[/b][/size]'
$result = $result -replace '(?m)^##\s+(.+)$', '[size=4][b]$1[/b][/size]'
$result = $result -replace '(?m)^#\s+(.+)$', '[size=5][b]$1[/b][/size]'
# **粗体**
$result = $result -replace '\*\*(.+?)\*\*', '[b]$1[/b]'
# *斜体*
$result = $result -replace '(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)', '[i]$1[/i]'
# ~~删除线~~
$result = $result -replace '~~(.+?)~~', '[s]$1[/s]'
# `代码`
$result = $result -replace '`([^`]+)`', '[code]$1[/code]'
# [文本](URL) → [url=URL]文本[/url]
$result = $result -replace '\[([^\]]+)\]\(([^)]+)\)', '[url=$2]$1[/url]'
# - 无序列表
$result = $result -replace '(?m)^-\s+(.+)$', '[*]$1[/*]'
# 1. 有序列表
$result = $result -replace '(?m)^\d+\.\s+(.+)$', '[*]$1[/*]'
# > 引用
$result = $result -replace '(?m)^>\s?(.+)$', '[quote]$1[/quote]'
# --- 分隔线
$result = $result -replace '(?m)^---\s*$', '[line]'
return $result
}
# ============================================================
# 1. 模组信息(NewMod.ps1 已自动填入)
# ============================================================
Write-Step "模组信息:" "Yellow"
Write-OK "命名空间: $ModNamespace"
Write-OK "显示名称: $ModDisplayName"
# 询问版本号(读自 Plugin.cs 作为默认值)
$userVersion = Read-Host "输入版本号 (默认: $ModVersion)"
if (-not [string]::IsNullOrWhiteSpace($userVersion))
{
$ModVersion = $userVersion
}
Write-OK "版本号: $ModVersion"
$releasesDir = Join-Path $scriptDir "Releases"
if (-not (Test-Path $releasesDir))
{
New-Item -ItemType Directory -Path $releasesDir -Force | Out-Null
}
$zipName = "$ModDisplayName-v$ModVersion.zip"
$zipPath = Join-Path $releasesDir $zipName
# ============================================================
# 2. 构建项目
# ============================================================
if (-not $SkipBuild)
{
Write-Step "构建项目 ($Configuration)..." "Yellow"
Push-Location $scriptDir
try
{
$buildResult = & dotnet build -c $Configuration 2>&1
if ($LASTEXITCODE -ne 0)
{
Write-Error "构建失败:`n$buildResult"
exit 1
}
Write-OK "构建成功"
}
finally
{
Pop-Location
}
}
# ============================================================
# 3. 收集文件并压缩
# ============================================================
Write-Step "收集文件并创建压缩包..." "Yellow"
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) "MossRelease_$([System.Guid]::NewGuid() )"
$packageDir = Join-Path $tempDir "package"
New-Item -ItemType Directory -Path $packageDir -Force | Out-Null
# DLL 文件
$buildOutputDir = Join-Path $scriptDir "bin/$Configuration/netstandard2.1"
$dllSource = Join-Path $buildOutputDir "$ModNamespace.dll"
if (Test-Path $dllSource)
{
Copy-Item $dllSource $packageDir -Force
Write-OK "已添加: $ModNamespace.dll"
}
else
{
Write-Warning "未找到 DLL: $dllSource"
}
# 依赖 DLL(从项目 BepInEx/plugins/ 目录收集)
$localPluginsDir = Join-Path $scriptDir "BepInEx/plugins"
if (Test-Path $localPluginsDir)
{
$depDlls = Get-ChildItem $localPluginsDir -File -Recurse -Filter "*.dll"
foreach ($dep in $depDlls)
{
Copy-Item $dep.FullName $packageDir -Force
Write-OK "已添加依赖: $( $dep.Name )"
}
}
# PuerTS 文件(从 Puer/ 目录)
$puertPath = Join-Path $scriptDir "Puer"
if (Test-Path $puertPath -PathType Container)
{
# 根目录所有文件(DLL 和 JS 等)
$puertFiles = Get-ChildItem $puertPath -File
foreach ($puertFile in $puertFiles)
{
Copy-Item $puertFile.FullName $packageDir -Force
Write-OK "已添加 PuerTS: $( $puertFile.Name )"
}
# 运行时文件(puerts/ 子目录递归)
$puertRuntimePath = Join-Path $puertPath "puerts"
if (Test-Path $puertRuntimePath -PathType Container)
{
$destPuertRuntime = Join-Path $packageDir "puerts"
Copy-Item $puertRuntimePath $destPuertRuntime -Recurse -Force
Write-OK "已添加 PuerTS 运行时文件"
}
}
# 文档文件 (含更新日志)
$docFiles = @("README.md", "README_ZH.md", "LICENSE.md")
foreach ($doc in $docFiles)
{
$docPath = Join-Path $scriptDir $doc
if (Test-Path $docPath)
{
Copy-Item $docPath $packageDir -Force
Write-OK "已添加: $doc"
}
}
# 如果指定了 GamePath,也收集部署目录下的额外文件
if ($GamePath -and (Test-Path $GamePath))
{
$deployedDir = Join-Path $GamePath "BepInEx/plugins/$ModDisplayName"
if (Test-Path $deployedDir)
{
$extraFiles = Get-ChildItem $deployedDir -File | Where-Object { $_.Extension -ne ".dll" }
foreach ($f in $extraFiles)
{
Copy-Item $f.FullName $packageDir -Force
Write-OK "从部署目录添加: $( $f.Name )"
}
}
}
# 创建压缩包
if (Get-Command Compress-Archive -ErrorAction SilentlyContinue)
{
Compress-Archive -Path "$packageDir/*" -DestinationPath $zipPath -Force
}
else
{
Write-Error "Compress-Archive 不可用"
exit 1
}
$zipSize = (Get-Item $zipPath).Length
$zipSizeMB = [math]::Round($zipSize / 1MB, 2)
Write-OK "压缩包: $zipName ($zipSizeMB MB)"
# 清理临时目录
Remove-Item -Recurse -Force $tempDir -ErrorAction SilentlyContinue
# ============================================================
# 4. 上传到 NexusMods
# ============================================================
if (-not $SkipNexus)
{
Write-Step "上传到 NexusMods..." "Yellow"
if ( [string]::IsNullOrWhiteSpace($NexusApiKey))
{
Write-Fail "未设置 NexusMods API Key。设置环境变量 NEXUS_API_KEY 或使用 -NexusApiKey 参数。"
}
elseif ($NexusModId -eq 0)
{
Write-Fail "未设置 NexusMods Mod ID。使用 -NexusModId 参数指定。"
}
else
{
$nexusBase = "https://api.nexusmods.com/v3"
$nexusHeaders = @{
"apikey" = $NexusApiKey
"Accept" = "application/json"
}
try
{
# 4a. 创建上传会话
Write-Host " 创建上传会话..." -ForegroundColor DarkGray
$createUploadBody = @{
filename = $zipName
size_bytes = $zipSize
} | ConvertTo-Json
$uploadSession = Invoke-RestMethod -Uri "$nexusBase/uploads" `
-Method Post -Headers $nexusHeaders `
-Body $createUploadBody -ContentType "application/json"
$uploadId = $uploadSession.data.id
$presignedUrl = $uploadSession.data.presigned_url
Write-OK "上传会话已创建: $uploadId"
# 4b. PUT 文件到 S3 预签名 URL
Write-Host " 上传文件中 ($zipSizeMB MB)..." -ForegroundColor DarkGray
$putClient = [System.Net.Http.HttpClient]::new()
$fileBytes = [System.IO.File]::ReadAllBytes($zipPath)
$byteContent = [System.Net.Http.ByteArrayContent]::new($fileBytes)
$byteContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/zip")
$byteContent.Headers.ContentLength = $fileBytes.Length
$putResponse = $putClient.PutAsync($presignedUrl, $byteContent).Result
if (-not $putResponse.IsSuccessStatusCode)
{
Write-Host " [已知问题] S3 预签名 URL 签名不匹配 (NexusMods API bug)" -ForegroundColor Yellow
Write-Host " 压缩包已生成: $zipPath" -ForegroundColor Yellow
Write-Host " 请通过 NexusMods 网页手动上传。" -ForegroundColor Yellow
throw "S3 PUT 失败: $( $putResponse.StatusCode ) - 请手动上传到 NexusMods"
}
Write-OK "文件已上传"
# 4c. 确认上传
Write-Host " 确认上传..." -ForegroundColor DarkGray
Invoke-RestMethod -Uri "$nexusBase/uploads/$uploadId/finalise" `
-Method Post -Headers $nexusHeaders | Out-Null
Write-OK "上传已确认"
# 4d. 创建 Mod 文件条目
Write-Host " 创建 Mod 文件条目..." -ForegroundColor DarkGray
$createFileBody = @{
upload_id = $uploadId
mod_id = $NexusModId
name = "$ModDisplayName v$ModVersion"
version = $ModVersion
file_category = 1
}
if ($NexusDescription)
{
$createFileBody["description"] = $NexusDescription
}
$modFile = Invoke-RestMethod -Uri "$nexusBase/mod-files" `
-Method Post -Headers $nexusHeaders `
-Body ($createFileBody | ConvertTo-Json) `
-ContentType "application/json"
Write-OK "Mod 文件已创建 (ID: $( $modFile.data.id ))"
Write-OK "NexusMods 上传完成!"
}
catch
{
Write-Fail "NexusMods 上传失败: $_"
if ($_.Exception.Response)
{
try
{
$reader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$errorBody = $reader.ReadToEnd()
Write-Host " API 响应: $errorBody" -ForegroundColor Red
}
catch
{
}
}
}
}
}
# ============================================================
# 5. 上传到 GitHub Release
# ============================================================
if (-not $SkipGitHub)
{
Write-Step "上传到 GitHub Release..." "Yellow"
$ghAvailable = Get-Command gh -ErrorAction SilentlyContinue
if (-not $ghAvailable)
{
Write-Fail "gh CLI 未安装。请运行: winget install GitHub.cli"
}
else
{
try
{
# 检查是否已认证
$ghAuth = & gh auth status 2>&1
if ($LASTEXITCODE -ne 0)
{
Write-Fail "gh 未认证。请运行: gh auth login"
}
else
{
$tagName = "v$ModVersion"
$notesFile = $null
try
{
# 构建 gh release create 参数
$ghArgs = @(
"release", "create", $tagName,
$zipPath,
"--title", "$ModDisplayName $tagName"
)
if ($ReleaseNotes)
{
# 使用 --notes-file 替代 --notes,避免命令行特殊字符和长度限制问题
$notesFile = [System.IO.Path]::GetTempFileName()
$ReleaseNotes | Out-File -FilePath $notesFile -Encoding UTF8 -NoNewline
$ghArgs += @("--notes-file", $notesFile)
}
else
{
$ghArgs += @("--generate-notes")
}
if ($Prerelease)
{
$ghArgs += "--prerelease"
}
Write-Host " 执行: gh $( $ghArgs -join ' ' )" -ForegroundColor DarkGray
$ghOutput = & gh @ghArgs 2>&1
$ghExitCode = $LASTEXITCODE
if ($ghExitCode -eq 0)
{
Write-OK "GitHub Release 已创建: $tagName"
}
else
{
Write-Host " gh 输出:" -ForegroundColor Red
Write-Host " $ghOutput" -ForegroundColor Red
Write-Fail "GitHub Release 创建失败 (exit code: $ghExitCode)"
$ghOutputStr = "$ghOutput"
if ($ghOutputStr -match "already exists")
{
Write-Host ""
Write-Host " [提示] Tag/Release 已存在,可能上次发布失败后残留。" -ForegroundColor Yellow
Write-Host " 手动修复:" -ForegroundColor Yellow
Write-Host " gh release delete $tagName --yes" -ForegroundColor DarkGray
Write-Host " git tag -d $tagName" -ForegroundColor DarkGray
Write-Host " git push origin :refs/tags/$tagName" -ForegroundColor DarkGray
}
}
}
finally
{
if ($notesFile -and (Test-Path $notesFile))
{
Remove-Item $notesFile -Force -ErrorAction SilentlyContinue
}
}
}
}
catch
{
Write-Fail "GitHub Release 失败: $_"
}
}
}
# ============================================================
# 完成
# ============================================================
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " 发布完成!" -ForegroundColor Green
Write-Host " 压缩包: $zipName" -ForegroundColor White
Write-Host " 大小: $zipSizeMB MB" -ForegroundColor White
Write-Host "========================================" -ForegroundColor Green
Write-Host ""