diff --git a/apps/gridcraft/src/profile_root.rs b/apps/gridcraft/src/profile_root.rs index 6264b350b..5e02dd621 100644 --- a/apps/gridcraft/src/profile_root.rs +++ b/apps/gridcraft/src/profile_root.rs @@ -211,10 +211,19 @@ fn validate_for( // takes its root from the Launcher only, so there is never a second candidate. return Err("nightly-v1 cannot be combined with GRIDCRAFT_CONFIG_DIR".into()); } - if platform != Platform::Windows && env("FLATPAK_ID").is_some_and(|v| !v.is_empty()) { - // These rows are native tar/app-bundle launches, not a Flatpak package. Inheriting its - // runtime namespace could point another profile at the stable app's flatpak folder. - return Err("nightly-v1 native packages cannot inherit a Flatpak instance; launch through the native Launcher".into()); + if platform != Platform::Windows + && let Some(id) = env("FLATPAK_ID").filter(|value| !value.is_empty()) + && id.to_string_lossy().as_ref() != gridcraft_ui_egui::channel::NIGHTLY_PACKAGE_ID + { + // `packaging/linux/flatpak-bundle.sh` builds GridCraft Nightly as its own Flatpak + // (`ai.storyteller.gridcraft.nightly`), whose sandbox is already a private home, so that one + // instance may carry a profile. Any other, the stable app's included, could point it at + // another installation's tree; these rows are otherwise native tar/app-bundle launches. + return Err(format!( + "nightly-v1 can only run inside its own Flatpak instance ({}), not {}", + gridcraft_ui_egui::channel::NIGHTLY_PACKAGE_ID, + id.to_string_lossy() + )); } let chosen = canonical_future(root)?; let exe = absolute(exe.ok_or("cannot establish the installation boundary")?.to_path_buf(), "the executable")?; @@ -408,6 +417,24 @@ mod tests { assert!(!candidate.exists()); } + #[test] + fn only_the_nightly_s_own_flatpak_instance_may_carry_a_profile() { + let mut fixture = Fixture::new(); + let root = fixture.root.join("flatpak profile"); + for (id, accepted) in [ + (gridcraft_ui_egui::channel::NIGHTLY_PACKAGE_ID, true), + // The stable Flatpak, and a lookalike that only shares the prefix, stay refused. + ("ai.storyteller.gridcraft", false), + ("ai.storyteller.gridcraft.nightly-v1", false), + ("io.github.other.Gridcraft", false), + ] { + fixture.env.insert("FLATPAK_ID".into(), id.into()); + let result = fixture.check(Platform::Linux, &root); + assert_eq!(result.is_ok(), accepted, "{id}"); + let _ = std::fs::remove_dir_all(&root); + } + } + #[test] fn failed_write_probe_never_falls_back_to_stable() { let fixture = Fixture::new(); diff --git a/crates/ui-egui/src/channel.rs b/crates/ui-egui/src/channel.rs index c6ebcd9e7..d48be6468 100644 --- a/crates/ui-egui/src/channel.rs +++ b/crates/ui-egui/src/channel.rs @@ -11,9 +11,12 @@ use std::sync::OnceLock; /// The stable window/Wayland app id, matching the `.desktop` file and the hicolor icon name. pub const APP_ID: &str = "ai.storyteller.gridcraft"; -/// The nightly window id and Windows AUMID. The package/bundle identity is -/// `ai.storyteller.gridcraft.nightly` (a different string, see packaging/). +/// The nightly window id and Windows AUMID. pub const NIGHTLY_APP_ID: &str = "ai.storyteller.gridcraft.nightly-v1"; +/// The nightly Flatpak package (and macOS bundle) identity, `packaging/linux/flatpak-bundle.sh`. +/// A different string from [`NIGHTLY_APP_ID`] on purpose: inside its sandbox the Flatpak names the +/// window after the package id, which is what its `.desktop` file and portal entry carry. +pub const NIGHTLY_PACKAGE_ID: &str = "ai.storyteller.gridcraft.nightly"; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Channel { @@ -124,6 +127,10 @@ mod tests { assert_eq!(id_for(Channel::Stable), "ai.storyteller.gridcraft"); assert_eq!(id_for(Channel::NightlyV1), "ai.storyteller.gridcraft.nightly-v1"); assert_ne!(id_for(Channel::Stable), id_for(Channel::NightlyV1)); + // Three strings, not two: the Flatpak package id is not the window id, and neither is stable's. + assert_eq!(NIGHTLY_PACKAGE_ID, "ai.storyteller.gridcraft.nightly"); + assert_ne!(NIGHTLY_PACKAGE_ID, id_for(Channel::NightlyV1)); + assert_ne!(NIGHTLY_PACKAGE_ID, id_for(Channel::Stable)); assert_eq!(name_for(Channel::Stable), "GridCraft"); assert_eq!(name_for(Channel::NightlyV1), "GridCraft Nightly"); assert!(stable_services_for(Channel::Stable)); diff --git a/packaging/linux/flatpak-bundle.sh b/packaging/linux/flatpak-bundle.sh index 0ce682293..9d276e92d 100755 --- a/packaging/linux/flatpak-bundle.sh +++ b/packaging/linux/flatpak-bundle.sh @@ -59,7 +59,29 @@ mkdir -p "$WORK/stage" tar -xzf "$TARBALL" -C "$WORK/stage" --strip-components=1 [[ -x "$WORK/stage/bin/gridcraft" ]] || { echo "error: $TARBALL has no bin/gridcraft" >&2; exit 1; } if [[ "$FLATPAK_ID" != "$APP_ID" ]]; then - sed -e "s|^Icon=$APP_ID$|Icon=$FLATPAK_ID|" \ + # The sandbox's own Flatpak id names the window (main.rs) and its data folder is already private, + # but the GUI still needs its launch profile: this launcher passes it, and the desktop entry and + # the manifest's one command name the launcher instead of the binary. + cat >"$WORK/stage/bin/gridcraft-nightly" <<'LAUNCHER' +#!/bin/sh +# GridCraft Nightly inside its own Flatpak: the launch profile is this app's private data folder. +set -eu +if [ -z "${XDG_DATA_HOME:-}" ] && [ -z "${HOME:-}" ]; then + echo "gridcraft-nightly: XDG_DATA_HOME and HOME are both unset, so there is no Nightly data folder" >&2 + exit 2 +fi +case "${1-}" in + --launch-profile | --profile-root) exec /app/bin/gridcraft "$@" ;; +esac +if [ "$#" -eq 1 ] && { [ "${1-}" = "--version" ] || [ "${1-}" = "-V" ]; }; then + exec /app/bin/gridcraft "$@" +fi +exec /app/bin/gridcraft --launch-profile nightly-v1 --profile-root "${XDG_DATA_HOME:-$HOME/.local/share}/gridcraft-nightly/profile" -- "$@" +LAUNCHER + chmod 755 "$WORK/stage/bin/gridcraft-nightly" + sed -e "s#^Exec=gridcraft\([[:space:]]\|$\)#Exec=gridcraft-nightly\1#" \ + -e "s|^TryExec=gridcraft$|TryExec=gridcraft-nightly|" \ + -e "s|^Icon=$APP_ID$|Icon=$FLATPAK_ID|" \ -e "s|^Name=GridCraft$|Name=GridCraft Nightly|" \ -e "s|^StartupWMClass=.*|StartupWMClass=$FLATPAK_ID|" \ -e '/^MimeType=/d' \ @@ -69,6 +91,8 @@ if [[ "$FLATPAK_ID" != "$APP_ID" ]]; then -e "s|GridCraft|GridCraft Nightly|g" \ -e "s|$APP_ID.desktop|$FLATPAK_ID.desktop|g" \ -e "s|$APP_ID|$FLATPAK_ID|g" \ + -e "s|gridcraft|gridcraft-nightly|g" \ + -e '//d' \ "$WORK/stage/share/metainfo/$APP_ID.metainfo.xml" >"$WORK/stage/share/metainfo/$FLATPAK_ID.metainfo.xml" rm -f "$WORK/stage/share/metainfo/$APP_ID.metainfo.xml" mv "$WORK/stage/share/mime/packages/$APP_ID.xml" "$WORK/stage/share/mime/packages/$FLATPAK_ID.xml" @@ -77,7 +101,10 @@ if [[ "$FLATPAK_ID" != "$APP_ID" ]]; then [[ -f "$NIGHTLY_ICON" ]] || NIGHTLY_ICON="$ROOT/assets/app-icon/hicolor/256x256/apps/$APP_ID.png" install -Dm644 "$NIGHTLY_ICON" \ "$WORK/stage/share/icons/hicolor/256x256/apps/$FLATPAK_ID.png" + # The manifest installs and runs the launcher; `command:` is what `flatpak run` starts. sed -e "s|ai\.storyteller\.gridcraft|$FLATPAK_ID|g" \ + -e "s|^command: gridcraft$|command: gridcraft-nightly|" \ + -e "s|^ - install -Dm755 bin/gridcraft-cli /app/bin/gridcraft-cli$|&\n - install -Dm755 bin/gridcraft-nightly /app/bin/gridcraft-nightly|" \ "$HERE/flatpak/$APP_ID.bundle.yml" >"$WORK/$FLATPAK_ID.yml" else cp "$HERE/flatpak/$APP_ID.bundle.yml" "$WORK/$FLATPAK_ID.yml" diff --git a/packaging/linux/nfpm.yaml b/packaging/linux/nfpm.yaml index 9ed545578..feee02f4d 100644 --- a/packaging/linux/nfpm.yaml +++ b/packaging/linux/nfpm.yaml @@ -55,6 +55,9 @@ overrides: - xdg-desktop-portal contents: + - src: ${STAGE}/usr/lib/gridcraft-nightly/gridcraft + dst: /usr/lib/gridcraft-nightly/gridcraft + file_info: { mode: 0755 } - src: ${STAGE}/usr/bin/${NFPM_BIN} dst: /usr/bin/${NFPM_BIN} file_info: { mode: 0755 } diff --git a/packaging/linux/nightly-arch-launch.sh b/packaging/linux/nightly-arch-launch.sh index c4fc2063c..e208b50f4 100755 --- a/packaging/linux/nightly-arch-launch.sh +++ b/packaging/linux/nightly-arch-launch.sh @@ -1,14 +1,20 @@ #!/bin/sh +# GridCraft Nightly for the Arch handoff: the GUI lives in .gridcraft-nightly/ beside this +# launcher. The launch profile is the only data root a nightly build accepts, and it holds the +# settings, logs, the control token and the session record, so no XDG variable has to move. set -eu -: "${XDG_CONFIG_HOME:=${HOME:-}/.config}" -: "${XDG_DATA_HOME:=${HOME:-}/.local/share}" -: "${XDG_STATE_HOME:=${HOME:-}/.local/state}" -: "${XDG_CACHE_HOME:=${HOME:-}/.cache}" -export XDG_CONFIG_HOME="$XDG_CONFIG_HOME/gridcraft-nightly" -export XDG_DATA_HOME="$XDG_DATA_HOME/gridcraft-nightly" -export XDG_STATE_HOME="$XDG_STATE_HOME/gridcraft-nightly" -export XDG_CACHE_HOME="$XDG_CACHE_HOME/gridcraft-nightly" +if [ -z "${XDG_DATA_HOME:-}" ] && [ -z "${HOME:-}" ]; then + echo "gridcraft-nightly: XDG_DATA_HOME and HOME are both unset, so there is no Nightly data folder" >&2 + exit 2 +fi real_bin="$(dirname "$0")/.gridcraft-nightly/gridcraft" -exec "$real_bin" "$@" +case "${1-}" in + --launch-profile | --profile-root) exec "$real_bin" "$@" ;; +esac +if [ "$#" -eq 1 ] && { [ "${1-}" = "--version" ] || [ "${1-}" = "-V" ]; }; then + exec "$real_bin" "$@" +fi +exec "$real_bin" --launch-profile nightly-v1 \ + --profile-root "${XDG_DATA_HOME:-$HOME/.local/share}/gridcraft-nightly/profile" -- "$@" diff --git a/packaging/linux/nightly-arch-tar-test.sh b/packaging/linux/nightly-arch-tar-test.sh index 8b648738c..ebc6f6d4a 100755 --- a/packaging/linux/nightly-arch-tar-test.sh +++ b/packaging/linux/nightly-arch-tar-test.sh @@ -31,19 +31,35 @@ mv "$bin/gridcraft-cli" "$bin/gridcraft-nightly-cli" test ! -e "$stage/bin/.gridcraft-nightly" cmp "$stage/bin/gridcraft" "$bin/.gridcraft-nightly/gridcraft" cmp "$stage/bin/gridcraft-cli" "$bin/gridcraft-nightly-cli" + +# The launcher starts the GUI through the flavor's launch profile, its only data root. +data="$work/data" +profile="$data/gridcraft-nightly/profile" expect() { expected="$1" shift - actual=$(PATH="$bin:$PATH" gridcraft-nightly "$@") + actual=$(XDG_DATA_HOME="$data" PATH="$bin:$PATH" gridcraft-nightly "$@") if [[ "$actual" != "$expected" ]]; then printf 'expected:\n%s\nactual:\n%s\n' "$expected" "$actual" >&2 exit 1 fi } -expect '<>' -expect "$(printf '<%s>\n' 'workbook with spaces.xlsx' '--other-name.xlsx')" \ +nightly_argv() { + printf '<%s>\n' --launch-profile nightly-v1 --profile-root "$profile" -- +} +expect "$(nightly_argv)" +expect "$(nightly_argv; printf '<%s>\n' 'workbook with spaces.xlsx' '--other-name.xlsx')" \ 'workbook with spaces.xlsx' '--other-name.xlsx' +# A bare version query and the Launcher's own protocol pass straight through. expect '<--version>' --version expect '<-V>' -V +expect "$(printf '<%s>\n' --launch-profile nightly-v1 --profile-root "$work/launcher" -- 'book.xlsx')" \ + --launch-profile nightly-v1 --profile-root "$work/launcher" -- 'book.xlsx' +# A profile needs a data folder: the launcher refuses instead of writing into /. +status=0 +env -u XDG_DATA_HOME -u HOME PATH="$bin:$PATH" gridcraft-nightly book.xlsx >/dev/null 2>&1 || status=$? +[[ "$status" -eq 2 ]] || { printf 'expected exit 2 without XDG_DATA_HOME/HOME, got %s\n' "$status" >&2; exit 1; } +test ! -e "$data/gridcraft-nightly" +test ! -e "$work/launcher" -echo "PASS: Arch Nightly handoff preserves launcher layout and arguments" +echo "PASS: Arch Nightly handoff preserves launcher layout, profile selection and arguments" diff --git a/packaging/linux/nightly-launch.sh b/packaging/linux/nightly-launch.sh new file mode 100755 index 000000000..e59d49061 --- /dev/null +++ b/packaging/linux/nightly-launch.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# GridCraft Nightly launcher for a moved tree: an AppImage (AppRun and usr/bin), the plain nightly +# tar, or a distro tarball's usr/bin. The launch profile is the only data root a nightly build +# accepts, so this is what keeps a Nightly run out of the stable app's settings, prefs and logs. +set -eu +here="$(cd "$(dirname "$0")" && pwd)" +real="$here/usr/bin/gridcraft" +[ -x "$real" ] || real="$here/gridcraft" +if [ ! -x "$real" ]; then + echo "gridcraft-nightly: no GridCraft binary beside $0" >&2 + exit 2 +fi +if [ -z "${XDG_DATA_HOME:-}" ] && [ -z "${HOME:-}" ]; then + echo "gridcraft-nightly: XDG_DATA_HOME and HOME are both unset, so there is no Nightly data folder" >&2 + exit 2 +fi +# A bare version query and the Launcher's own protocol pass straight through. +case "${1-}" in + --launch-profile | --profile-root) exec "$real" "$@" ;; +esac +if [ "$#" -eq 1 ] && { [ "${1-}" = "--version" ] || [ "${1-}" = "-V" ]; }; then + exec "$real" "$@" +fi +exec "$real" --launch-profile nightly-v1 --profile-root "${XDG_DATA_HOME:-$HOME/.local/share}/gridcraft-nightly/profile" -- "$@" diff --git a/packaging/linux/nightly-linux-launch-tests.py b/packaging/linux/nightly-linux-launch-tests.py new file mode 100644 index 000000000..3e95a7b96 --- /dev/null +++ b/packaging/linux/nightly-linux-launch-tests.py @@ -0,0 +1,233 @@ +#!/usr/bin/env python3 +"""The Linux launch profile, staged the way each format ships it; no cargo build, no GUI, no network. + +What is checked, all synthetic: + * `nightly-launch.sh` (the AppImage and tar launcher) passes the flavor's own tokens and nothing else; + * `package.sh` stages the deb/rpm, AppImage and tar launchers and a nightly desktop entry, and + leaves a stable tree exactly as it was. `nfpm`, `appimagetool` and `strip` are stubs on PATH. +""" +import os +from pathlib import Path +import shutil +import subprocess +import tarfile +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[2] +LAUNCHER = ROOT / "packaging/linux/nightly-launch.sh" +PACKAGE = ROOT / "packaging/linux/package.sh" +NIGHTLY_VERSION = "0.19.1-nightly.20261124.7.1.gabcdef012345" +PROFILE = "gridcraft-nightly/profile" + +RECORDER = """#!/bin/sh +printf '<%s>\\n' "$@" +""" + + +def run(argv, cwd, env, check=True): + return subprocess.run(argv, cwd=cwd, env=env, check=check, capture_output=True, text=True) + + +class MovedTreeLauncher(unittest.TestCase): + """nightly-launch.sh as AppRun and as an unpacked tar's launcher.""" + + def setUp(self): + self.scratch = tempfile.TemporaryDirectory() + self.addCleanup(self.scratch.cleanup) + self.root = Path(self.scratch.name) + self.data = self.root / "data" + self.env = {"PATH": os.environ["PATH"], "XDG_DATA_HOME": str(self.data)} + + def install(self, layout): + """AppImage layout: launcher at the root; tar layout: launcher beside the binary.""" + tree = self.root / layout + binary = tree / ("usr/bin/gridcraft" if layout == "appdir" else "gridcraft") + binary.parent.mkdir(parents=True) + binary.write_text(RECORDER) + binary.chmod(0o755) + launcher = tree / ("AppRun" if layout == "appdir" else "gridcraft-nightly") + shutil.copy2(LAUNCHER, launcher) + launcher.chmod(0o755) + return launcher + + def query(self, launcher, *args): + return run([str(launcher), *args], cwd=self.root, env=self.env) + + def tokens(self): + return ["<--launch-profile>", "", "<--profile-root>", f"<{self.data / PROFILE}>", "<-->"] + + def test_both_layouts_start_the_gui_through_the_launch_profile(self): + for layout in ("appdir", "tar"): + with self.subTest(layout=layout): + launcher = self.install(layout) + self.assertEqual(self.query(launcher).stdout.splitlines(), self.tokens()) + self.assertEqual( + self.query(launcher, "workbook with spaces.xlsx", "--other-name.xlsx").stdout.splitlines(), + self.tokens() + ["", "<--other-name.xlsx>"], + ) + + def test_a_bare_version_query_and_the_launcher_protocol_pass_through(self): + launcher = self.install("appdir") + for args in (["--version"], ["-V"], ["--launch-profile", "nightly-v1", "--profile-root", "/launcher", "--", "book.xlsx"]): + with self.subTest(args=args): + self.assertEqual(self.query(launcher, *args).stdout.splitlines(), [f"<{arg}>" for arg in args]) + + def test_a_missing_data_folder_or_binary_refuses_before_running_anything(self): + launcher = self.install("appdir") + refused = run([str(launcher), "book.xlsx"], cwd=self.root, env={"PATH": os.environ["PATH"]}, check=False) + self.assertEqual(refused.returncode, 2) + self.assertIn("XDG_DATA_HOME and HOME are both unset", refused.stderr) + (self.root / "appdir/usr/bin/gridcraft").unlink() + missing = run([str(launcher)], cwd=self.root, env=self.env, check=False) + self.assertEqual(missing.returncode, 2) + self.assertIn("no GridCraft binary", missing.stderr) + self.assertFalse(self.data.exists(), "a refused launch creates no data folder") + + def test_the_launcher_is_portable_shell(self): + run(["sh", "-n", str(LAUNCHER)], cwd=self.root, env=self.env) + self.assertTrue(LAUNCHER.read_text().startswith("#!/bin/sh\n")) + + +class StagedFormats(unittest.TestCase): + """package.sh with stub binaries, a stub nfpm and a stub strip.""" + + def setUp(self): + self.scratch = tempfile.TemporaryDirectory() + self.addCleanup(self.scratch.cleanup) + self.root = Path(self.scratch.name) + self.target = self.root / "target" + self.dist = self.root / "dist" + self.stubs = self.root / "stubs" + for directory in (self.target / "release", self.dist, self.stubs): + directory.mkdir(parents=True, exist_ok=True) + for name in ("gridcraft", "gridcraft-cli"): + binary = self.target / "release" / name + binary.write_text(RECORDER) + binary.chmod(0o755) + # `strip` would refuse the recorder scripts; the real one is a no-op for a non-ELF file too. + (self.stubs / "strip").write_text("#!/bin/sh\nexit 0\n") + (self.stubs / "strip").chmod(0o755) + (self.stubs / "nfpm").write_text( + '#!/bin/sh\n' + 'while [ "$#" -gt 0 ]; do [ "$1" = "-f" ] && cp "$2" "$STUB_NFPM_DIR/rendered.yaml"; shift; done\n' + 'exit 0\n' + ) + (self.stubs / "nfpm").chmod(0o755) + self.env = { + "PATH": f"{self.stubs}:{os.environ['PATH']}", + "HOME": os.environ.get("HOME", str(self.root)), + "CARGO_TARGET_DIR": str(self.target), + "DIST": str(self.dist), + "STUB_NFPM_DIR": str(self.root), + "GRIDCRAFT_BUILD_DATE": "2026-11-24", + "GRIDCRAFT_BUILD_SHA": "a" * 40, + } + + def stage(self, formats="deb tar", **extra): + env = dict(self.env, **extra) + run(["bash", str(PACKAGE), "--skip-build", "--formats", formats], cwd=ROOT, env=env) + return self.target / "linux-package" + + def nightly(self): + return self.stage(GRIDCRAFT_BUILD_CHANNEL="nightly-v1", GRIDCRAFT_VERSION=NIGHTLY_VERSION) + + def test_nightly_deb_installs_a_launcher_and_the_gui_beside_it(self): + work = self.nightly() + stage = work / "nfpm-root" + launcher = stage / "usr/bin/gridcraft-nightly" + gui = stage / "usr/lib/gridcraft-nightly/gridcraft" + self.assertTrue(self.executable(launcher)) + self.assertTrue(self.executable(gui)) + self.assertFalse((stage / "usr/bin/gridcraft").exists(), "the GUI moved out of usr/bin") + self.assertTrue(self.executable(stage / "usr/bin/gridcraft-nightly-cli")) + text = launcher.read_text() + self.assertIn('exec "/usr/lib/gridcraft-nightly/gridcraft" --launch-profile nightly-v1', text) + self.assertIn(f"/{PROFILE}", text) + self.assertEqual(gui.read_text(), RECORDER, "the packaged GUI is the built binary, unchanged") + rendered = (self.root / "rendered.yaml").read_text() + self.assertIn(f"src: {stage}/usr/lib/gridcraft-nightly/gridcraft", rendered) + self.assertIn("dst: /usr/lib/gridcraft-nightly/gridcraft", rendered) + self.assertIn("file_info: { mode: 0755 }", rendered) + + def test_nightly_desktop_and_metainfo_are_the_nightly_identity(self): + work = self.nightly() + applications = work / "nfpm-root/usr/share/applications" + desktop = applications / "ai.storyteller.gridcraft.nightly-v1.desktop" + self.assertEqual(sorted(p.name for p in applications.iterdir()), [desktop.name]) + text = desktop.read_text() + self.assertIn("Exec=gridcraft-nightly %F\n", text) + self.assertIn("TryExec=gridcraft-nightly\n", text) + self.assertIn("StartupWMClass=ai.storyteller.gridcraft.nightly-v1\n", text) + self.assertIn("Name=GridCraft Nightly\n", text) + self.assertNotIn("MimeType=", text) + metainfo = (work / "nfpm-root/usr/share/metainfo/ai.storyteller.gridcraft.nightly-v1.metainfo.xml").read_text() + self.assertNotIn("", metainfo) + self.assertIn("gridcraft-nightly", metainfo) + + def test_nightly_tar_ships_the_launcher_beside_the_binary(self): + self.nightly() + archive = self.dist / f"gridcraft-{NIGHTLY_VERSION}-linux-x86_64.tar.gz" + with tarfile.open(archive) as tar: + names = {member.name.split("/", 1)[-1] for member in tar.getmembers()} + launcher = tar.extractfile("gridcraft-%s-linux-x86_64/bin/gridcraft-nightly" % NIGHTLY_VERSION).read().decode() + binary = tar.extractfile("gridcraft-%s-linux-x86_64/bin/gridcraft" % NIGHTLY_VERSION).read().decode() + desktop = tar.extractfile("gridcraft-%s-linux-x86_64/share/applications/ai.storyteller.gridcraft.desktop" % NIGHTLY_VERSION).read().decode() + self.assertIn("bin/gridcraft", names) + self.assertEqual(binary, RECORDER) + self.assertEqual(launcher, LAUNCHER.read_text()) + self.assertIn("Exec=gridcraft-nightly %F\n", desktop) + self.assertIn("Name=GridCraft Nightly\n", desktop) + self.assertNotIn("MimeType=", desktop) + + def test_a_stable_tree_is_unchanged(self): + work = self.stage() + stage = work / "root" + self.assertEqual(stage, work / "root", "a stable build packages its one stage") + self.assertTrue(self.executable(stage / "usr/bin/gridcraft")) + self.assertTrue(self.executable(stage / "usr/bin/gridcraft-cli")) + self.assertFalse((stage / "usr/bin/gridcraft-nightly").exists()) + self.assertFalse((stage / "usr/lib").exists()) + rendered = (self.root / "rendered.yaml").read_text() + self.assertNotIn("gridcraft-nightly", rendered) + self.assertIn(f"src: {stage}/usr/bin/gridcraft\n", rendered) + desktop = (stage / "usr/share/applications/ai.storyteller.gridcraft.desktop").read_text() + self.assertIn("Exec=gridcraft %F\n", desktop) + self.assertIn("MimeType=", desktop) + with tarfile.open(self.dist / "gridcraft-0.19.1-linux-x86_64.tar.gz") as tar: + names = {member.name.split("/", 1)[-1] for member in tar.getmembers()} + self.assertIn("bin/gridcraft", names) + self.assertNotIn("bin/gridcraft-nightly", names) + + def test_nightly_appimage_runs_its_launcher_as_apprun(self): + tool = self.stubs / "appimagetool" + tool.write_text("#!/bin/sh\nprintf '%s\\n' \"$@\" >\"$STUB_NFPM_DIR/appimagetool.argv\"\n") + tool.chmod(0o755) + (self.stubs / "runtime").write_text("runtime fixture") + work = self.stage( + "appimage", + GRIDCRAFT_BUILD_CHANNEL="nightly-v1", + GRIDCRAFT_VERSION=NIGHTLY_VERSION, + APPIMAGETOOL=str(tool), + APPIMAGE_RUNTIME=str(self.stubs / "runtime"), + ) + appdir = work / "GridCraft.AppDir" + self.assertTrue(self.executable(appdir / "AppRun")) + self.assertEqual((appdir / "AppRun").read_text(), LAUNCHER.read_text(), "AppRun is the launcher, not a symlink") + self.assertFalse((appdir / "AppRun").is_symlink()) + self.assertEqual((appdir / "usr/bin/gridcraft-nightly").read_text(), LAUNCHER.read_text()) + self.assertEqual((appdir / "usr/bin/gridcraft").read_text(), RECORDER, "the binary stays in usr/bin") + desktop = (appdir / "usr/share/applications/ai.storyteller.gridcraft.nightly-v1.desktop").read_text() + self.assertIn("Exec=gridcraft-nightly %F\n", desktop) + self.assertIn("TryExec=gridcraft-nightly\n", desktop) + self.assertIn("StartupWMClass=ai.storyteller.gridcraft.nightly-v1\n", desktop) + self.assertNotIn("MimeType=", desktop) + + def executable(self, path): + self.assertTrue(path.is_file(), f"{path} is not a file") + self.assertTrue(os.access(path, os.X_OK), f"{path} is not executable") + return True + + +if __name__ == "__main__": + unittest.main() diff --git a/packaging/linux/nightly.sh b/packaging/linux/nightly.sh index 4fde63e4b..d35128343 100755 --- a/packaging/linux/nightly.sh +++ b/packaging/linux/nightly.sh @@ -20,6 +20,10 @@ STAGE="$NIGHTLY_WORK/stage" mkdir -p "$STAGE" cp "$NIGHTLY_GUI" "$STAGE/gridcraft" chmod 755 "$STAGE/gridcraft" +# An unpacked archive is isolated too: the GUI refuses to start without a launch profile, so the +# launcher beside it is how a user starts it. +cp "$ROOT/packaging/linux/nightly-launch.sh" "$STAGE/gridcraft-nightly" +chmod 755 "$STAGE/gridcraft-nightly" # The nightly channel's own icon (deeper field); the archive member name stays as the helper requires. cp "$ROOT/assets/app-icon/gridcraft-nightly.png" "$STAGE/gridcraft-nightly.png" nightly_copy_notices "$STAGE" diff --git a/packaging/linux/package.sh b/packaging/linux/package.sh index 405a54335..58b9c5faa 100755 --- a/packaging/linux/package.sh +++ b/packaging/linux/package.sh @@ -5,7 +5,12 @@ # $DIST/gridcraft--linux-.AppImage.zsync delta updates (needs zsyncmake) # $DIST/gridcraft--linux-.deb Debian, Ubuntu, Mint, Pop!_OS, ... # $DIST/gridcraft--linux-.rpm Fedora, openSUSE, RHEL, ... -# $DIST/gridcraft--linux-.tar.gz plain FHS-style tree (bin/, share/) +# $DIST/gridcraft--linux-.tar.gz plain tree (bin/, share/) +# +# A nightly build (`GRIDCRAFT_BUILD_CHANNEL=nightly-v1`) starts through the flavor's launcher in +# every format: usr/bin/gridcraft-nightly (deb/rpm, beside the GUI in usr/lib), the AppImage's +# AppRun script, and bin/gridcraft-nightly in the plain tree tarball. A nightly build refuses to +# start without `--launch-profile nightly-v1 --profile-root `, so every format ships one. # # Usage: packaging/linux/package.sh [--skip-build] [--formats "appimage deb rpm tar"] [--arch-tar-dir DIRECTORY] # @@ -103,10 +108,46 @@ has() { case " $FORMATS " in *" $format "*) return 0 ;; *) return 1 ;; esac } +# The deb/rpm launcher: /usr/bin/gridcraft-nightly starts the GUI from /usr/lib with this channel's +# own launch profile. A nightly build refuses to start without one, and the profile is the only +# data root it accepts, so a distro package can never share the stable app's settings, prefs or logs. +nightly_launcher() { + local destination="$1" real_bin="$2" + cat >"$destination" <&2 + exit 2 +fi +case "\${1-}" in + --launch-profile | --profile-root) exec "$real_bin" "\$@" ;; +esac +if [ "\$#" -eq 1 ] && { [ "\${1-}" = "--version" ] || [ "\${1-}" = "-V" ]; }; then + exec "$real_bin" "\$@" +fi +exec "$real_bin" --launch-profile nightly-v1 --profile-root "\${XDG_DATA_HOME:-\$HOME/.local/share}/gridcraft-nightly/profile" -- "\$@" +LAUNCHER + chmod 755 "$destination" +} + # ---- .tar.gz ------------------------------------------------------------------------------------ if has tar; then mkdir -p "$WORK/tar" cp -R "$STAGE/usr" "$WORK/tar/$BASENAME" + if [[ "$IS_NIGHTLY" = 1 ]]; then + # An unpacked nightly tar is isolated too: bin/gridcraft stays the binary, and the launcher + # beside it is how it starts (the binary itself refuses to run without a launch profile). + install -Dm755 "$HERE/nightly-launch.sh" "$WORK/tar/$BASENAME/bin/gridcraft-nightly" + TAR_DESK="$WORK/tar/$BASENAME/share/applications/$APP_ID.desktop" + sed -e "s#^Exec=gridcraft\([[:space:]]\|$\)#Exec=gridcraft-nightly\1#" \ + -e "s|^TryExec=gridcraft$|TryExec=gridcraft-nightly|" \ + -e "s|^Name=GridCraft$|Name=GridCraft Nightly|" \ + -e '/^MimeType=/d' \ + "$TAR_DESK" >"$TAR_DESK.tmp" + mv "$TAR_DESK.tmp" "$TAR_DESK" + fi tar -C "$WORK/tar" -czf "$DIST/$BASENAME.tar.gz" "$BASENAME" echo "wrote $DIST/$BASENAME.tar.gz" fi @@ -139,7 +180,11 @@ if has deb || has rpm; then NFPM_STAGE="$WORK/nfpm-root" rm -rf "$NFPM_STAGE" cp -R "$STAGE" "$NFPM_STAGE" - mv "$NFPM_STAGE/usr/bin/gridcraft" "$NFPM_STAGE/usr/bin/$NFPM_BIN" + # The GUI keeps its name under /usr/lib; /usr/bin/-nightly is its launcher (see + # nightly_launcher), so the desktop entry never starts a binary that refuses to run. + install -Dm755 "$NFPM_STAGE/usr/bin/gridcraft" "$NFPM_STAGE/usr/lib/gridcraft-nightly/gridcraft" + rm -f "$NFPM_STAGE/usr/bin/gridcraft" + nightly_launcher "$NFPM_STAGE/usr/bin/$NFPM_BIN" /usr/lib/gridcraft-nightly/gridcraft mv "$NFPM_STAGE/usr/bin/gridcraft-cli" "$NFPM_STAGE/usr/bin/$NFPM_CLI" mv "$NFPM_STAGE/usr/share/doc/gridcraft" "$NFPM_STAGE/usr/share/doc/$NFPM_NAME" sed -e "s#^Exec=gridcraft\([[:space:]]\|$\)#Exec=$NFPM_BIN\1#" \ @@ -155,8 +200,16 @@ if has deb || has rpm; then -e "s|$APP_ID|$NIGHTLY_DESK_ID|g" \ -e "s|gridcraft|$NFPM_BIN|g" \ -e "s|gridcraft-cli|$NFPM_CLI|g" \ + -e '//d' \ "$NFPM_STAGE/usr/share/metainfo/$APP_ID.metainfo.xml" >"$NFPM_STAGE/usr/share/metainfo/$NIGHTLY_DESK_ID.metainfo.xml" rm -f "$NFPM_STAGE/usr/share/metainfo/$APP_ID.metainfo.xml" + # The nightly files are different files: validate those too, not only the stable ones above. + if command -v desktop-file-validate >/dev/null; then + desktop-file-validate "$NFPM_STAGE/usr/share/applications/$NIGHTLY_DESK_ID.desktop" + fi + if command -v appstreamcli >/dev/null; then + appstreamcli validate --no-net --explain "$NFPM_STAGE/usr/share/metainfo/$NIGHTLY_DESK_ID.metainfo.xml" + fi rm -rf "$NFPM_STAGE/usr/share/mime" rm -rf "$NFPM_STAGE/usr/share/icons/hicolor" NIGHTLY_ICON="$ROOT/assets/app-icon/gridcraft-nightly.png" @@ -165,8 +218,14 @@ if has deb || has rpm; then "$NFPM_STAGE/usr/share/icons/hicolor/256x256/apps/$NIGHTLY_DESK_ID.png" fi # nfpm expands env vars in fields like `version` and `arch`, but not in `contents[].src`. + # A nightly package keeps its GUI under /usr/lib behind usr/bin/gridcraft-nightly (see + # nightly_launcher). A stable tree has no such file, so this three-line entry is dropped and the + # rendered file stays exactly what main renders. + DROP_LIB_ENTRY= # shellcheck disable=SC2016 - sed "s|\${STAGE}|$NFPM_STAGE|g;s|\${NFPM_BIN}|$NFPM_BIN|g;s|\${NFPM_CLI}|$NFPM_CLI|g" "$HERE/nfpm.yaml" >"$WORK/nfpm.yaml" + [[ -e "$NFPM_STAGE/usr/lib/gridcraft-nightly/gridcraft" ]] || DROP_LIB_ENTRY='\|^ - src: \${STAGE}/usr/lib/gridcraft-nightly/gridcraft|,+2d;' + # shellcheck disable=SC2016 + sed "${DROP_LIB_ENTRY}s|\${STAGE}|$NFPM_STAGE|g;s|\${NFPM_BIN}|$NFPM_BIN|g;s|\${NFPM_CLI}|$NFPM_CLI|g" "$HERE/nfpm.yaml" >"$WORK/nfpm.yaml" for fmt in deb rpm; do if has "$fmt"; then (cd "$ROOT" && nfpm package -f "$WORK/nfpm.yaml" -p "$fmt" -t "$DIST/$BASENAME.$fmt"); fi done @@ -202,10 +261,15 @@ if has appimage; then } APPDIR="$WORK/GridCraft.AppDir" cp -R "$STAGE" "$APPDIR" - ln -s usr/bin/gridcraft "$APPDIR/AppRun" if [[ "$IS_NIGHTLY" = 1 ]]; then + # A script, not the stable symlink: AppRun starts the GUI through its launch profile, and the + # desktop entry names the same launcher for integrations that read it. + install -Dm755 "$HERE/nightly-launch.sh" "$APPDIR/AppRun" + install -Dm755 "$HERE/nightly-launch.sh" "$APPDIR/usr/bin/gridcraft-nightly" APPIMAGE_ID="$APP_ID.nightly-v1" - sed -e "s|^Icon=$APP_ID$|Icon=$APPIMAGE_ID|" \ + sed -e "s#^Exec=gridcraft\([[:space:]]\|$\)#Exec=gridcraft-nightly\1#" \ + -e "s|^TryExec=gridcraft$|TryExec=gridcraft-nightly|" \ + -e "s|^Icon=$APP_ID$|Icon=$APPIMAGE_ID|" \ -e "s|^Name=GridCraft$|Name=GridCraft Nightly|" \ -e '/^MimeType=/d' \ "$APPDIR/usr/share/applications/$APP_ID.desktop" >"$APPDIR/usr/share/applications/$APPIMAGE_ID.desktop" @@ -219,6 +283,7 @@ if has appimage; then cp "$NIGHTLY_ICON" "$APPDIR/$APPIMAGE_ID.png" ln -s "$APPIMAGE_ID.png" "$APPDIR/.DirIcon" else + ln -s usr/bin/gridcraft "$APPDIR/AppRun" cp "$HERE/$APP_ID.desktop" "$APPDIR/$APP_ID.desktop" cp "$ROOT/assets/app-icon/hicolor/256x256/apps/$APP_ID.png" "$APPDIR/$APP_ID.png" ln -s "$APP_ID.png" "$APPDIR/.DirIcon" diff --git a/packaging/nightly-archive-tests.py b/packaging/nightly-archive-tests.py index 398a59fb3..1b5b172fb 100755 --- a/packaging/nightly-archive-tests.py +++ b/packaging/nightly-archive-tests.py @@ -29,10 +29,11 @@ class ArchivePolicy(unittest.TestCase): self.stage = self.root / "stage" self.stage.mkdir() self.output = self.root / "nightly.tar.gz" - self.required = {"gridcraft", "gridcraft-nightly.png", "README.md", "LICENSE-MIT", "LICENSE-APACHE", "NOTICE", "ATTRIBUTION.md"} + self.required = {"gridcraft", "gridcraft-nightly", "gridcraft-nightly.png", "README.md", "LICENSE-MIT", "LICENSE-APACHE", "NOTICE", "ATTRIBUTION.md"} for name in self.required: (self.stage / name).write_bytes(b"generated fixture: " + name.encode()) (self.stage / "gridcraft").chmod(0o755) + (self.stage / "gridcraft-nightly").chmod(0o755) def test_flat_regular_ustar_with_all_notices(self): (self.stage / "OFL-Font.txt").write_text("generated fixture license", encoding="utf-8") diff --git a/packaging/nightly-archive.py b/packaging/nightly-archive.py index 229a13451..cec5500c4 100644 --- a/packaging/nightly-archive.py +++ b/packaging/nightly-archive.py @@ -33,7 +33,7 @@ def checked_entries(root, stage): total = 0 for entry in entries: name = entry.name - if name not in {"gridcraft", "gridcraft-nightly.png"} and not notice(name): + if name not in {"gridcraft", "gridcraft-nightly", "gridcraft-nightly.png"} and not notice(name): raise ValueError(f"unexpected nightly member: {name}") if len(name.encode("utf-8")) > 100: raise ValueError(f"nightly member must be a short regular file: {name}") @@ -44,7 +44,7 @@ def checked_entries(root, stage): if name.casefold() in folded: raise ValueError(f"case-colliding nightly member: {name}") folded.add(name.casefold()) - required = {"gridcraft", "gridcraft-nightly.png", "README.md", "LICENSE-MIT", "LICENSE-APACHE", "NOTICE", "ATTRIBUTION.md"} + required = {"gridcraft", "gridcraft-nightly", "gridcraft-nightly.png", "README.md", "LICENSE-MIT", "LICENSE-APACHE", "NOTICE", "ATTRIBUTION.md"} if not required.issubset({item.name for item in entries}): raise ValueError("required GUI/icon/notices missing") return entries diff --git a/packaging/nightly-common.sh b/packaging/nightly-common.sh index 38741a986..5a1a319d8 100644 --- a/packaging/nightly-common.sh +++ b/packaging/nightly-common.sh @@ -34,7 +34,10 @@ nightly_init() { # Compile only: macOS runs the version query after signing (packaging/macos/nightly-verify.sh). nightly_compile_gui() { - (cd "$ROOT" && GRIDCRAFT_OVERRIDE_VERSION="$VERSION" cargo build --release --locked -p gridcraft --target "$NIGHTLY_RUST_TARGET") + # `build.rs` reads GRIDCRAFT_VERSION (it bakes it as GRIDCRAFT_BUILT_VERSION, the value the + # binary answers `--version` with), so the prepared identity is passed explicitly here as well as + # through the inherited environment; VERSION is that same value (see env.sh). + (cd "$ROOT" && GRIDCRAFT_VERSION="$VERSION" GRIDCRAFT_OVERRIDE_VERSION="$VERSION" cargo build --release --locked -p gridcraft --target "$NIGHTLY_RUST_TARGET") NIGHTLY_GUI="$CARGO_TARGET_DIR/$NIGHTLY_RUST_TARGET/release/gridcraft" [[ -f "$NIGHTLY_GUI" && ! -L "$NIGHTLY_GUI" ]] || nightly_die 'the owned GUI build did not produce a regular executable' [[ "$(git -C "$ROOT" rev-parse HEAD)" == "$GRIDCRAFT_BUILD_SHA" && -z "$(git -C "$ROOT" status --porcelain)" ]] || nightly_die 'source changed during the build' @@ -44,7 +47,10 @@ nightly_build_gui() { nightly_compile_gui local actual actual="$("$NIGHTLY_GUI" --version)" || nightly_die 'side-effect-free nightly version verification failed' - [[ "$actual" == "GridCraft $VERSION (nightly-v1)" ]] || nightly_die 'the built version/flavor is not the prepared identity' + # Exactly what the flavor prints (`build_flavor::version` in apps/gridcraft): the prepared + # version followed by the build channel. A stable build prints the Cargo version instead. + [[ "$actual" == "GridCraft $VERSION (nightly-v1)" ]] \ + || nightly_die "the built identity is '$actual', not 'GridCraft $VERSION (nightly-v1)'" [[ "$(git -C "$ROOT" rev-parse HEAD)" == "$GRIDCRAFT_BUILD_SHA" && -z "$(git -C "$ROOT" status --porcelain)" ]] || nightly_die 'source changed during the version query' } diff --git a/packaging/nightly-release-tests.py b/packaging/nightly-release-tests.py index c52382754..eaa46e3e7 100755 --- a/packaging/nightly-release-tests.py +++ b/packaging/nightly-release-tests.py @@ -286,6 +286,7 @@ class TrustedSourcePreparation(unittest.TestCase): self.assertEqual(len(inclusions), 1) entries = inclusions[0].split(",") expected = {"packaging/nightly-tests.py", "packaging/nightly-archive-tests.py", + "packaging/linux/nightly-linux-launch-tests.py", "packaging/macos/nightly_plist_tests.py", "packaging/macos/nightly_sign_tests.py", "packaging/windows/nightly-package-tests.ps1", "packaging/windows/nightly-signing-tests.ps1", "packaging/nightly-release-tests.py"} diff --git a/packaging/nightly-tests.py b/packaging/nightly-tests.py index 89d4ee4bf..684239f49 100755 --- a/packaging/nightly-tests.py +++ b/packaging/nightly-tests.py @@ -13,6 +13,7 @@ def main() -> int: ("gridcraft_plist_suite", "macos/nightly_plist_tests.py"), ("gridcraft_release_dispatch_suite", "nightly-release-tests.py"), ("gridcraft_mac_sign_suite", "macos/nightly_sign_tests.py"), + ("gridcraft_linux_launch_suite", "linux/nightly-linux-launch-tests.py"), ): spec = importlib.util.spec_from_file_location(name, here / relative) module = importlib.util.module_from_spec(spec) diff --git a/sonar-project.properties b/sonar-project.properties index cda09d5a0..5b6737236 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,7 +6,7 @@ sonar.organization=storytold sonar.sources=. sonar.tests=packaging # Only these are test runners: the helpers they exercise stay production code. -sonar.test.inclusions=packaging/nightly-tests.py,packaging/nightly-archive-tests.py,packaging/macos/nightly_plist_tests.py,packaging/macos/nightly_sign_tests.py,packaging/windows/nightly-package-tests.ps1,packaging/windows/nightly-signing-tests.ps1,packaging/nightly-release-tests.py +sonar.test.inclusions=packaging/nightly-tests.py,packaging/nightly-archive-tests.py,packaging/linux/nightly-linux-launch-tests.py,packaging/macos/nightly_plist_tests.py,packaging/macos/nightly_sign_tests.py,packaging/windows/nightly-package-tests.ps1,packaging/windows/nightly-signing-tests.ps1,packaging/nightly-release-tests.py # The region table is one record per locale with the same field names and many shared values # (month names, formats); that repetition is data, not copy-pasted logic.