diff --git a/pyproject.toml b/pyproject.toml index f7769d7..d272744 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "git-version-utils" -version = "0.0.0" +dynamic = ["version"] description = "Extract version information from a Git repository and output as environment variables" readme = "README.md" authors = [ @@ -22,4 +22,7 @@ Repository = "https://github.com/synacker/git_version_utils" git-version = "git_version.cli:main" [tool.setuptools.packages.find] -where = ["src"] \ No newline at end of file +where = ["src"] + +[tool.setuptools.dynamic] +version = { attr = "git_version._version.__version__" } \ No newline at end of file diff --git a/setup.py b/setup.py index 2d15470..0f6fac3 100644 --- a/setup.py +++ b/setup.py @@ -4,24 +4,47 @@ Run `python tools/write_version.py` before building to generate src/git_version/_version.py with the correct version. -The version in pyproject.toml is a fallback placeholder (0.0.0). +The version in pyproject.toml is declared as dynamic and resolved +from git_version._version.__version__ at build time. """ import os import re +from pathlib import Path from setuptools import setup def get_version() -> str: - """Get the package version from pyproject.toml.""" - pyproject = os.path.join(os.path.dirname(__file__), "pyproject.toml") - if not os.path.exists(pyproject): - return "0.0.0" - with open(pyproject, "r") as f: - content = f.read() - match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE) - return match.group(1) if match else "0.0.0" + """Get the package version from the generated _version.py or from git. + + Priority: + 1. src/git_version/_version.py (generated by tools/write_version.py) + 2. GIT_VERSION_UTILS_VERSION environment variable + 3. pyproject.toml fallback (0.0.0) + """ + # Try _version.py first (generated by tools/write_version.py) + version_py = Path(__file__).parent / "src" / "git_version" / "_version.py" + if version_py.exists(): + ns: dict[str, str] = {} + exec(version_py.read_text(), ns) + if "__version__" in ns: + return ns["__version__"] + + # Try environment variable + env_version = os.environ.get("GIT_VERSION_UTILS_VERSION") + if env_version: + return env_version + + # Fallback to pyproject.toml + pyproject = Path(__file__).parent / "pyproject.toml" + if pyproject.exists(): + content = pyproject.read_text() + match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE) + if match: + return match.group(1) + + return "0.0.0" if __name__ == "__main__":