diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 374e882..2ab674e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,6 +19,9 @@ jobs: macos: if: github.event_name == 'push' || inputs.platforms == 'all' || contains(inputs.platforms, 'macos') runs-on: macos-latest + env: + TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }} + TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }} steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v5 @@ -31,8 +34,19 @@ jobs: - run: npm ci - name: Test run: cargo test -p knock-core --locked - - name: App (universal .app and .dmg) - run: npx tauri build --target universal-apple-darwin --bundles app,dmg + - name: App (universal .app and .dmg, plus a signed update package) + run: | + # Update packages only when the signing key is available. + extra="" + if [ -n "$TAURI_SIGNING_PRIVATE_KEY" ]; then extra='--config {"bundle":{"createUpdaterArtifacts":true}}'; fi + npx tauri build --target universal-apple-darwin --bundles app,dmg $extra + v=$(node -p "require('./src-tauri/tauri.conf.json').version") + mkdir -p dist-update + b=target/universal-apple-darwin/release/bundle/macos + if [ -f "$b/knock.app.tar.gz" ]; then + cp "$b/knock.app.tar.gz" "dist-update/knock_${v}_universal.app.tar.gz" + cp "$b/knock.app.tar.gz.sig" "dist-update/knock_${v}_universal.app.tar.gz.sig" + fi - name: CLI (universal binary) run: | cargo build --release --locked -p knock-cli --target aarch64-apple-darwin @@ -53,12 +67,16 @@ jobs: path: | target/universal-apple-darwin/release/bundle/dmg/*.dmg knock-cli-macos-universal.tar.gz + dist-update/* # Best effort per the spec. windows: if: github.event_name == 'push' || inputs.platforms == 'all' || contains(inputs.platforms, 'windows') runs-on: windows-latest continue-on-error: true + env: + TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }} + TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }} steps: - uses: actions/checkout@v5 - uses: actions/setup-node@v5 @@ -67,8 +85,14 @@ jobs: cache: npm - uses: Swatinem/rust-cache@v2 - run: npm ci - - name: App (.msi and setup .exe) - run: npx tauri build --bundles msi,nsis + - name: App (.msi and setup .exe, signed for updates) + shell: bash + run: | + if [ -n "$TAURI_SIGNING_PRIVATE_KEY" ]; then + npx tauri build --bundles msi,nsis --config '{"bundle":{"createUpdaterArtifacts":true}}' + else + npx tauri build --bundles msi,nsis + fi - name: CLI shell: pwsh run: | @@ -80,6 +104,7 @@ jobs: path: | target/release/bundle/msi/*.msi target/release/bundle/nsis/*.exe + target/release/bundle/nsis/*.exe.sig knock-cli-windows-x64.zip # Native amd64 Linux build on Ubuntu 22.04, with the AppImage the arm64 @@ -170,6 +195,32 @@ jobs: with: path: artifacts merge-multiple: true + - name: Update manifest for macOS and Windows + env: + TAG: ${{ github.ref_name }} + REPO: ${{ github.repository }} + run: | + cd artifacts + v="${TAG#v}" + base="https://github.com/$REPO/releases/download/$TAG" + mac=$(ls *_universal.app.tar.gz 2>/dev/null | head -1 || true) + win=$(ls *-setup.exe 2>/dev/null | head -1 || true) + if [ -z "$mac$win" ]; then echo "no signed update packages, skipping latest.json"; exit 0; fi + python3 - "$v" "$base" "$mac" "$win" <<'PY' + import json, sys, datetime, os + v, base, mac, win = sys.argv[1:5] + platforms = {} + def sig(f): + return open(f + ".sig").read().strip() if f and os.path.exists(f + ".sig") else None + if mac and sig(mac): + for p in ("darwin-aarch64", "darwin-x86_64"): + platforms[p] = {"signature": sig(mac), "url": f"{base}/{mac}"} + if win and sig(win): + platforms["windows-x86_64"] = {"signature": sig(win), "url": f"{base}/{win}"} + json.dump({"version": v, "notes": f"knock {v}", "pub_date": datetime.datetime.now(datetime.timezone.utc).isoformat(), "platforms": platforms}, open("latest.json", "w"), indent=2) + print(open("latest.json").read()) + PY + - name: Checksums run: | cd artifacts diff --git a/Cargo.lock b/Cargo.lock index ea72b9e..ffad6ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -894,6 +894,15 @@ dependencies = [ "tiny-keccak", ] +[[package]] +name = "constant_time_eq" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "144e7f1de087984240b7816d47f698b414979faa6f40839c5b86d6bd47db57b7" +dependencies = [ + "rustversion", +] + [[package]] name = "cookie" version = "0.18.3" @@ -1593,6 +1602,16 @@ dependencies = [ "rustc_version", ] +[[package]] +name = "filetime" +version = "0.2.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1255e7a9f1902190d4c10a75f2f7f495d3d3ce13e9ba26e6c48683a0f4dbed7" +dependencies = [ + "cfg-if", + "libc", +] + [[package]] name = "find-msvc-tools" version = "0.1.16" @@ -2891,7 +2910,9 @@ dependencies = [ "tauri-plugin-dialog", "tauri-plugin-notification", "tauri-plugin-opener", + "tauri-plugin-process", "tauri-plugin-single-instance", + "tauri-plugin-updater", "tokio", "tokio-tungstenite", "tracing", @@ -3184,6 +3205,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "minisign-verify" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871285dc19d8d0ebe0eef3d0e99a205f2a71363b122632cbbfa4a6c370a960ce" + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -3292,6 +3319,18 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nix" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77730ed34f01f4990cb2ea67fec715ecebc6fab9a6396cf83c5529b13f581bdc" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -3611,6 +3650,18 @@ dependencies = [ "objc2-core-foundation", ] +[[package]] +name = "objc2-osa-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f112d1746737b0da274ef79a23aac283376f335f4095a083a267a082f21db0c0" +dependencies = [ + "bitflags 2.13.2", + "objc2", + "objc2-app-kit", + "objc2-foundation", +] + [[package]] name = "objc2-quartz-core" version = "0.3.2" @@ -3732,6 +3783,20 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "osakit" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "732c71caeaa72c065bb69d7ea08717bd3f4863a4f451402fc9513e29dbd5261b" +dependencies = [ + "objc2", + "objc2-foundation", + "objc2-osa-kit", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "owo-colors" version = "4.4.0" @@ -4629,6 +4694,7 @@ checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ "aws-lc-rs", "once_cell", + "ring", "rustls-pki-types", "rustls-webpki", "subtle", @@ -5611,6 +5677,17 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tar" +version = "0.4.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "target-lexicon" version = "0.12.16" @@ -5853,6 +5930,16 @@ dependencies = [ "zbus 5.19.0", ] +[[package]] +name = "tauri-plugin-process" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74ae3d8549ae564c0397377373e262f340908eb23a5b9fbe03f2a00007dc0038" +dependencies = [ + "tauri", + "tauri-plugin", +] + [[package]] name = "tauri-plugin-single-instance" version = "2.5.5" @@ -5870,6 +5957,40 @@ dependencies = [ "zbus 5.19.0", ] +[[package]] +name = "tauri-plugin-updater" +version = "2.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d957abbcb3f9f6c76d22a3a0f012f30880640d0bccb5634d2b12a9937e4c872b" +dependencies = [ + "base64 0.23.1", + "dirs", + "flate2", + "futures-util", + "http", + "infer", + "libc", + "log", + "minisign-verify", + "osakit", + "percent-encoding", + "reqwest", + "rustls", + "semver", + "serde", + "serde_json", + "tar", + "tauri", + "tauri-plugin", + "tempfile", + "thiserror 2.0.21", + "time", + "tokio", + "url", + "windows-sys 0.61.2", + "zip", +] + [[package]] name = "tauri-runtime" version = "2.12.4" @@ -6388,6 +6509,12 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "typed-path" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce898823d8cd0f028662d2f349694b1af0a5cc9be83f1d213a00d3147445e1e" + [[package]] name = "typeid" version = "1.0.3" @@ -7326,6 +7453,16 @@ dependencies = [ "time", ] +[[package]] +name = "xattr" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "963d6fab92d5504b971dc8bd90ca743c2575e590bc8071ea5bc571598667ae08" +dependencies = [ + "libc", + "rustix", +] + [[package]] name = "xdg-home" version = "1.3.0" @@ -7375,7 +7512,7 @@ dependencies = [ "futures-sink", "futures-util", "hex", - "nix", + "nix 0.29.0", "ordered-stream", "rand 0.8.8", "serde", @@ -7631,6 +7768,24 @@ dependencies = [ "syn 3.0.10", ] +[[package]] +name = "zip" +version = "9.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d8dbe089cf6b0448b78f5fc50139ab5aaa40d08ac07cdc1baf2c13add35fcce" +dependencies = [ + "constant_time_eq", + "crc32fast", + "getrandom 0.4.3", + "indexmap 2.14.2", + "libc", + "memchr", + "nix 0.32.0", + "typed-path", + "windows-sys 0.61.2", + "zeroize", +] + [[package]] name = "zlib-rs" version = "0.6.9" diff --git a/package-lock.json b/package-lock.json index 3b5d982..d900443 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,8 @@ "@tauri-apps/plugin-dialog": "^2.8.4", "@tauri-apps/plugin-notification": "^2.8.1", "@tauri-apps/plugin-opener": "^2.7.3", + "@tauri-apps/plugin-process": "^2.4.0", + "@tauri-apps/plugin-updater": "^2.14.1", "cm6-graphql": "^1.0.0", "graphql": "^17.1.0", "jsonpath-plus": "^11.3.0" @@ -1364,6 +1366,24 @@ "@tauri-apps/api": "^2.12.0" } }, + "node_modules/@tauri-apps/plugin-process": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@tauri-apps/plugin-process/-/plugin-process-2.4.0.tgz", + "integrity": "sha512-GddrmsSLAYrLa/yTXlXK/YRDzUgYeeOnUXTbiyvJWkmI424RmDPD3FVLeqcon7rv6O6ZfhBilcrHWnSo7o6hIQ==", + "license": "MIT OR Apache-2.0", + "dependencies": { + "@tauri-apps/api": "^2.12.0" + } + }, + "node_modules/@tauri-apps/plugin-updater": { + "version": "2.14.1", + "resolved": "https://registry.npmjs.org/@tauri-apps/plugin-updater/-/plugin-updater-2.14.1.tgz", + "integrity": "sha512-Hr7nlBm1wT6xbwzjZ/S36YULazu8MNlj5vvOwMl47fXI7HTa5Bjl1PbDEwVHaZTAKzE1VZsUy35UqnbrQxmYQw==", + "license": "MIT OR Apache-2.0", + "dependencies": { + "@tauri-apps/api": "^2.12.0" + } + }, "node_modules/@tsconfig/svelte": { "version": "5.0.9", "resolved": "https://registry.npmjs.org/@tsconfig/svelte/-/svelte-5.0.9.tgz", diff --git a/package.json b/package.json index 565ea52..b2900d1 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,8 @@ "@tauri-apps/plugin-dialog": "^2.8.4", "@tauri-apps/plugin-notification": "^2.8.1", "@tauri-apps/plugin-opener": "^2.7.3", + "@tauri-apps/plugin-process": "^2.4.0", + "@tauri-apps/plugin-updater": "^2.14.1", "cm6-graphql": "^1.0.0", "graphql": "^17.1.0", "jsonpath-plus": "^11.3.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 26c97ad..b1eab1b 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -30,6 +30,8 @@ tauri = { version = "2", features = [] } tauri-plugin-dialog = "2" tauri-plugin-opener = "2" tauri-plugin-notification = "2" +tauri-plugin-updater = "2" +tauri-plugin-process = "2" tauri-plugin-single-instance = { version = "2", features = ["deep-link"] } tauri-plugin-deep-link = "2" rusqlite = { version = "0.37", features = ["bundled"] } diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index bbbf787..e215612 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -19,6 +19,8 @@ "deep-link:default", "core:webview:allow-create-webview-window", "core:window:allow-set-title", - "notification:default" + "notification:default", + "updater:default", + "process:allow-restart" ] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f95db52..e64ae3f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -20,6 +20,8 @@ pub fn run() { .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_notification::init()) + .plugin(tauri_plugin_updater::Builder::new().build()) + .plugin(tauri_plugin_process::init()) .setup(|app| { let dir = app.path().app_data_dir()?; app.manage(state::AppState::new(dir)?); diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index c264fb2..1144e42 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -35,6 +35,15 @@ "knock" ] } + }, + "updater": { + "pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDM2QjY4RUU5MzQwNzQzNTIKUldSU1F3YzA2WTYyTnNPZU0zR0xQUjl4dmUvQlpZazJkVXhjUE1JbDhNNTJRUUdzQjI4UTVCdlMK", + "endpoints": [ + "https://github.com/JoshiCodes/knock/releases/latest/download/latest.json" + ], + "windows": { + "installMode": "passive" + } } }, "bundle": { diff --git a/src/lib/state/updates.svelte.ts b/src/lib/state/updates.svelte.ts index 2ec929c..b781290 100644 --- a/src/lib/state/updates.svelte.ts +++ b/src/lib/state/updates.svelte.ts @@ -1,6 +1,11 @@ import { invoke } from '@tauri-apps/api/core'; import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/plugin-notification'; import { errorText } from '$lib/api'; +import { check as checkInstallable, type Update } from '@tauri-apps/plugin-updater'; +import { relaunch } from '@tauri-apps/plugin-process'; + +/** macOS and Windows update themselves; Linux updates come from the package manager. */ +export const canSelfUpdate = /Mac|Win/.test(navigator.userAgent); export interface Release { version: string; @@ -38,6 +43,10 @@ class Updates { checking = $state(false); lastError = $state(null); askNow = $state(false); + /** Signed update package ready to install (macOS, Windows). */ + installable = $state(null); + installing = $state(false); + progress = $state(null); start() { if (this.mode === null) { @@ -64,6 +73,7 @@ class Updates { const skipped = get('knock.updates.skip'); if (r.newer && (manual || skipped !== r.version)) { this.available = r; + if (canSelfUpdate) await this.findInstallable(); if (!manual) await this.notify(r); } return r; @@ -75,6 +85,39 @@ class Updates { } } + private async findInstallable() { + try { + this.installable = await checkInstallable(); + } catch { + // No signed package for this platform (yet); the download link still works. + this.installable = null; + } + } + + /** Downloads, verifies the signature, installs and restarts. */ + async install() { + const u = this.installable; + if (!u) return; + this.installing = true; + this.progress = 0; + let total = 0; + let done = 0; + try { + await u.downloadAndInstall((ev) => { + if (ev.event === 'Started') total = ev.data.contentLength ?? 0; + else if (ev.event === 'Progress') { + done += ev.data.chunkLength; + this.progress = total ? Math.round((done / total) * 100) : null; + } + }); + await relaunch(); + } catch (e) { + this.lastError = `Update failed: ${errorText(e)}`; + this.installing = false; + this.progress = null; + } + } + skip() { if (this.available) set('knock.updates.skip', this.available.version); this.available = null; diff --git a/src/lib/views/UpdateNotice.svelte b/src/lib/views/UpdateNotice.svelte index 0098836..9acebc7 100644 --- a/src/lib/views/UpdateNotice.svelte +++ b/src/lib/views/UpdateNotice.svelte @@ -6,11 +6,23 @@ {#if updates.available} {@const r = updates.available} + {#if updates.lastError && updates.installing === false && updates.installable} +
{updates.lastError}
+ {/if}
knock {r.version} is available. - Your version keeps working; updates come through your package manager or the download page. - - + {#if updates.installable} + {updates.installing ? `Downloading${updates.progress !== null ? ` ${updates.progress} %` : '…'} knock restarts when it is done.` : 'Signed update, ready to install.'} + + {#if !updates.installing} + + + {/if} + {:else} + Your version keeps working; updates come through your package manager or the download page. + + + {/if}
diff --git a/src/lib/views/settings/AppSettings.svelte b/src/lib/views/settings/AppSettings.svelte index 4587b1f..6cb6613 100644 --- a/src/lib/views/settings/AppSettings.svelte +++ b/src/lib/views/settings/AppSettings.svelte @@ -87,7 +87,7 @@
Tell me about new versions
-
Once a day knock asks GitHub for the latest release. Nothing else is sent. knock never updates itself; use your package manager or the download page.
+
Once a day knock asks GitHub for the latest release. Nothing else is sent. On macOS and Windows knock can install signed updates when you confirm; on Linux, update through your package manager.
updates.choose(v)} aria-label="Check for updates" />