C2: lutra CLI in the setup with optional PATH entry, xtask bundle prints the version
Some checks failed
ci / ci (macos-15) (push) Has been cancelled
ci / ci (ubuntu-24.04) (push) Has been cancelled
ci / ci (windows-2025) (push) Has been cancelled

- xtask bundle builds lutra-cli and adds it as a Tauri external binary
  through a --config override (lutra.exe next to the app on Windows,
  Lutra.app/Contents/MacOS/lutra on macOS); prints path, version, size.
- NSIS: "Add lutra to PATH" page, unticked by default, /ADDTOPATH for
  silent installs. Changes HKCU\Environment Path only, keeps its type,
  broadcasts WM_SETTINGCHANGE, records the entry; the uninstaller removes
  exactly that entry. A PATH too long for NSIS strings is left alone.
- lutra CLI resolves a symlinked executable before looking for the app.
- localStorage: the UI never used webview storage, so there is nothing to
  migrate; docs corrected, ESLint now bans localStorage, sessionStorage
  and indexedDB, with tests.
- docs/install.md: CLI, PATH, macOS link step, webview folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-01 12:46:49 +01:00
parent d8e66a4652
commit 0a4b80adec
7 changed files with 628 additions and 46 deletions

View file

@ -1,37 +1,59 @@
//! `cargo xtask bundle`: the platform's installer.
//!
//! Builds the desktop app in release mode with the bundled UI through the
//! Tauri CLI and packs it: an NSIS setup (`.exe`, per-user install) on
//! Windows, a DMG on macOS. The settings live in
//! `apps/lutra-desktop/tauri.conf.json`; the installer images in
//! `assets/installer/`. On macOS the 1x and 2x DMG backgrounds are joined
//! into one HiDPI TIFF with `tiffutil` first, so Finder picks the sharp one
//! on a Retina display; without `tiffutil` the 1x PNG from the config is
//! used. Other platforms have no installer yet.
//! Builds the `lutra` command-line tool and the desktop app in release mode
//! with the bundled UI through the Tauri CLI and packs them: an NSIS setup
//! (`.exe`, per-user install) on Windows, a DMG on macOS. The settings live
//! in `apps/lutra-desktop/tauri.conf.json`; the installer images in
//! `assets/installer/`.
//!
//! The CLI goes in as a Tauri external binary: it is copied to
//! `<target>/sidecar/lutra-<host triple>[.exe]`, the name the bundler
//! expects, and named in `bundle.externalBin` through a `--config` override,
//! so plain `cargo build` and `cargo xtask ci` never need it. The bundler
//! puts it next to the app binary: `lutra.exe` in the install folder on
//! Windows, `Lutra.app/Contents/MacOS/lutra` on macOS.
//!
//! On macOS the 1x and 2x DMG backgrounds are joined into one HiDPI TIFF
//! with `tiffutil` first, so Finder picks the sharp one on a Retina display;
//! without `tiffutil` the 1x PNG from the config is used. Other platforms
//! have no installer yet.
//!
//! At the end it prints the installer's path, the app version and the size.
use std::path::{Path, PathBuf};
use crate::util::{TaskResult, repo_root, run};
use serde_json::{Value, json};
/// The Tauri bundle target and the output folder under `bundle/` for this
/// platform, or `None` without an installer.
fn target() -> Option<(&'static str, &'static str)> {
use crate::util::{TaskResult, capture, repo_root, run};
/// Folder under the target directory for the external binaries.
const SIDECAR_DIR: &str = "sidecar";
/// Name of the CLI binary, without the platform's extension.
const CLI_NAME: &str = "lutra";
/// The Tauri bundle target, the output folder under `bundle/` and the
/// installer's file extension for this platform, or `None` without an
/// installer.
fn target() -> Option<(&'static str, &'static str, &'static str)> {
if cfg!(windows) {
Some(("nsis", "nsis"))
Some(("nsis", "nsis", "exe"))
} else if cfg!(target_os = "macos") {
Some(("dmg", "dmg"))
Some(("dmg", "dmg", "dmg"))
} else {
None
}
}
/// Builds the platform's installer and prints where it is.
/// Builds the platform's installer and prints where it is and which
/// version it holds.
///
/// # Errors
/// Fails on a platform without an installer, if the frontend dependencies
/// cannot be installed, or if the Tauri CLI is missing or fails.
/// cannot be installed, the CLI cannot be built, the Tauri CLI is missing
/// or fails, or no installer was produced.
pub fn bundle() -> TaskResult {
let Some((bundles, folder)) = target() else {
let Some((bundles, folder, extension)) = target() else {
return Err("no installer for this platform yet (Windows: NSIS, macOS: DMG)".into());
};
let root = repo_root()?;
@ -39,21 +61,25 @@ pub fn bundle() -> TaskResult {
if !root.join("ui").join("node_modules").is_dir() {
run(&root.join("ui"), "pnpm", &["install", "--frozen-lockfile"])?;
}
let mut args = vec![
let target_dir = target_dir(&root);
let version = app_version(&root)?;
let sidecar = build_cli(&root, &cargo, &target_dir)?;
let mut config = sidecar_override(&sidecar);
if cfg!(target_os = "macos")
&& let Some(tiff) = hidpi_background(&root, &target_dir)
{
merge(&mut config, dmg_background_override(&tiff));
}
let args = vec![
"tauri".to_string(),
"build".into(),
"--bundles".into(),
bundles.into(),
"--features".into(),
"bundled-ui".into(),
"--config".into(),
config.to_string(),
];
let target_dir = target_dir(&root);
if cfg!(target_os = "macos")
&& let Some(tiff) = hidpi_background(&root, &target_dir)
{
args.push("--config".into());
args.push(dmg_background_override(&tiff));
}
let status = std::process::Command::new(&cargo)
.args(&args)
.current_dir(root.join("apps").join("lutra-desktop"))
@ -69,22 +95,33 @@ pub fn bundle() -> TaskResult {
let out = target_dir.join("release").join("bundle").join(folder);
let listing = std::fs::read_dir(&out)
.map_err(|e| format!("no installer folder {}: {e}", out.display()))?;
let mut found = false;
for entry in listing.flatten() {
let path = entry.path();
if !is_installer(&path, extension, &version) {
continue;
}
found = true;
let size = entry.metadata().map(|m| m.len()).unwrap_or(0);
println!(
"bundle: {} ({:.1} MB)",
"bundle: Lutra {version}: {} ({:.1} MB)",
path.display(),
size as f64 / 1_000_000.0
);
}
if !found {
return Err(format!(
"no Lutra {version} installer (*.{extension}) in {}",
out.display()
));
}
Ok(())
}
/// The Cargo target directory: `CARGO_TARGET_DIR` (relative to the
/// repository root) or `<root>/target`. [`bundle`] hands it to the Tauri
/// CLI as an absolute path: Cargo would read a relative one against
/// `apps/lutra-desktop`, where the CLI runs.
/// repository root) or `<root>/target`. [`bundle`] hands it to Cargo and
/// the Tauri CLI as an absolute path: Cargo would read a relative one
/// against `apps/lutra-desktop`, where the CLI runs.
fn target_dir(root: &Path) -> PathBuf {
match std::env::var_os("CARGO_TARGET_DIR").filter(|v| !v.is_empty()) {
Some(dir) => root.join(dir),
@ -92,6 +129,113 @@ fn target_dir(root: &Path) -> PathBuf {
}
}
/// The app version, `version` in `apps/lutra-desktop/tauri.conf.json`.
///
/// # Errors
/// If the file cannot be read or has no version string.
fn app_version(root: &Path) -> TaskResult<String> {
let path = root
.join("apps")
.join("lutra-desktop")
.join("tauri.conf.json");
let text = std::fs::read_to_string(&path)
.map_err(|e| format!("cannot read {}: {e}", path.display()))?;
version_of(&text).ok_or_else(|| format!("{} has no version", path.display()))
}
/// The `version` string of a Tauri config, or `None`.
fn version_of(config: &str) -> Option<String> {
let json: Value = serde_json::from_str(config).ok()?;
json.get("version")?.as_str().map(str::to_string)
}
/// Whether `path` is an installer of `version`: its extension is
/// `extension` and its file name contains `_<version>_`, as the bundler
/// names them (`Lutra_0.1.0-alpha_x64-setup.exe`). Older installers left in
/// the folder are skipped.
fn is_installer(path: &Path, extension: &str, version: &str) -> bool {
let name = path
.file_name()
.map(|n| n.to_string_lossy())
.unwrap_or_default();
path.extension()
.is_some_and(|e| e.eq_ignore_ascii_case(extension))
&& name.contains(&format!("_{version}_"))
}
/// Builds `lutra-cli` in release mode into `target_dir` and copies the
/// binary to the name the bundler expects (see [`sidecar_file`]).
///
/// # Returns
/// The external-binary path for the config: the copy without its triple
/// and extension.
///
/// # Errors
/// If the build fails, the host triple is unknown or the copy fails.
fn build_cli(root: &Path, cargo: &str, target_dir: &Path) -> TaskResult<PathBuf> {
let status = std::process::Command::new(cargo)
.args(["build", "--release", "-p", "lutra-cli"])
.current_dir(root)
.env("CARGO_TARGET_DIR", target_dir)
.status()
.map_err(|e| format!("cannot start `{cargo}`: {e}"))?;
if !status.success() {
return Err(format!(
"`{cargo} build --release -p lutra-cli` failed ({status})"
));
}
let triple = host_triple(&capture(root, "rustc", &["-vV"])?)
.ok_or("`rustc -vV` names no host triple")?;
let exe = std::env::consts::EXE_SUFFIX;
let built = target_dir.join("release").join(format!("{CLI_NAME}{exe}"));
let base = target_dir.join(SIDECAR_DIR).join(CLI_NAME);
let copy = sidecar_file(&base, &triple, exe);
std::fs::create_dir_all(target_dir.join(SIDECAR_DIR)).map_err(|e| {
format!(
"cannot create {}: {e}",
target_dir.join(SIDECAR_DIR).display()
)
})?;
std::fs::copy(&built, &copy)
.map_err(|e| format!("cannot copy {} to {}: {e}", built.display(), copy.display()))?;
Ok(base)
}
/// The host triple from the output of `rustc -vV` (its `host:` line).
fn host_triple(rustc_version: &str) -> Option<String> {
rustc_version
.lines()
.find_map(|line| line.strip_prefix("host:"))
.map(|triple| triple.trim().to_string())
.filter(|triple| !triple.is_empty())
}
/// The file the bundler looks for when the config names `base` as an
/// external binary: `<base>-<triple><exe suffix>`.
fn sidecar_file(base: &Path, triple: &str, exe_suffix: &str) -> PathBuf {
let mut name = base.as_os_str().to_os_string();
name.push(format!("-{triple}{exe_suffix}"));
PathBuf::from(name)
}
/// The `--config` JSON that adds `base` as an external binary.
fn sidecar_override(base: &Path) -> Value {
json!({ "bundle": { "externalBin": [base] } })
}
/// Merges the JSON object `extra` into `into`, key by key and recursively;
/// any other value of `extra` replaces the one in `into`.
fn merge(into: &mut Value, extra: Value) {
match (into, extra) {
(Value::Object(into), Value::Object(extra)) => {
for (key, value) in extra {
merge(into.entry(key).or_insert(Value::Null), value);
}
}
(into, extra) => *into = extra,
}
}
/// Joins `dmg-background.png` and `dmg-background@2x.png` into
/// `<target>/dmg-background.tiff` with `tiffutil -cathidpicheck`. `None`
/// (and a note) if that fails; the bundle then uses the 1x PNG.
@ -117,11 +261,10 @@ fn hidpi_background(root: &Path, target_dir: &Path) -> Option<PathBuf> {
/// The `--config` JSON for the Tauri CLI that sets the DMG background to
/// `tiff`.
fn dmg_background_override(tiff: &Path) -> String {
serde_json::json!({
fn dmg_background_override(tiff: &Path) -> Value {
json!({
"bundle": { "macOS": { "dmg": { "background": tiff } } }
})
.to_string()
}
#[cfg(test)]
@ -130,8 +273,71 @@ mod tests {
#[test]
fn the_override_names_the_tiff() {
let json: serde_json::Value =
serde_json::from_str(&dmg_background_override(Path::new("/t/bg.tiff"))).unwrap();
let json = dmg_background_override(Path::new("/t/bg.tiff"));
assert_eq!(json["bundle"]["macOS"]["dmg"]["background"], "/t/bg.tiff");
}
#[test]
fn the_overrides_merge_into_one_config() {
let mut config = sidecar_override(Path::new("/t/sidecar/lutra"));
merge(
&mut config,
dmg_background_override(Path::new("/t/bg.tiff")),
);
assert_eq!(config["bundle"]["externalBin"][0], "/t/sidecar/lutra");
assert_eq!(config["bundle"]["macOS"]["dmg"]["background"], "/t/bg.tiff");
}
#[test]
fn the_sidecar_carries_the_triple() {
let base = Path::new("/t/sidecar/lutra");
assert_eq!(
sidecar_file(base, "x86_64-pc-windows-msvc", ".exe"),
Path::new("/t/sidecar/lutra-x86_64-pc-windows-msvc.exe")
);
assert_eq!(
sidecar_file(base, "aarch64-apple-darwin", ""),
Path::new("/t/sidecar/lutra-aarch64-apple-darwin")
);
}
#[test]
fn the_host_triple_comes_from_rustc() {
let out = "rustc 1.95.0 (abc 2026-09-01)\nbinary: rustc\nhost: x86_64-pc-windows-msvc\nrelease: 1.95.0\n";
assert_eq!(host_triple(out).as_deref(), Some("x86_64-pc-windows-msvc"));
assert_eq!(host_triple("rustc 1.95.0\n"), None);
}
#[test]
fn the_version_comes_from_the_tauri_config() {
let conf = include_str!("../../apps/lutra-desktop/tauri.conf.json");
let version = version_of(conf).unwrap();
assert!(!version.is_empty());
assert_eq!(version_of("{}"), None);
}
#[test]
fn only_installers_of_this_version_are_listed() {
let v = "0.1.0-alpha";
assert!(is_installer(
Path::new("/b/Lutra_0.1.0-alpha_x64-setup.exe"),
"exe",
v
));
assert!(is_installer(
Path::new("/b/Lutra_0.1.0-alpha_aarch64.dmg"),
"dmg",
v
));
assert!(!is_installer(
Path::new("/b/Lutra_0.0.0_x64-setup.exe"),
"exe",
v
));
assert!(!is_installer(
Path::new("/b/Lutra_0.1.0-alpha_x64.nsi"),
"exe",
v
));
}
}