Skip to content

Commit fa16e40

Browse files
authored
Merge pull request #3 from synacker/fix/dynamic_package_version
Set for toml dynamic version
2 parents 6fbec34 + f7f272a commit fa16e40

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "git-version-utils"
7-
version = "0.0.0"
7+
dynamic = ["version"]
88
description = "Extract version information from a Git repository and output as environment variables"
99
readme = "README.md"
1010
authors = [
@@ -22,4 +22,7 @@ Repository = "https://github.com/synacker/git_version_utils"
2222
git-version = "git_version.cli:main"
2323

2424
[tool.setuptools.packages.find]
25-
where = ["src"]
25+
where = ["src"]
26+
27+
[tool.setuptools.dynamic]
28+
version = { attr = "git_version._version.__version__" }

setup.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,47 @@
44
Run `python tools/write_version.py` before building to generate
55
src/git_version/_version.py with the correct version.
66
7-
The version in pyproject.toml is a fallback placeholder (0.0.0).
7+
The version in pyproject.toml is declared as dynamic and resolved
8+
from git_version._version.__version__ at build time.
89
"""
910

1011
import os
1112
import re
13+
from pathlib import Path
1214

1315
from setuptools import setup
1416

1517

1618
def get_version() -> str:
17-
"""Get the package version from pyproject.toml."""
18-
pyproject = os.path.join(os.path.dirname(__file__), "pyproject.toml")
19-
if not os.path.exists(pyproject):
20-
return "0.0.0"
21-
with open(pyproject, "r") as f:
22-
content = f.read()
23-
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
24-
return match.group(1) if match else "0.0.0"
19+
"""Get the package version from the generated _version.py or from git.
20+
21+
Priority:
22+
1. src/git_version/_version.py (generated by tools/write_version.py)
23+
2. GIT_VERSION_UTILS_VERSION environment variable
24+
3. pyproject.toml fallback (0.0.0)
25+
"""
26+
# Try _version.py first (generated by tools/write_version.py)
27+
version_py = Path(__file__).parent / "src" / "git_version" / "_version.py"
28+
if version_py.exists():
29+
ns: dict[str, str] = {}
30+
exec(version_py.read_text(), ns)
31+
if "__version__" in ns:
32+
return ns["__version__"]
33+
34+
# Try environment variable
35+
env_version = os.environ.get("GIT_VERSION_UTILS_VERSION")
36+
if env_version:
37+
return env_version
38+
39+
# Fallback to pyproject.toml
40+
pyproject = Path(__file__).parent / "pyproject.toml"
41+
if pyproject.exists():
42+
content = pyproject.read_text()
43+
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
44+
if match:
45+
return match.group(1)
46+
47+
return "0.0.0"
2548

2649

2750
if __name__ == "__main__":

0 commit comments

Comments
 (0)