From cf908184c7d1574965157e6dbaa09c3376f1b5ab Mon Sep 17 00:00:00 2001 From: insleker Date: Sat, 11 Apr 2026 23:13:54 +0800 Subject: [PATCH] chore: add python installer script `install.py` --- .github/workflows/unittest.yaml | 6 ++++++ README.md | 2 ++ scripts/install.py | 24 ++++++++++++++++++------ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.github/workflows/unittest.yaml b/.github/workflows/unittest.yaml index 0a54efe..f7396e2 100644 --- a/.github/workflows/unittest.yaml +++ b/.github/workflows/unittest.yaml @@ -4,7 +4,13 @@ on: push: branches: - main + paths: + - "lib/**" + - "bin/**" pull_request: + paths: + - "lib/**" + - "bin/**" jobs: dart: diff --git a/README.md b/README.md index 15ebd21..d0b5ffe 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,9 @@ dart run bin/code_work_spawner.dart \ Install the latest matching release binary from GitHub Releases: ```bash +# export GH_TOKEN="$TOKEN" # If got 404, `GITHUB_TOKEN` also work uv run https://github.com/existedinnettw/code_work_spawner/scripts/install.py +uv run https://$GH_TOKEN@raw.githubusercontent.com/existedinnettw/code_work_spawner/main/scripts/install.py --help ``` Install a specific version: diff --git a/scripts/install.py b/scripts/install.py index fe392d6..96d53c7 100755 --- a/scripts/install.py +++ b/scripts/install.py @@ -22,6 +22,17 @@ REPO = "existedinnettw/code_work_spawner" BINARY_NAME = "code_work_spawner" +def github_headers() -> dict[str, str]: + headers = { + "Accept": "application/vnd.github+json", + "User-Agent": "code_work_spawner-install-script", + } + token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") + if token: + headers["Authorization"] = f"Bearer {token}" + return headers + + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser( description="Install code_work_spawner from GitHub Releases." @@ -105,18 +116,19 @@ def download_release_metadata(repo: str, version: str) -> dict: try: response = httpx.get( url, - headers={ - "Accept": "application/vnd.github+json", - "User-Agent": "code_work_spawner-install-script", - }, + headers=github_headers(), timeout=30.0, follow_redirects=True, ) response.raise_for_status() return response.json() except httpx.HTTPStatusError as exc: + hint = "" + if exc.response.status_code == 404: + hint = " If this release exists, set GH_TOKEN (or GITHUB_TOKEN) and retry." raise RuntimeError( - f"Failed to fetch release metadata ({exc.response.status_code}): {exc.response.text}" + "Failed to fetch release metadata " + f"({exc.response.status_code}): {exc.response.text}{hint}" ) from exc except httpx.HTTPError as exc: raise RuntimeError(f"Failed to fetch release metadata: {exc}") from exc @@ -147,7 +159,7 @@ def download_asset(asset: dict, destination: Path) -> None: with httpx.stream( "GET", download_url, - headers={"User-Agent": "code_work_spawner-install-script"}, + headers=github_headers(), timeout=60.0, follow_redirects=True, ) as response: