|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.9" |
| 3 | +# dependencies = [ |
| 4 | +# "tomli>=2.2.1", |
| 5 | +# "tomli-w>=1.2.0", |
| 6 | +# "urllib3>=2", |
| 7 | +# "packaging>=21.0", |
| 8 | +# ] |
| 9 | +# /// |
| 10 | + |
| 11 | +import subprocess |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import tomli |
| 15 | +import tomli_w |
| 16 | +import urllib3 |
| 17 | +from packaging.requirements import Requirement |
| 18 | +from packaging.version import Version |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + # Load pyproject.toml |
| 23 | + with open(Path(__file__).parent / "pyproject.toml", "rb") as f: |
| 24 | + pyproject = tomli.load(f) |
| 25 | + # Load README.md |
| 26 | + with open(Path(__file__).parent / "README.md", "r") as f: |
| 27 | + readme = f.read() |
| 28 | + |
| 29 | + # 获取当前版本的 ast-grep |
| 30 | + deps = pyproject["project"]["dependencies"] |
| 31 | + assert len(deps) == 1 |
| 32 | + ast_grep_dep = Requirement(deps[0]) |
| 33 | + assert ast_grep_dep.name == "ast-grep-cli" |
| 34 | + ast_grep_specs = list(ast_grep_dep.specifier) |
| 35 | + assert len(ast_grep_specs) == 1 |
| 36 | + assert ast_grep_specs[0].operator == "==" |
| 37 | + current_version = Version(ast_grep_specs[0].version) |
| 38 | + |
| 39 | + # Get ast-grep versions from PyPI |
| 40 | + http = urllib3.PoolManager() |
| 41 | + resp = http.request("GET", "https://pypi.org/pypi/ast-grep-cli/json") |
| 42 | + if resp.status != 200: |
| 43 | + raise RuntimeError("Failed to fetch data from PyPI") |
| 44 | + |
| 45 | + versions = [Version(release) for release in resp.json()["releases"]] |
| 46 | + versions = [v for v in versions if v > current_version and not v.is_prerelease] |
| 47 | + versions.sort() |
| 48 | + |
| 49 | + # Update ast-grep for each version |
| 50 | + for version in versions: |
| 51 | + # Update pyproject.toml |
| 52 | + pyproject["project"]["version"] = str(version) |
| 53 | + pyproject["project"]["dependencies"] = [f"ast-grep-cli=={version}"] |
| 54 | + # Update README.md |
| 55 | + updated_readme = readme.replace(str(current_version), str(version)) |
| 56 | + |
| 57 | + # Write pyproject.toml and README.md |
| 58 | + with open(Path(__file__).parent / "pyproject.toml", "wb") as f: |
| 59 | + tomli_w.dump(pyproject, f) |
| 60 | + with open(Path(__file__).parent / "README.md", "w") as f: |
| 61 | + f.write(updated_readme) |
| 62 | + |
| 63 | + # Commit and tag |
| 64 | + subprocess.run(["git", "add", "pyproject.toml", "README.md"]) |
| 65 | + subprocess.run( |
| 66 | + [ |
| 67 | + "git", |
| 68 | + "commit", |
| 69 | + "-m", |
| 70 | + f":arrow_up: bump ast-grep version to {version}", |
| 71 | + ] |
| 72 | + ) |
| 73 | + subprocess.run(["git", "tag", f"v{version}"]) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments