Skip to content

Commit d3a6fbb

Browse files
ci(release): 自动更新版本号 (#6)
* ci(release): 自动更新版本号 * ci(release): PYTHONIOENCODING: utf-8 * 在更新版本号前 .strip() 并 检查版本号是否为空 * 补全更新版本号步骤中的 env 定义 * 修正日志输出顺序
1 parent ce317bc commit d3a6fbb

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

.github/workflows/release.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ name: Release
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: 版本号 (不带v)
8+
required: true
9+
default: 2.3.3
510
release:
611
types: [published]
712

@@ -11,6 +16,8 @@ permissions:
1116
jobs:
1217
release:
1318
runs-on: windows-latest
19+
env:
20+
PYTHONIOENCODING: utf-8
1421

1522
steps:
1623
- name: 检出代码
@@ -23,6 +30,25 @@ jobs:
2330
with:
2431
dotnet-version: 10.x
2532

33+
- name: 配置 Python
34+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
35+
with:
36+
python-version: 3.x
37+
38+
- name: 更新版本号
39+
shell: bash
40+
env:
41+
GITHUB_EVENT_INPUTS_VERSION: ${{ github.event.inputs.version }}
42+
run: |-
43+
if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
44+
version="${GITHUB_EVENT_INPUTS_VERSION#v}"
45+
else
46+
version="${GITHUB_REF_NAME#refs/tags/}"
47+
version="${version#v}"
48+
fi
49+
50+
python ".scripts/update_version.py" "$version"
51+
2652
- name: 发布
2753
run: dotnet publish PinAction --configuration Release
2854

.scripts/update_version.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
更新 PinAction/Program.cs 和 Installer.iss 中的版本号。
3+
"""
4+
5+
import os.path
6+
import sys
7+
8+
9+
def update_ver(root: str, path: str, old: str, new: str, max_matches: int = 1):
10+
"""
11+
更新文件中的版本号
12+
13+
Args:
14+
root (str): 相对路径 path 基于的路径
15+
path (str): 文件基于 root 的相对路径
16+
old (str): 旧内容
17+
new (str): 新内容
18+
max_matches (int): 最大匹配行数。默认为 1
19+
"""
20+
21+
if max_matches < 1:
22+
raise ValueError("最大匹配次数 max_matches 不应小于 1")
23+
24+
path = os.path.normpath(os.path.join(root, path))
25+
26+
matches: int = 0
27+
with open(path, "r", encoding="utf-8") as f:
28+
lines: list[str] = f.readlines()
29+
30+
for index, line in enumerate(lines):
31+
if old in line.rstrip("\n"):
32+
matches += 1
33+
new_line: str = line.replace(old, new, 1)
34+
# pylint: disable=C0301:line-too-long
35+
print(f"({matches}/{max_matches}) 匹配 {os.path.relpath(path, root)}{index+1} 行: \"{lines[index].rstrip("\n")}\" -> \"{new_line.rstrip("\n")}\"")
36+
lines[index] = new_line
37+
38+
if matches == max_matches:
39+
break
40+
41+
with open(path, "w", encoding="utf-8") as f:
42+
f.writelines(lines)
43+
44+
45+
def main(args: list[str]) -> int:
46+
"""
47+
入口函数
48+
49+
Args:
50+
args (list[str]): 命令行参数
51+
52+
Returns:
53+
int: 退出代码
54+
"""
55+
56+
if len(args) != 1:
57+
print("使用方法: update_version.py <version>")
58+
return 1
59+
60+
version: str = args[0].strip()
61+
if not version:
62+
raise ValueError("版本号不能为空")
63+
print(f"版本号: {version}")
64+
65+
# os.path.dirname(__file__) -> .scripts/(update_version.py)
66+
# os.path.dirname(os.path.dirname(__file__)) -> ./[.scripts/(update_version.py)]
67+
root: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
68+
69+
# 更新版本号
70+
update_ver(
71+
root,
72+
"installer.iss",
73+
"AppVersion=develop",
74+
f"AppVersion={version}"
75+
)
76+
update_ver(
77+
root,
78+
"PinAction/Program.cs",
79+
# pylint: disable=C0301:line-too-long
80+
# 不是 f-string 的不用转义 {}
81+
'AnsiConsole.MarkupLine($"PinAction {Strings.Version} [green]develop[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]");',
82+
f'AnsiConsole.MarkupLine($"PinAction {{Strings.Version}} [green]{version}[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]");',
83+
)
84+
85+
return 0
86+
87+
88+
if __name__ == "__main__":
89+
sys.exit(main(sys.argv[1:]))

Installer.iss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Setup]
22
AppName=PinAction
3-
AppVersion=1.0.1
3+
AppVersion=develop
44
DefaultDirName={pf}\DuckStudio\PinAction
55
VersionInfoCopyright=版权所有 (c) 2026 鸭鸭「カモ」
66
LicenseFile=LICENSE.txt

PinAction/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int Main(string[] args)
4848
case "--version":
4949
case "--ver":
5050
case "-v":
51-
AnsiConsole.MarkupLine($"PinAction {Strings.Version} [green]1.0.1[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]");
51+
AnsiConsole.MarkupLine($"PinAction {Strings.Version} [green]develop[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]");
5252
Console.WriteLine();
5353
AnsiConsole.MarkupLine(Strings.HelpVer2License);
5454
return 0;

0 commit comments

Comments
 (0)