335 lines
12 KiB
PowerShell
335 lines
12 KiB
PowerShell
#Requires -Version 7.0
|
||
<#
|
||
.SYNOPSIS
|
||
Build GitHub Release notes for a Reims-Go app release (stable, alpha, or dev).
|
||
|
||
.DESCRIPTION
|
||
- stable: extracts the matching `## <Tag>` section from CHANGELOG.md and fails
|
||
fast if the entry is missing or empty.
|
||
- alpha: uses `## <Tag>` from CHANGELOG.md when present, otherwise summarizes
|
||
git commits since the latest stable tag and marks the release as temporary.
|
||
- dev: generates rolling development release notes with version, commit SHA,
|
||
UTC build time, and git commits since the latest stable tag.
|
||
#>
|
||
[CmdletBinding()]
|
||
param(
|
||
[Parameter(Mandatory = $true)]
|
||
[string]$Tag,
|
||
[ValidateSet('auto', 'stable', 'alpha', 'dev')]
|
||
[string]$Channel = 'auto',
|
||
[string]$ChangelogPath = 'CHANGELOG.md',
|
||
[string]$OutFile = '',
|
||
[string]$Repo = 'Nwflower/Reims-Go',
|
||
[string]$PrevTag = '',
|
||
[string]$Commit = '',
|
||
[string]$StableRuntimeTag = '',
|
||
[string]$StableRuntimeTitle = '',
|
||
[string]$Summary = ''
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
|
||
if ($Tag -notmatch '^v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$') {
|
||
throw "Invalid release tag '$Tag' (expected semantic version starting with 'v', e.g. v0.2.9, v0.2.9-alpha.1, or v0.2.9-dev-66da194)."
|
||
}
|
||
|
||
$resolvedChannel = $Channel
|
||
if ($resolvedChannel -eq 'auto') {
|
||
if ($Tag -match '^v[0-9]+\.[0-9]+\.[0-9]+$') {
|
||
$resolvedChannel = 'stable'
|
||
} elseif ($Tag -match '^v[0-9]+\.[0-9]+\.[0-9]+-dev(?:[.-][0-9A-Za-z.-]+)?$') {
|
||
$resolvedChannel = 'dev'
|
||
} else {
|
||
$resolvedChannel = 'alpha'
|
||
}
|
||
}
|
||
|
||
$utf8NoBom = [Text.UTF8Encoding]::new($false)
|
||
|
||
function Find-LatestStableGitTag {
|
||
param([string]$ExcludeTag = '')
|
||
try {
|
||
$tags = @(git tag --list 'v*' 2>$null)
|
||
if ($LASTEXITCODE -ne 0 -or $tags.Count -eq 0) {
|
||
return ''
|
||
}
|
||
$parsed = foreach ($t in $tags) {
|
||
$trimmed = $t.Trim()
|
||
if ($trimmed -eq $ExcludeTag) { continue }
|
||
if ($trimmed -match '^v([0-9]+)\.([0-9]+)\.([0-9]+)$') {
|
||
[PSCustomObject]@{
|
||
Tag = $trimmed
|
||
Major = [int]$Matches[1]
|
||
Minor = [int]$Matches[2]
|
||
Patch = [int]$Matches[3]
|
||
}
|
||
}
|
||
}
|
||
$best = $parsed | Sort-Object Major, Minor, Patch -Descending | Select-Object -First 1
|
||
if ($best) { return $best.Tag }
|
||
} catch {
|
||
return ''
|
||
}
|
||
return ''
|
||
}
|
||
|
||
function Get-GitCommitBullets {
|
||
param(
|
||
[string]$BaseTag,
|
||
[int]$MaxItems = 50
|
||
)
|
||
$prevEncoding = [Console]::OutputEncoding
|
||
try {
|
||
[Console]::OutputEncoding = $utf8NoBom
|
||
$range = if (-not [string]::IsNullOrWhiteSpace($BaseTag)) { "$BaseTag..HEAD" } else { 'HEAD' }
|
||
$args = @('-c', 'i18n.logOutputEncoding=utf-8', 'log', $range, '--no-merges', '--pretty=format:- `%h` %s')
|
||
if ([string]::IsNullOrWhiteSpace($BaseTag)) {
|
||
$args += @('-n', "$MaxItems")
|
||
}
|
||
$rawLines = @(git @args 2>$null)
|
||
if ($LASTEXITCODE -ne 0) {
|
||
return @()
|
||
}
|
||
$lines = @($rawLines | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
|
||
if ($lines.Count -gt $MaxItems) {
|
||
$omitted = $lines.Count - $MaxItems
|
||
$lines = @($lines[0..($MaxItems - 1)]) + @("- …(另有 $omitted 条提交,详见文末完整提交对比)")
|
||
}
|
||
return $lines
|
||
} catch {
|
||
return @()
|
||
} finally {
|
||
[Console]::OutputEncoding = $prevEncoding
|
||
}
|
||
}
|
||
|
||
function Read-ChangelogSection {
|
||
param(
|
||
[string]$Path,
|
||
[string]$TargetTag
|
||
)
|
||
$result = [PSCustomObject]@{
|
||
Found = $false
|
||
HasContent = $false
|
||
LeadLines = [Collections.Generic.List[string]]::new()
|
||
BodyLines = [Collections.Generic.List[string]]::new()
|
||
DetectedPrevTag = ''
|
||
}
|
||
if (-not (Test-Path -LiteralPath $Path)) {
|
||
return $result
|
||
}
|
||
$raw = [IO.File]::ReadAllText((Resolve-Path -LiteralPath $Path).Path, $utf8NoBom)
|
||
$lines = $raw -split "\r?\n"
|
||
$headingPattern = '^##\s+(?<tag>v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?)(?:\s|$|[—–\-\(])'
|
||
|
||
$inSection = $false
|
||
$sectionLines = [Collections.Generic.List[string]]::new()
|
||
|
||
foreach ($line in $lines) {
|
||
if ($line -match '^##\s+') {
|
||
if ($inSection) {
|
||
if ($line -match $headingPattern) {
|
||
$result.DetectedPrevTag = $Matches['tag']
|
||
}
|
||
break
|
||
}
|
||
if ($line -match $headingPattern -and $Matches['tag'] -eq $TargetTag) {
|
||
$inSection = $true
|
||
$result.Found = $true
|
||
continue
|
||
}
|
||
} elseif ($inSection) {
|
||
$sectionLines.Add($line)
|
||
}
|
||
}
|
||
|
||
if (-not $result.Found) {
|
||
return $result
|
||
}
|
||
|
||
while ($sectionLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($sectionLines[0])) {
|
||
$sectionLines.RemoveAt(0)
|
||
}
|
||
while ($sectionLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($sectionLines[$sectionLines.Count - 1])) {
|
||
$sectionLines.RemoveAt($sectionLines.Count - 1)
|
||
}
|
||
|
||
foreach ($l in $sectionLines) {
|
||
$t = $l.Trim()
|
||
if ($t -ne '' -and $t -notmatch '^#+\s+') {
|
||
$result.HasContent = $true
|
||
break
|
||
}
|
||
}
|
||
if (-not $result.HasContent) {
|
||
return $result
|
||
}
|
||
|
||
$seenSubheading = $false
|
||
foreach ($l in $sectionLines) {
|
||
if (-not $seenSubheading -and $l -match '^(?:###\s+|<h3\s+id="cn-[^"]+">)') {
|
||
$seenSubheading = $true
|
||
}
|
||
if ($seenSubheading) {
|
||
$result.BodyLines.Add($l)
|
||
} else {
|
||
$result.LeadLines.Add($l)
|
||
}
|
||
}
|
||
|
||
if (-not $seenSubheading) {
|
||
$result.BodyLines.Add('### 变更说明')
|
||
$result.BodyLines.Add('')
|
||
foreach ($l in $result.LeadLines) { $result.BodyLines.Add($l) }
|
||
$result.LeadLines.Clear()
|
||
} else {
|
||
while ($result.LeadLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($result.LeadLines[$result.LeadLines.Count - 1])) {
|
||
$result.LeadLines.RemoveAt($result.LeadLines.Count - 1)
|
||
}
|
||
}
|
||
|
||
return $result
|
||
}
|
||
|
||
$leadLines = [Collections.Generic.List[string]]::new()
|
||
$bodyLines = [Collections.Generic.List[string]]::new()
|
||
$detectedPrevTag = ''
|
||
|
||
if ($resolvedChannel -eq 'stable') {
|
||
if (-not (Test-Path -LiteralPath $ChangelogPath)) {
|
||
throw "Changelog file not found: $ChangelogPath"
|
||
}
|
||
$sec = Read-ChangelogSection -Path $ChangelogPath -TargetTag $Tag
|
||
if (-not $sec.Found) {
|
||
throw "CHANGELOG.md does not contain a '## $Tag' section. Add release notes for $Tag to $ChangelogPath before releasing."
|
||
}
|
||
if (-not $sec.HasContent) {
|
||
throw "CHANGELOG.md section '## $Tag' has no release note entries."
|
||
}
|
||
$leadLines = $sec.LeadLines
|
||
$bodyLines = $sec.BodyLines
|
||
$detectedPrevTag = $sec.DetectedPrevTag
|
||
} elseif ($resolvedChannel -eq 'alpha') {
|
||
$sec = Read-ChangelogSection -Path $ChangelogPath -TargetTag $Tag
|
||
if ($sec.Found -and $sec.HasContent) {
|
||
$leadLines = $sec.LeadLines
|
||
$bodyLines = $sec.BodyLines
|
||
$detectedPrevTag = $sec.DetectedPrevTag
|
||
} else {
|
||
if ([string]::IsNullOrWhiteSpace($PrevTag)) {
|
||
$detectedPrevTag = Find-LatestStableGitTag -ExcludeTag $Tag
|
||
}
|
||
$base = if (-not [string]::IsNullOrWhiteSpace($PrevTag)) { $PrevTag } else { $detectedPrevTag }
|
||
$commits = Get-GitCommitBullets -BaseTag $base
|
||
$heading = if (-not [string]::IsNullOrWhiteSpace($base)) { "### 自 $base 以来的提交" } else { '### 近期提交' }
|
||
$bodyLines.Add($heading)
|
||
$bodyLines.Add('')
|
||
if ($commits.Count -gt 0) {
|
||
foreach ($c in $commits) { $bodyLines.Add($c) }
|
||
} elseif (-not [string]::IsNullOrWhiteSpace($base)) {
|
||
$bodyLines.Add("- 当前提交与 $base 一致,无新增提交。")
|
||
} else {
|
||
$bodyLines.Add('- 详见仓库提交历史。')
|
||
}
|
||
}
|
||
} else {
|
||
# dev (rolling)
|
||
if ([string]::IsNullOrWhiteSpace($Commit)) {
|
||
try {
|
||
$Commit = (git rev-parse --short=7 HEAD 2>$null | Out-String).Trim()
|
||
} catch {
|
||
$Commit = ''
|
||
}
|
||
}
|
||
if ([string]::IsNullOrWhiteSpace($PrevTag)) {
|
||
$detectedPrevTag = Find-LatestStableGitTag -ExcludeTag $Tag
|
||
}
|
||
$base = if (-not [string]::IsNullOrWhiteSpace($PrevTag)) { $PrevTag } else { $detectedPrevTag }
|
||
$commits = Get-GitCommitBullets -BaseTag $base
|
||
$heading = if (-not [string]::IsNullOrWhiteSpace($base)) { "### 自 $base 以来的提交" } else { '### 近期提交' }
|
||
$bodyLines.Add($heading)
|
||
$bodyLines.Add('')
|
||
if ($commits.Count -gt 0) {
|
||
foreach ($c in $commits) { $bodyLines.Add($c) }
|
||
} elseif (-not [string]::IsNullOrWhiteSpace($base)) {
|
||
$bodyLines.Add("- 当前提交与 $base 一致,无新增提交。")
|
||
} else {
|
||
$bodyLines.Add('- 详见仓库提交历史。')
|
||
}
|
||
}
|
||
|
||
if ([string]::IsNullOrWhiteSpace($PrevTag)) {
|
||
$PrevTag = $detectedPrevTag
|
||
}
|
||
|
||
$blocks = [Collections.Generic.List[string]]::new()
|
||
|
||
if ($resolvedChannel -eq 'alpha') {
|
||
$blocks.Add(@"
|
||
> [!NOTE]
|
||
> 这是 **测试版(Alpha 预发布)**,用于正式版发布前的验证与反馈;下一正式版发布后,所有已发行的测试版将被自动清理。日常使用建议安装 [最新正式版](https://github.com/$Repo/releases/latest)。
|
||
"@.Trim())
|
||
} elseif ($resolvedChannel -eq 'dev') {
|
||
$builtAt = (Get-Date).ToUniversalTime().ToString('yyyy-MM-dd HH:mm:ss') + ' UTC'
|
||
$commitPart = if (-not [string]::IsNullOrWhiteSpace($Commit)) { ",提交 ``$Commit``" } else { '' }
|
||
$blocks.Add(@"
|
||
> [!WARNING]
|
||
> 这是 ``main`` 分支每次提交自动滚动更新的 **开发版(Dev)**,当前构建版本为 ``$Tag``$commitPart(构建时间:$builtAt)。开发版可能包含未完成或破坏性改动,仅供开发与尝鲜测试;日常使用请下载 [最新正式版](https://github.com/$Repo/releases/latest)。
|
||
"@.Trim())
|
||
}
|
||
|
||
if (-not [string]::IsNullOrWhiteSpace($Summary)) {
|
||
$blocks.Add("Reims-Go ${Tag}:$($Summary.Trim())")
|
||
} elseif ($leadLines.Count -gt 0) {
|
||
$blocks.Add(($leadLines -join "`n").Trim())
|
||
}
|
||
|
||
$downloadLines = @(
|
||
'### 下载'
|
||
''
|
||
"- ``reims-go-$Tag-windows-amd64.exe``:单文件,免安装。双击打开界面,带子命令即在终端运行(如 ``reims-go vm list``);"
|
||
"- ``reims-go-$Tag-windows-amd64.zip``:较小的下载,解压后得到相同的 EXE。"
|
||
'- `SHA256SUMS.txt` 包含 EXE 和 ZIP 的 SHA256。'
|
||
)
|
||
if (-not [string]::IsNullOrWhiteSpace($StableRuntimeTag)) {
|
||
$rtLabel = if (-not [string]::IsNullOrWhiteSpace($StableRuntimeTitle)) { $StableRuntimeTitle } else { $StableRuntimeTag }
|
||
$downloadLines += ''
|
||
$downloadLines += "当前稳定版运行时为 [$rtLabel](https://github.com/$Repo/releases/tag/$StableRuntimeTag),已经装过的不用重装。"
|
||
}
|
||
$blocks.Add(($downloadLines -join "`n"))
|
||
|
||
$blocks.Add(($bodyLines -join "`n").Trim())
|
||
|
||
$blocks.Add(@"
|
||
### 已知限制
|
||
|
||
- 没有代码签名,第一次运行时 Windows SmartScreen 会拦截,需要点「更多信息」→「仍要运行」。
|
||
"@.Trim())
|
||
|
||
$blocks.Add(@"
|
||
### 许可证
|
||
|
||
Reims-Go 自身代码采用 Apache-2.0 许可证。运行时组件按各自的 GPL/LGPL 许可证分发,对应源码与许可证文本随运行时 Release 一起提供。
|
||
|
||
本应用不包含、也不分发任何 Apple 系统文件;macOS 安装包由你的电脑直接从 Apple 服务器下载。macOS 的软件许可只允许在 Apple 硬件上运行 macOS 虚拟机,使用前请自行确认合规。
|
||
"@.Trim())
|
||
|
||
$compareRef = if ($resolvedChannel -eq 'dev' -and -not [string]::IsNullOrWhiteSpace($Commit)) { $Commit } else { $Tag }
|
||
if (-not [string]::IsNullOrWhiteSpace($PrevTag)) {
|
||
$blocks.Add("完整更新日志见 [CHANGELOG.md](https://github.com/$Repo/blob/main/CHANGELOG.md) · 完整提交对比:[$PrevTag...$compareRef](https://github.com/$Repo/compare/$PrevTag...$compareRef)。")
|
||
} else {
|
||
$blocks.Add("完整更新日志见 [CHANGELOG.md](https://github.com/$Repo/blob/main/CHANGELOG.md)。")
|
||
}
|
||
|
||
$output = ($blocks -join "`n`n") + "`n"
|
||
|
||
if (-not [string]::IsNullOrWhiteSpace($OutFile)) {
|
||
$parent = Split-Path -Parent $OutFile
|
||
if (-not [string]::IsNullOrWhiteSpace($parent)) {
|
||
New-Item -ItemType Directory -Force -Path $parent | Out-Null
|
||
}
|
||
[IO.File]::WriteAllText($OutFile, $output, $utf8NoBom)
|
||
} else {
|
||
[Console]::Out.Write($output)
|
||
}
|