Files
Reims-Go/scripts/build-release-notes.ps1
T

174 lines
5.8 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#Requires -Version 7.0
<#
.SYNOPSIS
Build GitHub Release notes for a Reims-Go app tag from CHANGELOG.md.
.DESCRIPTION
Extracts the matching `## <Tag>` section from CHANGELOG.md and wraps it with
download, stable runtime, known limitation, license, and compare links.
Fails fast if CHANGELOG.md has no entry for -Tag or the section is empty,
preventing releases with empty/auto-only notes.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Tag,
[string]$ChangelogPath = 'CHANGELOG.md',
[string]$OutFile = '',
[string]$Repo = 'Nwflower/Reims-Go',
[string]$PrevTag = '',
[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.5)."
}
if (-not (Test-Path -LiteralPath $ChangelogPath)) {
throw "Changelog file not found: $ChangelogPath"
}
$utf8NoBom = [Text.UTF8Encoding]::new($false)
$raw = [IO.File]::ReadAllText((Resolve-Path -LiteralPath $ChangelogPath).Path, $utf8NoBom)
$lines = $raw -split "\r?\n"
$headingPattern = '^##\s+(?<tag>v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?)(?:\s|$|[—–\-\(])'
$inSection = $false
$found = $false
$sectionLines = [Collections.Generic.List[string]]::new()
$detectedPrevTag = ''
foreach ($line in $lines) {
if ($line -match '^##\s+') {
if ($inSection) {
if ($line -match $headingPattern) {
$detectedPrevTag = $Matches['tag']
}
break
}
if ($line -match $headingPattern -and $Matches['tag'] -eq $Tag) {
$inSection = $true
$found = $true
continue
}
} elseif ($inSection) {
$sectionLines.Add($line)
}
}
if (-not $found) {
throw "CHANGELOG.md does not contain a '## $Tag' section. Add release notes for $Tag to $ChangelogPath before releasing."
}
# Trim leading and trailing blank lines.
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)
}
$hasContent = $false
foreach ($l in $sectionLines) {
$t = $l.Trim()
if ($t -ne '' -and $t -notmatch '^#+\s+') {
$hasContent = $true
break
}
}
if (-not $hasContent) {
throw "CHANGELOG.md section '## $Tag' has no release note entries."
}
# If CHANGELOG.md has an introductory paragraph before the first ### subheading,
# place it at the top of the release notes (above ### 下载).
$leadLines = [Collections.Generic.List[string]]::new()
$bodyLines = [Collections.Generic.List[string]]::new()
$seenSubheading = $false
foreach ($l in $sectionLines) {
if (-not $seenSubheading -and $l -match '^###\s+') {
$seenSubheading = $true
}
if ($seenSubheading) {
$bodyLines.Add($l)
} else {
$leadLines.Add($l)
}
}
if (-not $seenSubheading) {
# Section has prose/bullets without ### subheadings: keep them under ### 变更说明.
$bodyLines.Add('### 变更说明')
$bodyLines.Add('')
foreach ($l in $leadLines) { $bodyLines.Add($l) }
$leadLines.Clear()
} else {
while ($leadLines.Count -gt 0 -and [string]::IsNullOrWhiteSpace($leadLines[$leadLines.Count - 1])) {
$leadLines.RemoveAt($leadLines.Count - 1)
}
}
if ([string]::IsNullOrWhiteSpace($PrevTag)) {
$PrevTag = $detectedPrevTag
}
$blocks = [Collections.Generic.List[string]]::new()
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``);"
'- `SHA256SUMS.txt`:上面文件的 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())
if (-not [string]::IsNullOrWhiteSpace($PrevTag)) {
$blocks.Add("完整更新日志见 [CHANGELOG.md](https://github.com/$Repo/blob/main/CHANGELOG.md) · 完整提交对比:[$PrevTag...$Tag](https://github.com/$Repo/compare/$PrevTag...$Tag)。")
} 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)
}