diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e06c17..7e979b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ # Tests on every pull request and push to main; the Linux, Windows and macOS builds # and a GitHub Release with the AppImage, the Windows .exe, the macOS .dmg and a source archive for every tag -# v (matching "version" in src-tauri/tauri.conf.json). Running the +# v (the version comes from the tag: tools/set_version.py writes it in). Running the # workflow by hand (workflow_dispatch) builds all three without releasing: do that # before tagging, since merges no longer build. # @@ -123,14 +123,12 @@ jobs: - name: Install Tauri CLI run: cargo install tauri-cli --version "^2" --locked - - name: Check the tag matches the app version + # A release's version is its tag's (v0.2.0 → 0.2.0), written into + # tauri.conf.json, Cargo.toml and Cargo.lock before building: nothing to + # bump by hand. A tag that isn't v.. fails here. + - name: Set the version from the tag if: startsWith(github.ref, 'refs/tags/v') - run: | - version=$(python3 -c "import json; print(json.load(open('src-tauri/tauri.conf.json'))['version'])") - if [ "v$version" != "$GITHUB_REF_NAME" ]; then - echo "::error::tag $GITHUB_REF_NAME doesn't match version $version in src-tauri/tauri.conf.json" - exit 1 - fi + run: python3 tools/set_version.py "$GITHUB_REF_NAME" - name: Build the AppImage env: @@ -177,6 +175,11 @@ jobs: - name: Install Tauri CLI run: cargo install tauri-cli --version "^2" --locked + - name: Set the version from the tag + if: startsWith(github.ref, 'refs/tags/v') + shell: bash + run: python tools/set_version.py "$GITHUB_REF_NAME" + - name: Build the app env: KIVI_GITHUB_CLIENT_ID: ${{ vars.KIVI_GITHUB_CLIENT_ID }} @@ -229,6 +232,10 @@ jobs: - name: Install Tauri CLI run: cargo install tauri-cli --version "^2" --locked + - name: Set the version from the tag + if: startsWith(github.ref, 'refs/tags/v') + run: python3 tools/set_version.py "$GITHUB_REF_NAME" + - name: Build the .dmg env: KIVI_GITHUB_CLIENT_ID: ${{ vars.KIVI_GITHUB_CLIENT_ID }} @@ -263,12 +270,17 @@ jobs: - uses: actions/checkout@v4 with: path: src + # With the tag's version written in, like the binaries: `git stash create` + # makes a commit of the tagged tree plus that change, without touching + # any branch, and the archive is made from it. - name: Source archive run: | version="${GITHUB_REF_NAME#v}" + python3 src/tools/set_version.py "$GITHUB_REF_NAME" + tree=$(git -C src -c user.name=KiVi -c user.email=ci@localhost stash create) mkdir -p bundles/source git -C src archive --format=tar.gz --prefix="KiVi-${version}/" \ - -o "$PWD/bundles/source/KiVi_${version}_source.tar.gz" HEAD + -o "$PWD/bundles/source/KiVi_${version}_source.tar.gz" "${tree:-HEAD}" - uses: softprops/action-gh-release@v2 with: diff --git a/CLAUDE.md b/CLAUDE.md index b994f84..7067bde 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -557,7 +557,11 @@ tests/smoke.sh # pages load cleanly, viewer opens with a hint if they're missing. This machine's connection is slow (~0.2 MB/s): a full fetch takes ~6 min here. - CI is `.github/workflows/ci.yml` (see README): tests and Rust tests (with the notices check) on PRs and main → Linux, Windows and macOS builds (artifacts; tags and manual runs only, the user's choice to save Actions minutes on the private repo) - → GitHub Release on `v*` tags. Keep its commands in sync with this list; + → GitHub Release on `v*` tags. The release version is the tag's (the user's + choice): every build job and the source archive run `tools/set_version.py + ` first (tauri.conf.json, Cargo.toml [package], the kivi-desktop entry + of Cargo.lock); the files in the repository aren't bumped and only matter for + local and manual builds. Keep its commands in sync with this list; it was written without a way to run it locally (no act/actionlint here), so the first run on GitHub is its real test. - `kicad-cli` (KiCad's own SVG export, for `tools/compare.py`) is the one diff --git a/README.md b/README.md index c11b816..60e7f95 100644 --- a/README.md +++ b/README.md @@ -454,11 +454,16 @@ ok diff `KiVi__universal.dmg` (Apple Silicon and Intel; `LICENSE` and `THIRD-PARTY-NOTICES.md` are inside the app). Not code-signed or notarised. -Pushing a tag `v` (matching `version` in `src-tauri/tauri.conf.json`, -checked) runs all five and publishes a GitHub Release with the AppImage, the +Pushing a tag `v` (for example `v0.2.0`) runs all five and publishes a +GitHub Release. The version comes from the tag: each build first runs +`tools/set_version.py`, which writes it into `src-tauri/tauri.conf.json`, +`src-tauri/Cargo.toml` and `src-tauri/Cargo.lock`, so there's nothing to bump +by hand (the versions in the repository are only for local and manual builds; +a tag that isn't `v..`, optionally with `-beta.1` and the +like, fails the build). The release has the AppImage, the Windows `.exe`, the macOS `.dmg`, `LICENSE`, `THIRD-PARTY-NOTICES.md`, `appimage-libraries.md` and `KiVi__source.tar.gz` (`git archive` of -the tag). Merges don't build, so run the workflow by hand on `main` before +the tag, with the version written in). Merges don't build, so run the workflow by hand on `main` before tagging and check all three builds pass. ## Licence diff --git a/tools/set_version.py b/tools/set_version.py new file mode 100755 index 0000000..2a4b9f7 --- /dev/null +++ b/tools/set_version.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +"""Set KiVi's version everywhere it's written down. + + python3 tools/set_version.py v0.2.0 # or 0.2.0 + +A release takes its version from the tag: CI runs this with the pushed tag +(`v0.2.0`) before building, so the AppImage, the .exe, the .dmg and the source +archive all carry it, and nobody has to bump the files by hand first. The +files in the repository keep whatever version they have; it's only used for +local and manual builds. + +It changes, keeping everything else as it is: + src-tauri/tauri.conf.json "version" (the app, bundle and file names) + src-tauri/Cargo.toml [package] version + src-tauri/Cargo.lock the kivi-desktop entry (so a --locked build agrees) +""" +import re +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +# Semantic versions, as Tauri and Cargo accept them: 1.2.3, or 1.2.3-beta.1. +VERSION = re.compile(r"^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$") + +# (file, pattern whose group 2 is the version, what it is) +PLACES = [ + ("src-tauri/tauri.conf.json", re.compile(r'^(\s*"version"\s*:\s*")([^"]*)(")', re.M), "tauri.conf.json version"), + ("src-tauri/Cargo.toml", re.compile(r'(\[package\][^\[]*?^version\s*=\s*")([^"]*)(")', re.M | re.S), "Cargo.toml [package] version"), + ("src-tauri/Cargo.lock", re.compile(r'(^name = "kivi-desktop"\nversion = ")([^"]*)(")', re.M), "Cargo.lock kivi-desktop entry"), +] + + +def set_version(version: str, root: Path = ROOT) -> list[str]: + """Write `version` into every place; returns what changed. Raises ValueError for a bad version or a place not found.""" + version = version.strip().removeprefix("v") + if not VERSION.match(version): + raise ValueError(f'"{version}" isn\'t a version like 1.2.3 (tags are v1.2.3)') + changed = [] + for rel, pattern, what in PLACES: + path = root / rel + text = path.read_text(encoding="utf-8") + m = pattern.search(text) + if not m: + raise ValueError(f"no {what} found in {rel}") + if m.group(2) != version: + changed.append(f"{what}: {m.group(2)} → {version}") + text = text[: m.start(2)] + version + text[m.end(2) :] + path.write_text(text, encoding="utf-8") + return changed + + +def main() -> int: + if len(sys.argv) != 2: + print(__doc__.strip().splitlines()[2].strip(), file=sys.stderr) + return 2 + try: + changed = set_version(sys.argv[1]) + except ValueError as e: + print(f"set_version: {e}", file=sys.stderr) + return 1 + print("\n".join(changed) if changed else "already that version") + return 0 + + +if __name__ == "__main__": + sys.exit(main())