forked from ChinaGodMan/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_version.py
More file actions
74 lines (63 loc) · 2.86 KB
/
Copy pathupdate_version.py
File metadata and controls
74 lines (63 loc) · 2.86 KB
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
import datetime
import os
import re
import subprocess
from searcher import search_in_file
# https://github.com/murdos/musicbrainz-userscripts/blob/master/tools/update_version.py
def make_version_line(old_value='0.0.0.0', spacing=' ' * 8, eol="\n"):
prev_version = [int(x) for x in old_value.split('.')]
now = datetime.datetime.now()
version = [now.year, now.month, now.day, 1]
if prev_version[:3] == version[:3]:
version[3] = prev_version[3] + 1
version_str = '%04d.%d.%d.%d' % tuple(version)
return ('// @version' + spacing + version_str, version_str)
def process_files(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
version_match = re.search(r'(//\s*@version)(\s+)(\S+)', content)
if version_match:
spaces = version_match.group(2) # 匹配中间的空格数
old_version = version_match.group(3) # 匹配旧版本号
updated_line, version_str = make_version_line(old_version, spaces)
print(f"{file_path}:{old_version} (old)")
print(f"{file_path}:{version_str} (new)")
updated_content = content.replace(version_match.group(0), updated_line)
with open(file_path, 'w', encoding='utf-8', newline='\n') as file:
file.write(updated_content)
return version_str
def list_userjs():
result = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True, check=True)
modified_files = []
for line in result.stdout.splitlines():
status = line[:2].strip()
file_path = line[3:].strip()
if status in {"M", "A", "R", "D"}:
if ".user.js" in file_path:
modified_files.append(file_path)
return modified_files
def update_log(md_path, lines_to_add):
with open(md_path, "r", encoding="utf-8") as f:
original_content = f.readlines()
# 在脚本名称后插入新的更新段.
updated_content = original_content[:1] + lines_to_add + original_content[1:]
with open(md_path, "w", encoding="utf-8", newline='\n') as f:
f.writelines("\n" + line + "\n" if not line.endswith("\n") else line for line in updated_content)
if __name__ == "__main__":
modified_files = list_userjs()
if modified_files:
for file in modified_files:
new = process_files(file)
results = search_in_file(file, 'zh-CN')
name = results.name_matches[0]
change_log = os.path.dirname(file) + "/CHANGELOG.md"
if not os.path.exists(change_log):
with open(change_log, 'w', encoding='utf-8', newline='\n') as f:
f.write(f"# **🛠️ {name} 更新日志**\n")
lines = [
f"### **📅 {new}**",
"**新增**: 说明....<br>",
"---",
]
update_log(change_log, lines)
os.system(f"code {change_log}")