forked from vrm-c/vrm.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_gen.py
156 lines (125 loc) · 3.88 KB
/
release_gen.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# Release 時のドキュメントを作成するスクリプト
#
# required
# pip install GitPython pyperclip
#
# 1. github release page の markdown => clipboard
# 2. changelog => docs/release/079/v0.XX.Y.md
# 3. download button => docs/index.html
#
import pathlib
import sys
import re
import io
from functools import cmp_to_key
try:
import git.repo
import pyperclip
except:
print("import error. require")
print("pip install GitPython pyperclip")
sys.exit()
HERE = pathlib.Path(__file__).absolute().parent
UNIVRM_VERSION = HERE.parent / "Assets/VRM/Runtime/Format/VRMVersion.cs"
MERGE_PATTERN = re.compile(r"Merge pull request #(\d+)")
TEMPLATE = HERE / "release_template.md"
HTML_TEMPLATE = HERE / "html_template.html"
# RELEASE_NOTE_DIR = "112"
# UNITY_VERSION = "2021.3 LTS"
RELEASE_NOTE_DIR = "128"
UNITY_VERSION = "2022.3 LTS"
def gen(template: str, version: str, hash: str):
version_hash = f"{version}_{hash[0:4]}"
values = {
"version": version,
"version_hash": version_hash,
"hash4": version_hash[0:4],
"dir": RELEASE_NOTE_DIR,
"unity_version": UNITY_VERSION,
}
def replace(m: re.Match):
return values[m.group(1)]
return re.sub(r"\$\{([^}]+)\}", replace, template)
def get_version() -> str:
m = re.search(
r'public const string VERSION = "(\d.\d+.\d)";',
UNIVRM_VERSION.read_text(encoding="utf-8"),
)
if m:
return m[1]
raise Exception("no version")
def get_hash(repo, tag_name) -> str:
# res = subprocess.check_output("git rev-parse HEAD")
# return res.decode('utf-8')
for tag in repo.tags:
if tag.name == tag_name:
return tag.commit.hexsha
raise Exception()
def copy_release_md(version: str, hash: str):
text = gen(TEMPLATE.read_text(encoding="utf-8"), version, hash)
pyperclip.copy(text)
print("copy to clipboard")
#
#
#
def change_log(repo: git.repo.Repo, version: str):
major, minor, patch = [int(x) for x in version.split(".")]
rev = f"v{major}.{minor-1}.0..v{major}.{minor}.0"
w = io.StringIO()
w.write(f"# {rev}: ChangeLog\n")
w.write("\n")
for item in repo.iter_commits(rev=rev):
if len(item.parents) > 1:
msg = item.message
if isinstance(msg, bytes):
msg = msg.decode("utf-8")
m = MERGE_PATTERN.match(msg)
if m:
# merge commit
pr = m[1]
lines = msg.split("\n")
w.write(
f"* [[\\#{pr}](https://github.com/vrm-c/UniVRM/pull/{pr})] {lines[2]}\n"
)
else:
w.write(f"* {msg}")
return w.getvalue()
def get_tags(repo):
P = re.compile(r"v(\w+)\.(\w+)\.(\w+)")
for tag in repo.tags:
m = P.match(tag.name)
if m:
yield int(m[1]), int(m[2]), int(m[3])
def main(path: str):
repo = git.repo.Repo(path)
print(repo)
def cmp_tag(l, r):
if l[0] != r[0]:
return l[0] - r[0]
if l[1] != r[1]:
return l[1] - r[1]
return l[2] - r[2]
tags = [tag for tag in get_tags(repo)]
tags = sorted(tags, key=cmp_to_key(cmp_tag))
x, y, z = tags[-1]
version = f"{x}.{y}.{z}"
hash = get_hash(repo, f"v{version}")
# 1.
copy_release_md(f"{version}", hash)
# 2.
# release = HERE / f"docs/release/{RELEASE_NOTE_DIR}/v{version}.md"
# if not release.exists():
# text = change_log(repo, f"{version}")
# release.write_text(text, encoding="utf-8")
# 3.
# (HERE / "index.html").write_text(
# gen(HTML_TEMPLATE.read_text(encoding="utf-8"), f"{version}", hash),
# encoding="utf-8",
# )
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: py release_gen.py {UNIVRM_REPO_DIR}")
sys.exit()
repo = pathlib.Path(sys.argv[1])
main(str(repo.absolute()))