Files
EasyDesignCollection/scripts/publish_release.ps1
T

172 lines
8.4 KiB
PowerShell
Raw Normal View History

# ============================================================
# 发布新版本到 Gitea Releases(更新说明 + 安装包附件)
#
# 用法(在仓库根目录或任意位置):
# powershell -File scripts\publish_release.ps1 -Version 1.2.1 `
# -NotesFile release\notes\RELEASE_NOTES_v1.2.1.md
#
# 说明:
# - 更新说明写入 Release 正文(Markdown),客户端「检查更新」时读取展示;
# - 安卓 APK:默认取 release\android\EasyDesignCollection_android_v<版本>_release.apk;
# - Windows 绿色版:默认把 release\windows\EasyDesignCollection\ 打包为 zip
# (客户端下载后原地解压覆盖;zip 内部必须平铺,脚本自动生成于 release\windows\);
# - 安装包作为 Release 附件保存,不进入 git 历史,仓库保持精简;
# - 发布幂等:v<版本> 已存在(如上次上传中断)时复用它并更新说明;
# - 发布完成后自动清理历史附件:只保留「最新 1 个版本」的安装包
# (Release 记录与更新说明保留,仅删除更早版本的附件,释放服务器空间)。
# ============================================================
param(
[Parameter(Mandatory = $true)][string]$Version,
[Parameter(Mandatory = $true)][string]$NotesFile,
[string]$ApkPath = "",
[string]$WindowsZipPath = "",
[string]$Server = "http://124.220.63.9:30000",
[string]$Owner = "admin",
[string]$Repo = "EasyDesignCollection", # 2026-10 由 Sequence-Diagram 改名(旧名保留 301 重定向)
[string]$Auth = "liuyu65@126.com:ds3317210",
# 上传限速(curl --limit-rate):服务器带宽很小(5Mbps),
# 限速上传避免打满带宽把服务器压垮(默认 320KB/s ≈ 2.6Mbps)。
[string]$UploadLimit = "320k"
)
$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot # 仓库根目录(scripts 的上一级)
# ---- 1) 定位安装包 ----
if ($ApkPath -eq "") {
$ApkPath = Join-Path $root "release\android\EasyDesignCollection_android_v${Version}_release.apk"
}
if (!(Test-Path $ApkPath)) {
throw "找不到 APK:$ApkPath(先执行 flutter build apk --release 并归档到 release\)"
}
if ($WindowsZipPath -eq "") {
$WindowsZipPath = Join-Path $root "release\windows\EasyDesignCollection_windows_v${Version}.zip"
}
if (!(Test-Path $WindowsZipPath) -or (Get-Item $WindowsZipPath).Length -lt 1048576) {
$winDir = Join-Path $root "release\windows\EasyDesignCollection"
if (!(Test-Path (Join-Path $winDir "EasyDesignCollection.exe"))) {
throw "找不到 release\windows\EasyDesignCollection\EasyDesignCollection.exe(先执行 flutter build windows --release 并同步到该目录)"
}
Write-Host "打包 Windows 绿色版:$WindowsZipPath"
if (Test-Path $WindowsZipPath) { Remove-Item $WindowsZipPath -Force }
& tar.exe -a -cf $WindowsZipPath -C $winDir .
if ($LASTEXITCODE -ne 0) { throw "打包失败" }
}
if (!(Test-Path $NotesFile)) { throw "找不到更新说明文件:$NotesFile" }
$notes = [System.IO.File]::ReadAllText($NotesFile, [System.Text.Encoding]::UTF8)
# ---- 2) 创建(或复用)Release(正文 = 更新说明) ----
# 幂等:若 v<版本> 已存在(如上次发布上传中断),复用该 Release 并更新说明。
$tmp = Join-Path $env:TEMP "seq_release.json"
$tagFile = Join-Path $env:TEMP "seq_release_tag.json"
curl.exe -s --max-time 300 -o $tagFile "$Server/api/v1/repos/$Owner/$Repo/releases/tags/v$Version" | Out-Null
$rel = $null
try {
$rel = [System.IO.File]::ReadAllText($tagFile, [System.Text.Encoding]::UTF8) | ConvertFrom-Json
} catch {
$rel = $null
}
if ($rel -and $rel.id) {
Write-Host "Release v$Version 已存在(id=$($rel.id)),复用并更新说明..."
$payload = @{ body = $notes } | ConvertTo-Json -Compress
[System.IO.File]::WriteAllText($tmp, $payload, (New-Object System.Text.UTF8Encoding($false)))
curl.exe -s --max-time 300 -o NUL -X PATCH -H "Content-Type: application/json" --data-binary "@$tmp" -u $Auth "$Server/api/v1/repos/$Owner/$Repo/releases/$($rel.id)" | Out-Null
} else {
$payload = @{
tag_name = "v$Version"
name = "v$Version"
body = $notes
draft = $false
prerelease = $false
} | ConvertTo-Json -Compress
[System.IO.File]::WriteAllText($tmp, $payload, (New-Object System.Text.UTF8Encoding($false)))
Write-Host "创建 Release v$Version ..."
$resp = curl.exe -s --max-time 300 -X POST -H "Content-Type: application/json" --data-binary "@$tmp" -u $Auth "$Server/api/v1/repos/$Owner/$Repo/releases"
# 响应解析容错:偶发 JSON 解析异常时按 tag 重新查询(创建可能已成功)。
$rel = $null
try { $rel = $resp | ConvertFrom-Json } catch { $rel = $null }
if (-not $rel -or -not $rel.id) {
curl.exe -s --max-time 300 -o $tagFile "$Server/api/v1/repos/$Owner/$Repo/releases/tags/v$Version" | Out-Null
try {
$rel = [System.IO.File]::ReadAllText($tagFile, [System.Text.Encoding]::UTF8) | ConvertFrom-Json
} catch { $rel = $null }
}
if (-not $rel -or -not $rel.id) { throw "创建 Release 失败:$resp" }
}
Write-Host "Release id = $($rel.id)"
# ---- 3) 上传附件 ----
# 同名附件先删后传:支持上传中断后重跑(避免重名重复)。
# 注意:大文件上传不设 --max-time(慢链路下需要数分钟)。
function Upload-Asset([string]$path) {
$name = Split-Path $path -Leaf
$curFile = Join-Path $env:TEMP "seq_release_cur.json"
curl.exe -s --max-time 300 -o $curFile "$Server/api/v1/repos/$Owner/$Repo/releases/tags/v$Version" | Out-Null
try {
$cur = [System.IO.File]::ReadAllText($curFile, [System.Text.Encoding]::UTF8) | ConvertFrom-Json
foreach ($a in $cur.assets) {
if ($a.name -eq $name) {
curl.exe -s --max-time 300 -o NUL -X DELETE -u $Auth "$Server/api/v1/repos/$Owner/$Repo/releases/$($rel.id)/assets/$($a.id)" | Out-Null
}
}
} catch {}
Write-Host "上传附件:$name (限速 $UploadLimit)..."
$r = curl.exe -s --limit-rate $UploadLimit -X POST -F "attachment=@$path" -u $Auth "$Server/api/v1/repos/$Owner/$Repo/releases/$($rel.id)/assets?name=$name"
# 解析容错:异常时按 tag 复核附件是否已存在(上传可能已成功)。
$asset = $null
try { $asset = $r | ConvertFrom-Json } catch { $asset = $null }
if (-not $asset -or -not $asset.name) {
curl.exe -s --max-time 300 -o $curFile "$Server/api/v1/repos/$Owner/$Repo/releases/tags/v$Version" | Out-Null
$okName = $false
try {
$cur2 = [System.IO.File]::ReadAllText($curFile, [System.Text.Encoding]::UTF8) | ConvertFrom-Json
foreach ($a in $cur2.assets) {
if ($a.name -eq $name) { $okName = $true; $asset = $a }
}
} catch {}
if (-not $okName) { throw "上传失败:$r" }
}
Write-Host " 完成:$($asset.browser_download_url)"
}
Upload-Asset $ApkPath
Upload-Asset $WindowsZipPath
# ---- 4) 清理历史版本附件:只保留最新 1 个版本的安装包 ----
# 客户端更新只使用「最新版本」的附件,更早版本的安装包可安全删除以释放空间;
# Release 记录与更新说明保留,历史版本页面仍可查阅。
# 该步骤失败不影响本次发布(仅打印警告)。
$keepVersions = 1
try {
Write-Host ""
Write-Host "清理历史版本附件(只保留最新 $keepVersions 个版本)..."
$listFile = Join-Path $env:TEMP "seq_releases_list.json"
curl.exe -s --max-time 300 -o $listFile "$Server/api/v1/repos/$Owner/$Repo/releases?limit=50" | Out-Null
$all = [System.IO.File]::ReadAllText($listFile, [System.Text.Encoding]::UTF8) | ConvertFrom-Json
$ordered = @($all | Sort-Object -Property created_at -Descending)
$freed = 0
for ($i = $keepVersions; $i -lt $ordered.Count; $i++) {
$r = $ordered[$i]
foreach ($a in $r.assets) {
$code = curl.exe -s --max-time 300 -o NUL -w "%{http_code}" -X DELETE -u $Auth "$Server/api/v1/repos/$Owner/$Repo/releases/$($r.id)/assets/$($a.id)"
if ($code -eq "204") {
$freed += $a.size
Write-Host " 已清理 $($r.tag_name) / $($a.name)"
} else {
Write-Host " [warn] 清理失败 $($r.tag_name) / $($a.name):HTTP $code"
}
}
}
if ($freed -gt 0) {
Write-Host " 共释放 $([math]::Round($freed / 1MB, 1)) MB"
} else {
Write-Host " 无需清理(仅最新 $keepVersions 个版本带有附件)"
}
} catch {
Write-Host " [warn] 历史附件清理失败(不影响本次发布):$_"
}
Write-Host ""
Write-Host "发布完成!客户端下次启动即可检查到 v$Version($Server/$Owner/$Repo/releases)"