feat(version): resolve the version from a generated module

A checkout carries no version of its own. The release stamps
hermes_cli/_version.py into the build tree, and a tree without it reports
0.0.0 rather than a number read out of git.
This commit is contained in:
ethernet
2026-09-21 20:19:58 -04:00
parent 5f1d3294b9
commit 9786846da7
3 changed files with 44 additions and 1 deletions

2
.gitignore vendored
View File

@@ -7,6 +7,8 @@
/_pycache/
*.pyc*
__pycache__/
# Generated by the release into the build tree; a checkout never carries one.
hermes_cli/_version.py
act/
.act-sandbox-agent.*
.venv/

View File

@@ -3,9 +3,15 @@
import os
import sys
__version__ = "1.0.0"
__release_date__ = "2026.9.21"
# A checkout carries no version. The release stamps ``_version.py`` into the build
# tree; without it the tree reports the placeholder, never a number read from git.
try:
from hermes_cli._version import __version__ # type: ignore[import-not-found]
except ImportError:
__version__ = "0.0.0"
def _ensure_utf8():
"""Force UTF-8 stdout/stderr to prevent UnicodeEncodeError crashes.

View File

@@ -0,0 +1,35 @@
"""The shipped version comes from a generated module, never from the tree.
A checkout carries no version of its own: the release stamps ``hermes_cli/_version.py``
into the build tree, and a tree without that file reports the placeholder.
"""
import subprocess
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parents[2]
def _version_of(tree: Path) -> str:
env = {"PYTHONPATH": str(tree)}
script = "import hermes_cli; print(hermes_cli.__version__)"
out = subprocess.run([sys.executable, "-c", script], cwd=tree, env=env,
capture_output=True, text=True, check=True)
return out.stdout.strip()
def test_generated_module_wins(tmp_path):
cli = tmp_path / "hermes_cli"
cli.mkdir()
(cli / "__init__.py").write_text((REPO / "hermes_cli" / "__init__.py").read_text())
(cli / "_version.py").write_text('__version__ = "0.21.5"\n')
assert _version_of(tmp_path) == "0.21.5"
def test_absent_generated_module_is_the_placeholder(tmp_path):
cli = tmp_path / "hermes_cli"
cli.mkdir()
(cli / "__init__.py").write_text((REPO / "hermes_cli" / "__init__.py").read_text())
assert _version_of(tmp_path) == "0.0.0"