- semantic versioning - auto update on build - git hash zusätzlich - Anzeige per cli und im Fenster
91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
"""Update version in config.json with Git commit hash."""
|
|
|
|
import json
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def get_git_commit_hash() -> str:
|
|
"""Get the short Git commit hash. Return 'unknown' if Git not available."""
|
|
try:
|
|
result = subprocess.run(
|
|
["git", "rev-parse", "--short", "HEAD"],
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=Path(__file__).parent,
|
|
)
|
|
if result.returncode == 0:
|
|
return result.stdout.strip()
|
|
return "unknown"
|
|
except Exception:
|
|
return "unknown"
|
|
|
|
|
|
def parse_version(version_str: str) -> tuple[int, int, int]:
|
|
"""Parse semantic version and return (major, minor, patch)."""
|
|
# Remove any build metadata (e.g., "+abc123")
|
|
base_version = version_str.split("+")[0].strip()
|
|
|
|
match = re.match(r"^(\d+)\.(\d+)\.(\d+)$", base_version)
|
|
if not match:
|
|
raise ValueError(f"Invalid semantic version format: {base_version}")
|
|
|
|
return int(match.group(1)), int(match.group(2)), int(match.group(3))
|
|
|
|
|
|
def increment_patch_version(version_str: str) -> str:
|
|
"""Increment patch version (e.g., 1.0.0 → 1.0.1)."""
|
|
major, minor, patch = parse_version(version_str)
|
|
return f"{major}.{minor}.{patch + 1}"
|
|
|
|
|
|
def update_version() -> None:
|
|
"""Update config.json version with incremented patch and Git hash."""
|
|
config_path = Path(__file__).parent / "config.json"
|
|
|
|
if not config_path.exists():
|
|
print(f"[ERROR] config.json not found at {config_path}")
|
|
sys.exit(1)
|
|
|
|
# Load config
|
|
try:
|
|
with open(config_path, "r", encoding="utf-8") as f:
|
|
config = json.load(f)
|
|
except Exception as exc:
|
|
print(f"[ERROR] Failed to read config.json: {exc}")
|
|
sys.exit(1)
|
|
|
|
# Get current version
|
|
current_version = config.get("version", "1.0.0")
|
|
print(f"Current version: {current_version}")
|
|
|
|
# Increment patch version
|
|
new_base_version = increment_patch_version(current_version)
|
|
print(f"New version (base): {new_base_version}")
|
|
|
|
# Get Git commit hash
|
|
git_hash = get_git_commit_hash()
|
|
print(f"Git commit hash: {git_hash}")
|
|
|
|
# Combine version with Git hash (Semantic Versioning build metadata)
|
|
new_version = f"{new_base_version}+{git_hash}"
|
|
print(f"Final version: {new_version}")
|
|
|
|
# Update config
|
|
config["version"] = new_version
|
|
|
|
# Write back to config.json
|
|
try:
|
|
with open(config_path, "w", encoding="utf-8") as f:
|
|
json.dump(config, f, indent=4, ensure_ascii=False)
|
|
print(f"\n✓ Version updated in config.json")
|
|
except Exception as exc:
|
|
print(f"[ERROR] Failed to write config.json: {exc}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
update_version()
|