|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import shlex |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +from subprocess import check_output, run |
| 10 | + |
| 11 | +from packaging.version import parse |
| 12 | + |
| 13 | +LERNA_CMD = "npm run lerna version --no-push --force-publish --no-git-tag-version" |
| 14 | + |
| 15 | + |
| 16 | +def install_dependencies() -> None: |
| 17 | + pkgs = [] |
| 18 | + try: |
| 19 | + import hatch |
| 20 | + except ImportError: |
| 21 | + pkgs.append("hatch") |
| 22 | + |
| 23 | + if pkgs: |
| 24 | + run([sys.executable, "-m", "pip", "install"] + pkgs) |
| 25 | + |
| 26 | + |
| 27 | +def bump(force: bool, spec: str) -> None: |
| 28 | + install_dependencies() |
| 29 | + |
| 30 | + HERE = Path(__file__).parent.parent.resolve() |
| 31 | + output = check_output( |
| 32 | + shlex.split("git status --porcelain"), cwd=HERE, encoding="utf-8" |
| 33 | + ) |
| 34 | + if len(output) > 0: |
| 35 | + raise Exception("Must be in a clean git state with no untracked files") |
| 36 | + |
| 37 | + run( |
| 38 | + [sys.executable, "-m", "hatch", "version", spec], cwd=HERE, encoding="utf-8", check=True |
| 39 | + ) |
| 40 | + |
| 41 | + version = parse( |
| 42 | + # Output maybe multi-lines if build dependencies needs to be installed. |
| 43 | + check_output( |
| 44 | + [sys.executable, "-m", "hatch", "version"], cwd=HERE, encoding="utf-8" |
| 45 | + ).strip("\n").split("\n")[-1] |
| 46 | + ) |
| 47 | + |
| 48 | + # convert the Python version |
| 49 | + js_spec = "prerelease" if spec in ["alpha", "a", "beta", "b", "rc"] else spec |
| 50 | + |
| 51 | + # bump the JS packages |
| 52 | + lerna_cmd = f"{LERNA_CMD} {js_spec}" |
| 53 | + if force: |
| 54 | + lerna_cmd += " --yes" |
| 55 | + run(shlex.split(lerna_cmd), cwd=HERE, check=True) |
| 56 | + |
| 57 | + path = HERE.joinpath("package.json") |
| 58 | + if path.exists(): |
| 59 | + with path.open(mode="r") as f: |
| 60 | + data = json.load(f) |
| 61 | + |
| 62 | + data["version"] = js_version |
| 63 | + |
| 64 | + with path.open(mode="w") as f: |
| 65 | + json.dump(data, f, indent=2) |
| 66 | + |
| 67 | + else: |
| 68 | + raise FileNotFoundError(f"Could not find package.json under dir {path!s}") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + parser = argparse.ArgumentParser("bump_version", "Bump package version") |
| 73 | + parser.add_argument("--force", action="store_true") |
| 74 | + parser.add_argument("spec") |
| 75 | + |
| 76 | + args = parser.parse_args() |
| 77 | + bump(args.force, args.spec) |
0 commit comments