diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 36ffbb3..1a63e90 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -2,6 +2,11 @@ name: Release on: workflow_dispatch: + inputs: + version: + description: 版本号 (不带v) + required: true + default: 2.3.3 release: types: [published] @@ -11,6 +16,8 @@ permissions: jobs: release: runs-on: windows-latest + env: + PYTHONIOENCODING: utf-8 steps: - name: 检出代码 @@ -23,6 +30,23 @@ jobs: with: dotnet-version: 10.x + - name: 配置 Python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: 3.x + + - name: 更新版本号 + shell: bash + run: |- + if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then + version="${GITHUB_EVENT_INPUTS_VERSION#v}" + else + version="${GITHUB_REF_NAME#refs/tags/}" + version="${version#v}" + fi + + python ".scripts/update_version.py" "$version" + - name: 发布 run: dotnet publish PinAction --configuration Release diff --git a/.scripts/update_version.py b/.scripts/update_version.py new file mode 100644 index 0000000..3b33ab8 --- /dev/null +++ b/.scripts/update_version.py @@ -0,0 +1,87 @@ +""" +更新 PinAction/Program.cs 和 Installer.iss 中的版本号。 +""" + +import os.path +import sys + + +def update_ver(root: str, path: str, old: str, new: str, max_matches: int = 1): + """ + 更新文件中的版本号 + + Args: + root (str): 相对路径 path 基于的路径 + path (str): 文件基于 root 的相对路径 + old (str): 旧内容 + new (str): 新内容 + max_matches (int): 最大匹配行数。默认为 1 + """ + + if max_matches < 1: + raise ValueError("最大匹配次数 max_matches 不应小于 1") + + path = os.path.normpath(os.path.join(root, path)) + + matches: int = 0 + with open(path, "r", encoding="utf-8") as f: + lines: list[str] = f.readlines() + + for index, line in enumerate(lines): + if old in line.rstrip("\n"): + new_line: str = line.replace(old, new, 1) + lines[index] = new_line + matches += 1 + # pylint: disable=C0301:line-too-long + print(f"({matches}/{max_matches}) 匹配 {os.path.relpath(path, root)} 第 {index+1} 行: \"{lines[index].rstrip("\n")}\" -> \"{new_line.rstrip("\n")}\"") + + if matches == max_matches: + break + + with open(path, "w", encoding="utf-8") as f: + f.writelines(lines) + + +def main(args: list[str]) -> int: + """ + 入口函数 + + Args: + args (list[str]): 命令行参数 + + Returns: + int: 退出代码 + """ + + if len(args) != 1: + print("使用方法: update_version.py ") + return 1 + + version = args[0] + print(f"版本号: {version}") + + # os.path.dirname(__file__) -> .scripts/(update_version.py) + # os.path.dirname(os.path.dirname(__file__)) -> ./[.scripts/(update_version.py)] + root: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + # 更新版本号 + update_ver( + root, + "installer.iss", + "AppVersion=develop", + f"AppVersion={version}" + ) + update_ver( + root, + "PinAction/Program.cs", + # pylint: disable=C0301:line-too-long + # 不是 f-string 的不用转义 {} + 'AnsiConsole.MarkupLine($"PinAction {Strings.Version} [green]develop[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]");', + f'AnsiConsole.MarkupLine($"PinAction {{Strings.Version}} [green]{version}[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]");', + ) + + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/Installer.iss b/Installer.iss index 71cd48d..6ac2d4b 100644 --- a/Installer.iss +++ b/Installer.iss @@ -1,6 +1,6 @@ [Setup] AppName=PinAction -AppVersion=1.0.1 +AppVersion=develop DefaultDirName={pf}\DuckStudio\PinAction VersionInfoCopyright=版权所有 (c) 2026 鸭鸭「カモ」 LicenseFile=LICENSE.txt diff --git a/PinAction/Program.cs b/PinAction/Program.cs index 17d3b4b..ee2398c 100644 --- a/PinAction/Program.cs +++ b/PinAction/Program.cs @@ -48,7 +48,7 @@ static int Main(string[] args) case "--version": case "--ver": case "-v": - AnsiConsole.MarkupLine($"PinAction {Strings.Version} [green]1.0.1[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]"); + AnsiConsole.MarkupLine($"PinAction {Strings.Version} [green]develop[/] by [link=https://duckduckstudio.github.io/yazicbs.github.io/]鸭鸭「カモ」[/]"); Console.WriteLine(); AnsiConsole.MarkupLine(Strings.HelpVer2License); return 0;