#!/usr/bin/env python3
"""
ASS (Advanced Streaming Software) - Helper script
This file is part of ASS.
ASS is free software: you can redistribute it and/or modify it under the terms of the Affero GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
ASS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Affero GNU
General Public License for more details.
You should have received a copy of the Affero GNU General Public License along with ASS.
If not, see
Copyright 2026 ludolpif
"""
"""
Generates /app_metadata/version.h and /VERSION from git tags (e.g. v1.2.3) or fallback.
Outputs a minimal, rc.exe-friendly header containing ONLY simple defines:
ASS_VERSION_MAJOR
ASS_VERSION_MINOR
ASS_VERSION_PATCH
ASS_VERSION_STRING
If no git tag is found, falls back to 999.999.999 (unreleased development build)
with ASS_VERSION_STRING set to the 7-character short commit hash.
Output files are meant to remain uncommitted (.gitignored).
"""
import os
import re
import subprocess
import sys
from pathlib import Path
def get_git_info(repo_dir):
# 1. Check CI environment variables (e.g. GITHUB_REF_NAME, CI_COMMIT_TAG)
for env_var in ("CI_COMMIT_TAG", "GITHUB_REF_NAME", "ASS_GIT_TAG"):
val = os.environ.get(env_var, "")
m = re.match(r"^v?(\d+)\.(\d+)\.(\d+)$", val)
if m:
return int(m.group(1)), int(m.group(2)), int(m.group(3)), f"{m.group(1)}.{m.group(2)}.{m.group(3)}"
short_hash = "0000000"
try:
cmd = ["git", "rev-parse", "--short=7", "HEAD"]
out = subprocess.check_output(cmd, cwd=repo_dir, stderr=subprocess.DEVNULL, text=True).strip()
if out:
short_hash = out[:7]
except Exception:
pass
# 2. Check exact tags on HEAD first, sorted by highest semver (--sort=-v:refname)
try:
cmd_exact = ["git", "tag", "--points-at", "HEAD", "--sort=-v:refname", "v*"]
out_exact = subprocess.check_output(cmd_exact, cwd=repo_dir, stderr=subprocess.DEVNULL, text=True).splitlines()
for line in out_exact:
m_exact = re.match(r"^v?(\d+)\.(\d+)\.(\d+)$", line.strip())
if m_exact:
major, minor, patch = int(m_exact.group(1)), int(m_exact.group(2)), int(m_exact.group(3))
return major, minor, patch, f"{major}.{minor}.{patch}"
except Exception:
pass
# 3. Not on an exact release tag: find highest reachable tag for MAJOR.MINOR, PATCH=999
tag_major, tag_minor = 999, 999
try:
cmd_merged = ["git", "tag", "--merged", "HEAD", "--sort=-v:refname", "v*"]
out_merged = subprocess.check_output(cmd_merged, cwd=repo_dir, stderr=subprocess.DEVNULL, text=True).splitlines()
for line in out_merged:
m = re.match(r"^v?(\d+)\.(\d+)(?:\.(\d+))?", line.strip())
if m:
tag_major, tag_minor = int(m.group(1)), int(m.group(2))
break
except Exception:
pass
return tag_major, tag_minor, 999, short_hash
def main():
script_dir = Path(__file__).resolve().parent
repo_dir = script_dir.parent
major, minor, patch, ver_string = get_git_info(repo_dir)
target_dir = repo_dir / "app_metadata"
target_path = target_dir / "version.h"
content = f"""#pragma once
/*
* Auto-generated by scripts/generate_version.py from git tag or CI environment.
* DO NOT COMMIT THIS FILE TO VERSION CONTROL.
*
* This file contains strictly simple #defines compatible with rc.exe (Windows Resource Compiler).
*/
#define ASS_VERSION_MAJOR {major}
#define ASS_VERSION_MINOR {minor}
#define ASS_VERSION_PATCH {patch}
#define ASS_VERSION_STRING "{ver_string}"
"""
os.makedirs(target_dir, exist_ok=True)
header_changed = True
if target_path.exists():
with open(target_path, "r", encoding="utf-8") as f:
existing = f.read()
if existing == content:
header_changed = False
if header_changed:
with open(target_path, "w", encoding="utf-8") as f:
f.write(content)
print(f"[generate_version] Generated {target_path} -> v{major}.{minor}.{patch} ({ver_string})")
# Maintain the flat VERSION file at repo root
version_file_path = repo_dir / "VERSION"
ver_content = f"{major}.{minor}.{patch}\n"
ver_changed = True
if version_file_path.exists():
with open(version_file_path, "r", encoding="utf-8") as vf:
if vf.read() == ver_content:
ver_changed = False
if ver_changed:
with open(version_file_path, "w", encoding="utf-8") as vf:
vf.write(ver_content)
print(f"[generate_version] Generated {version_file_path} -> {major}.{minor}.{patch}")
return 0
if __name__ == "__main__":
sys.exit(main())