Skip to content

Commit d406e10

Browse files
committed
chore(bump): improve version handling and error reporting in bump process
1 parent b770563 commit d406e10

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

Diff for: utils/bump.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,31 @@ def update_uv_lock():
6464

6565

6666
def get_current_version():
67+
# 优先尝试从 core.__version__ 获取
6768
try:
68-
from core import __version__
69+
import core
6970

70-
return __version__
71+
importlib.reload(core)
72+
return core.__version__
7173
except ImportError:
74+
# 从 .bumpversion.cfg 获取
7275
try:
7376
with open(".bumpversion.cfg") as f:
7477
for line in f:
7578
if line.startswith("current_version"):
7679
return line.split("=")[1].strip()
77-
except Exception:
80+
except Exception as e:
81+
print(f"❌ 读取 .bumpversion.cfg 失败: {e}")
7882
pass
83+
# 从 pyproject.toml 获取
7984
try:
8085
import tomli
8186

8287
with open("pyproject.toml", "rb") as f:
8388
data = tomli.load(f)
8489
return data.get("project", {}).get("version", "未知")
85-
except Exception:
90+
except Exception as e:
91+
print(f"❌ 读取 pyproject.toml 失败: {e}")
8692
return "未知"
8793

8894

@@ -104,19 +110,20 @@ def generate_changelog(version: str):
104110
sys.exit(1)
105111

106112

107-
def git_commit_and_tag(version: str, tag: bool):
113+
def git_commit_and_tag(prev_version: str, new_version: str, tag: bool):
108114
files = ["pyproject.toml", "uv.lock", ".bumpversion.cfg"]
109115
if os.path.exists("core/__init__.py"):
110116
files.append("core/__init__.py")
111117
if os.path.exists("CHANGELOG.md"):
112118
files.append("CHANGELOG.md")
113119

114120
subprocess.run(["git", "add"] + files, check=True)
115-
subprocess.run(["git", "commit", "-m", f"bump: v{version}"], check=True)
121+
message = f"chore(release): 版本更新 v{prev_version} → v{new_version}"
122+
subprocess.run(["git", "commit", "-m", message], check=True)
116123

117124
if tag:
118-
subprocess.run(["git", "tag", f"v{version}"], check=True)
119-
print(f"✅ Git tag v{version} 已创建")
125+
subprocess.run(["git", "tag", f"v{new_version}"], check=True)
126+
print(f"✅ Git tag v{new_version} 已创建")
120127

121128

122129
def main():
@@ -171,29 +178,30 @@ def main():
171178

172179
check_bumpversion()
173180

174-
# 决定是预发布标志还是版本升级
181+
prev_version = get_current_version()
182+
183+
# 预发布标签处理
175184
pre_types = ["dev", "alpha", "beta", "rc", "release"]
176185
if args.command in pre_types:
177-
current = get_current_version()
178-
base = get_base_version(current)
186+
base = get_base_version(prev_version)
179187
new_version = base if args.command == "release" else f"{base}-{args.command}"
180188
run_bumpversion("patch", new_version=new_version)
181189
else:
182190
run_bumpversion(args.command)
183191

184-
# 更新 uv.lock 中的 version
185-
update_uv_lock()
192+
# bump 完成后重新获取新版本号
193+
new_version = get_current_version()
186194

187-
# 获取最新版本号
188-
version = get_current_version()
195+
# 更新 uv.lock
196+
update_uv_lock()
189197

190-
# 生成 changelog(可选
198+
# 生成 changelog(如指定
191199
if args.changelog:
192-
generate_changelog(version)
200+
generate_changelog(new_version)
193201

194-
# 手动提交(如果指定 --commit
202+
# 提交和打 tag(如指定
195203
if args.commit:
196-
git_commit_and_tag(version, tag=args.tag)
204+
git_commit_and_tag(prev_version, new_version, tag=args.tag)
197205

198206

199207
if __name__ == "__main__":

0 commit comments

Comments
 (0)