Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions macros.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
from __future__ import annotations

import tomllib
from pathlib import Path
from typing import Any

from setuptools_scm import get_version

def _load_version(pyproject_path: Path) -> str:
"""Return project version string from pyproject.toml."""
with pyproject_path.open("rb") as pyproject_file:
data = tomllib.load(pyproject_file)
project = data.get("project", {})
version = project.get("version")
if not isinstance(version, str):
raise ValueError("project.version must be defined in pyproject.toml")
return version

def _load_version(root: Path) -> str:
"""Return project version string using setuptools_scm."""
return get_version(root=str(root))


def define_env(env: Any) -> None:
"""Expose project version to MkDocs macros and theme configuration."""
root = Path(__file__).resolve().parent
version = _load_version(root / "pyproject.toml")
version = _load_version(root)
env.variables["project_version"] = version
extra = env.conf.get("extra") or {}
extra["version"] = version
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ docs = [
"mkdocs-material",
"mkdocstrings[python]",
"mkdocs-macros-plugin",
"setuptools_scm>=8.0",
]

[project.scripts]
Expand All @@ -103,7 +104,7 @@ typeCheckingMode = "standard"
reportAny = false
reportExplicitAny = false
reportUnusedCallResult = false
exclude = ["src/alpha_hwr/*_baseline.py"]
exclude = ["src/alpha_hwr/*_baseline.py", "macros.py"]

[tool.mypy]
ignore_missing_imports = true
Expand Down