-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
.release.py
85 lines (70 loc) · 2.62 KB
/
.release.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""用于发布新版本的脚本。"""
# ruff: noqa: S603, S607
import argparse
import json
import subprocess
from pathlib import Path
import tomlkit
from tomlkit.container import OutOfOrderTableProxy
from tomlkit.items import Array, Table
CHANGELOG_PREFIX = """---
sidebar: auto
---
# 更新日志
"""
parser = argparse.ArgumentParser(prog="release", description="AliceBot Release")
parser.add_argument("version", help="target version")
args = parser.parse_args()
version = args.version
def write_version_json(file: Path, version: str) -> None:
"""写入 package.json。
Args:
file: 文件路径。
version: 版本信息。
"""
with file.open(encoding="utf-8") as f:
json_file = json.load(f)
json_file["version"] = version
with file.open("w", encoding="utf-8") as f:
json.dump(json_file, f, indent=2)
f.write("\n")
def write_version_toml(file: Path, version: str, *, is_package: bool = False) -> None:
"""写入 pyproject.toml。
Args:
file: 文件路径。
version: 版本信息。
is_package: 是否是 packages 目录下的包。
"""
with file.open(encoding="utf-8") as f:
toml_file = tomlkit.load(f)
project_table = toml_file["project"]
assert isinstance(project_table, (OutOfOrderTableProxy, Table))
project_table["version"] = version
if is_package:
dependencies_array = project_table["dependencies"]
assert isinstance(dependencies_array, Array)
dependencies_array[0] = f"alicebot=={version}"
with file.open("w", encoding="utf-8") as f:
tomlkit.dump(toml_file, f) # pyright: ignore[reportUnknownMemberType]
write_version_json(Path("package.json"), version)
write_version_toml(Path("pyproject.toml"), version)
for package in Path("packages").iterdir():
if package.is_dir():
write_version_toml(package / "pyproject.toml", version, is_package=True)
subprocess.run(["pnpm", "run", "changelog"], check=True)
with Path("docs/changelog.md").open(encoding="utf-8") as f:
changelog_file = f.read()
with Path("docs/changelog.md").open("w", encoding="utf-8") as f:
f.write(
CHANGELOG_PREFIX
+ (
"\n".join(
("#" + x) if x.startswith("# ") else x
for x in changelog_file.split("\n")
)
).replace("_", "\\_")
)
subprocess.run(["pnpm", "exec", "prettier", "--write", "docs/changelog.md"], check=True)
subprocess.run(["uv", "sync", "--all-extras", "--dev", "--upgrade"], check=True)
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "chore: 发布 " + version], check=True)