Skip to content

Commit 3185a23

Browse files
authored
ops: convert update_version to Python script as it's runnable on win (#1339)
1 parent f8e4835 commit 3185a23

File tree

3 files changed

+90
-27
lines changed

3 files changed

+90
-27
lines changed

.github/scripts/update_version.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Update project versions from a GitHub tag reference.
4+
5+
Behavior mirrors the original Bash script:
6+
- Reads GITHUB_REF and looks for refs/tags/v<version>
7+
- If not found, prints a message and exits 0 (no-op)
8+
- Updates the root Cargo.toml version
9+
- Writes python/cocoindex/_version.py with __version__
10+
11+
Assumes current working directory is the repository root.
12+
Works on macOS, Linux, and Windows.
13+
"""
14+
15+
from __future__ import annotations
16+
17+
import os
18+
import re
19+
import sys
20+
from collections.abc import Mapping
21+
from pathlib import Path
22+
23+
24+
TAG_PATTERN = re.compile(r"^refs/tags/v(?P<version>.+)$")
25+
VERSION_LINE_PATTERN = re.compile(r'(?m)^(?P<prefix>\s*version\s*=\s*)"[^"]*"')
26+
27+
28+
def extract_version_from_github_ref(env: Mapping[str, str]) -> str | None:
29+
ref = env.get("GITHUB_REF", "")
30+
match = TAG_PATTERN.match(ref)
31+
if not match:
32+
return None
33+
return match.group("version")
34+
35+
36+
def update_cargo_version(cargo_toml_path: Path, version: str) -> bool:
37+
original = cargo_toml_path.read_text(encoding="utf-8")
38+
updated, count = VERSION_LINE_PATTERN.subn(
39+
rf'\g<prefix>"{version}"', original, count=1
40+
)
41+
if count == 0:
42+
print(f"Version line not found in Cargo.toml", file=sys.stderr)
43+
return False
44+
cargo_toml_path.write_text(updated, encoding="utf-8", newline="\n")
45+
return True
46+
47+
48+
def write_python_version(version_file_path: Path, version: str) -> None:
49+
version_file_path.parent.mkdir(parents=True, exist_ok=True)
50+
content = f'__version__ = "{version}"\n'
51+
version_file_path.write_text(content, encoding="utf-8", newline="\n")
52+
53+
54+
def main() -> int:
55+
version = extract_version_from_github_ref(os.environ)
56+
if not version:
57+
print("No version tag found")
58+
return 0
59+
60+
print(f"Building release version: {version}")
61+
62+
cargo_toml = Path("Cargo.toml")
63+
if not cargo_toml.exists():
64+
print(f"Cargo.toml not found at: {cargo_toml}", file=sys.stderr)
65+
return 1
66+
67+
if not update_cargo_version(cargo_toml, version):
68+
return 1
69+
70+
py_version_file = Path("python") / "cocoindex" / "_version.py"
71+
write_python_version(py_version_file, version)
72+
73+
return 0
74+
75+
76+
if __name__ == "__main__":
77+
raise SystemExit(main())

.github/scripts/update_version.sh

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ jobs:
2121
- uses: actions/checkout@v4
2222
with:
2323
fetch-depth: 1
24-
- run: ./.github/scripts/update_version.sh
24+
- uses: actions/setup-python@v5
25+
with:
26+
python-version: 3.13
27+
- run: python ./.github/scripts/update_version.py
2528
- name: Install Rust toolchain
2629
uses: dtolnay/rust-toolchain@stable
2730
- uses: taiki-e/install-action@v2
@@ -52,13 +55,13 @@ jobs:
5255
- uses: actions/checkout@v4
5356
with:
5457
fetch-depth: 1
55-
- run: ./.github/scripts/update_version.sh
5658
- uses: actions/download-artifact@v4
5759
with:
5860
name: THIRD_PARTY_NOTICES.html
5961
- uses: actions/setup-python@v5
6062
with:
6163
python-version: 3.13
64+
- run: python ./.github/scripts/update_version.py
6265
- name: Build wheels
6366
uses: PyO3/maturin-action@v1
6467
with:
@@ -98,7 +101,10 @@ jobs:
98101
- uses: actions/checkout@v4
99102
with:
100103
fetch-depth: 1
101-
- run: ./.github/scripts/update_version.sh
104+
- uses: actions/setup-python@v5
105+
with:
106+
python-version: 3.13
107+
- run: python ./.github/scripts/update_version.py
102108
- uses: actions/download-artifact@v4
103109
with:
104110
name: THIRD_PARTY_NOTICES.html
@@ -129,7 +135,10 @@ jobs:
129135
- uses: actions/checkout@v4
130136
with:
131137
fetch-depth: 1
132-
- run: ./.github/scripts/update_version.sh
138+
- uses: actions/setup-python@v5
139+
with:
140+
python-version: 3.13
141+
- run: python ./.github/scripts/update_version.py
133142
- uses: actions/download-artifact@v4
134143
with:
135144
name: THIRD_PARTY_NOTICES.html

0 commit comments

Comments
 (0)