|
| 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:])) |
0 commit comments