Skip to content

Commit 00293c5

Browse files
committed
Moving to static version and pre commit hook to fix version publishing issue
1 parent dafe836 commit 00293c5

File tree

5 files changed

+56
-39
lines changed

5 files changed

+56
-39
lines changed

.github/workflows/pr-preview.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install dependencies
2222
run: |
2323
python -m pip install --upgrade pip
24-
pip install hatchling==1.27.0 hatch==1.14.0 hatch-vcs==0.4.0
24+
pip install hatchling==1.27.0 hatch==1.14.0
2525
2626
- name: Inject full dynamic version
2727
run: python .hooks/sync_version.py --dev

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install dependencies
2222
run: |
2323
python -m pip install --upgrade pip
24-
pip install hatchling==1.27.0 hatch==1.14.0 hatch-vcs==0.4.0
24+
pip install hatchling==1.27.0 hatch==1.14.0
2525
2626
- name: Get Version
2727
id: version

.hooks/sync_version.py

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,64 @@
44
import re
55
import sys
66

7-
PACKAGE_FILE = pathlib.Path("socketsecurity/__init__.py")
7+
INIT_FILE = pathlib.Path("socketsecurity/__init__.py")
8+
PYPROJECT_FILE = pathlib.Path("pyproject.toml")
9+
810
VERSION_PATTERN = re.compile(r"__version__\s*=\s*['\"]([^'\"]+)['\"]")
11+
PYPROJECT_PATTERN = re.compile(r'^version\s*=\s*".*"$', re.MULTILINE)
912

10-
def get_hatch_version(full=False, strip_local=False):
11-
version = subprocess.check_output(["hatch", "version"], text=True).strip()
12-
if not full or strip_local:
13-
version = version.split("+")[0] # strip local metadata
14-
return version
13+
def get_git_tag():
14+
try:
15+
tag = subprocess.check_output(["git", "describe", "--tags", "--exact-match"], stderr=subprocess.DEVNULL, text=True).strip()
16+
return tag.lstrip("v") # Remove 'v' prefix
17+
except subprocess.CalledProcessError:
18+
return None
1519

16-
def get_current_version():
17-
content = PACKAGE_FILE.read_text()
18-
match = VERSION_PATTERN.search(content)
19-
return match.group(1) if match else None
20+
def get_latest_tag():
21+
try:
22+
tag = subprocess.check_output(["git", "describe", "--tags", "--abbrev=0"], text=True).strip()
23+
return tag.lstrip("v")
24+
except subprocess.CalledProcessError:
25+
return "0.0.0"
2026

21-
def update_version(new_version):
22-
content = PACKAGE_FILE.read_text()
23-
new_content = VERSION_PATTERN.sub(f"__version__ = '{new_version}'", content)
24-
PACKAGE_FILE.write_text(new_content)
27+
def get_commit_count_since(tag):
28+
try:
29+
output = subprocess.check_output(["git", "rev-list", f"{tag}..HEAD", "--count"], text=True).strip()
30+
return int(output)
31+
except subprocess.CalledProcessError:
32+
return 0
2533

26-
def main():
27-
full_mode = "--dev" in sys.argv
28-
hatch_version = get_hatch_version(full=full_mode, strip_local=full_mode)
29-
current_version = get_current_version()
34+
def inject_version(version: str):
35+
print(f"🔁 Injecting version: {version}")
36+
37+
# Update __init__.py
38+
init_content = INIT_FILE.read_text()
39+
new_init_content = VERSION_PATTERN.sub(f"__version__ = '{version}'", init_content)
40+
INIT_FILE.write_text(new_init_content)
3041

31-
if not current_version:
32-
print(f"❌ Couldn't find __version__ in {PACKAGE_FILE}")
33-
return 1
42+
# Update pyproject.toml
43+
pyproject = PYPROJECT_FILE.read_text()
44+
if PYPROJECT_PATTERN.search(pyproject):
45+
new_pyproject = PYPROJECT_PATTERN.sub(f'version = "{version}"', pyproject)
46+
else:
47+
new_pyproject = re.sub(r"(\[project\])", rf"\1\nversion = \"{version}\"", pyproject)
48+
PYPROJECT_FILE.write_text(new_pyproject)
49+
50+
def main():
51+
mode = "--dev" if "--dev" in sys.argv else "release"
3452

35-
if hatch_version != current_version:
36-
print(f"🔁 Updating version: {current_version}{hatch_version}")
37-
update_version(hatch_version)
38-
return 0 if full_mode else 1
53+
if mode == "release":
54+
version = get_git_tag()
55+
if not version:
56+
print("❌ Error: No exact tag found for release.")
57+
sys.exit(1)
58+
else:
59+
base = get_latest_tag()
60+
commits = get_commit_count_since(f"v{base}")
61+
version = f"{base}.dev{commits}"
3962

40-
print(f"✅ Version is in sync: {hatch_version}")
41-
return 0
63+
inject_version(version)
64+
print(f"✅ Injected {mode} version: {version}")
4265

4366
if __name__ == "__main__":
44-
sys.exit(main())
67+
main()

pyproject.toml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
[build-system]
22
requires = [
3-
"hatchling",
4-
"hatch-vcs"
3+
"hatchling"
54
]
65
build-backend = "hatchling.build"
76

87
[project]
98
name = "socketsecurity"
10-
dynamic = ["version"]
9+
version = "2.0.25"
1110
requires-python = ">= 3.10"
1211
dependencies = [
1312
'requests',
@@ -47,8 +46,7 @@ dev = [
4746
"twine", # for building
4847
"pip-tools>=7.4.0", # for pip-compile
4948
"pre-commit",
50-
"hatch",
51-
"hatch-vcs"
49+
"hatch"
5250
]
5351

5452
[project.scripts]
@@ -161,8 +159,5 @@ docstring-code-format = false
161159
# enabled.
162160
docstring-code-line-length = "dynamic"
163161

164-
[tool.hatch.version]
165-
source = "vcs" # Uses the latest git tag like v2.0.15
166-
167162
[tool.hatch.build.targets.wheel]
168163
include = ["socketsecurity", "LICENSE"]

requirements-dev.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
hatchling==1.27.0
1313
hatch==1.14.0
14-
hatch-vcs==0.4.0
1514
argparse==1.4.0
1615
# via socketsecurity
1716
certifi==2024.12.14

0 commit comments

Comments
 (0)