Skip to content

Commit b14f697

Browse files
authored
fix version for exe/dmg download (#2104)
1 parent 6d5e50a commit b14f697

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed

.github/workflows/docs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ jobs:
109109
run: python -m development.docs.write_openapi_spec
110110
working-directory: ./inference_repo
111111

112+
- name: Replace template variables (VERSION, etc.)
113+
run: python docs/scripts/macros.py
114+
working-directory: ./inference_repo
115+
112116
- name: Validate generated docs (Jinja2 syntax)
113117
run: python -m development.docs.validate_docs_jinja2
114118
working-directory: ./inference_repo

docs/scripts/macros.py

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,78 @@
1+
import glob
2+
import os
13
import sys
24
from pathlib import Path
35

6+
7+
def get_version():
8+
"""Read version from inference/core/version.py"""
9+
current_path = Path(__file__).resolve()
10+
for parent in current_path.parents:
11+
if (parent / 'inference').is_dir():
12+
repo_root = parent
13+
break
14+
else:
15+
raise FileNotFoundError("Could not find repository root with 'inference' directory")
16+
17+
version_file_path = repo_root.joinpath('inference', 'core', 'version.py')
18+
19+
try:
20+
namespace = {}
21+
with open(version_file_path, 'r') as f:
22+
exec(f.read(), namespace)
23+
return namespace['__version__']
24+
except Exception as e:
25+
print(f"Warning: Could not read version from {version_file_path}: {e}")
26+
return "unknown"
27+
28+
429
def define_env(env):
530
"""Hook function to define macros for MkDocs."""
6-
7-
@env.macro
8-
def get_version():
9-
"""Read version from inference/core/version.py"""
10-
# Find the root of the repository by iterating up parent directories
11-
current_path = Path(__file__).resolve()
12-
for parent in current_path.parents:
13-
# Check if this directory contains the 'inference' subdirectory
14-
if (parent / 'inference').is_dir():
15-
repo_root = parent
16-
break
17-
else:
18-
raise FileNotFoundError("Could not find repository root with 'inference' directory")
19-
20-
version_file_path = repo_root.joinpath('inference', 'core', 'version.py')
21-
22-
try:
23-
# Execute the version.py file and extract __version__
24-
namespace = {}
25-
with open(version_file_path, 'r') as f:
26-
exec(f.read(), namespace)
27-
return namespace['__version__']
28-
except Exception as e:
29-
print(f"Warning: Could not read version from {version_file_path}: {e}")
30-
return "unknown"
31-
32-
# Make VERSION available globally to all templates
33-
env.variables['VERSION'] = get_version()
31+
env.macro(get_version)
32+
env.variables['VERSION'] = get_version()
33+
34+
35+
def replace_in_docs(docs_dir: str) -> int:
36+
"""Replace ``{{ VERSION }}`` in all Markdown files under *docs_dir*.
37+
38+
Zensical does not run MkDocs plugins, so this can be called as a
39+
pre-build step to perform the substitution that mkdocs-macros would
40+
normally handle.
41+
42+
Returns the number of files modified.
43+
"""
44+
version = get_version()
45+
replacements = {
46+
"{{ VERSION }}": version,
47+
}
48+
49+
modified = 0
50+
for md_file in sorted(
51+
glob.glob(os.path.join(docs_dir, "**", "*.md"), recursive=True)
52+
):
53+
with open(md_file, encoding="utf-8") as fh:
54+
content = fh.read()
55+
56+
new_content = content
57+
for placeholder, value in replacements.items():
58+
new_content = new_content.replace(placeholder, value)
59+
60+
if new_content != content:
61+
with open(md_file, "w", encoding="utf-8") as fh:
62+
fh.write(new_content)
63+
modified += 1
64+
print(f" replaced variables in {os.path.relpath(md_file, docs_dir)}")
65+
66+
return modified
67+
68+
69+
if __name__ == "__main__":
70+
docs_dir = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
71+
os.path.dirname(__file__), "..",
72+
)
73+
docs_dir = os.path.abspath(docs_dir)
74+
75+
version = get_version()
76+
print(f"Replacing template variables in {docs_dir} (VERSION={version}) ...")
77+
count = replace_in_docs(docs_dir)
78+
print(f"Done - {count} file(s) updated.")

0 commit comments

Comments
 (0)