diff --git a/.github/actions/package/action.yml b/.github/actions/package/action.yml index aca93b13..609075c5 100644 --- a/.github/actions/package/action.yml +++ b/.github/actions/package/action.yml @@ -113,6 +113,34 @@ runs: check AppImage "$(ls "$RUNNER_TEMP/squashfs-root/usr/bin")" devnotes fi + # The icon the menus, the dock and the file managers show, read from the package and not from + # the window. Windows reads it from devnotes.exe, which a test of `crates/app` holds. + - name: Each package carries the icon + if: runner.os != 'Windows' + shell: bash + run: | + packages="$GITHUB_WORKSPACE/target/release/packages" + need() { + if ! echo "$2" | grep -qxF -- "$3"; then + echo "::error::$1 has no $3" + exit 1 + fi + echo "$1: $3" + } + if [ "$RUNNER_OS" = macOS ]; then + app="$packages/DevNotes.app/Contents" + icns=$(plutil -extract CFBundleIconFile raw "$app/Info.plist") + need app "$(ls "$app/Resources")" "$icns" + need archive "$(tar -tzf "$(ls "$packages"/*.app.tar.gz)" | xargs -n1 basename)" "$icns" + else + icon=usr/share/icons/hicolor/512x512/apps/devnotes.png + deb=$(ls "$packages"/*.deb) + need deb "$(dpkg-deb -c "$deb" | awk '{ print $6 }' | sed 's|^\./||')" "$icon" + need deb "$(dpkg-deb --fsys-tarfile "$deb" | tar -xO --wildcards '*usr/share/applications/devnotes.desktop')" Icon=devnotes + need rpm "$(rpm -qlp "$(ls "$packages"/*.rpm)")" "/$icon" + need AppImage "$(ls -A "$RUNNER_TEMP/squashfs-root")" .DirIcon + fi + # The packages, their signatures, and the standalone executable, renamed so that the two # operating systems do not overlap in a release. - name: Gather diff --git a/Cargo.lock b/Cargo.lock index 4f9396f1..3174980f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,8 +263,10 @@ dependencies = [ "resvg", "tempfile", "tokio", + "toml 1.1.3+spec-1.1.0", "ui", "winit", + "winresource", ] [[package]] @@ -7962,6 +7964,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "winresource" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bc6d36480b829272a5bdabf08e6dca8004761c1da7e35efef800540c66221b" +dependencies = [ + "version_check", +] + [[package]] name = "wit-bindgen" version = "0.57.1" diff --git a/Cargo.toml b/Cargo.toml index 43d44fa6..94d63cc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,6 +99,7 @@ unicode-segmentation = "1.13.3" url = "2.5.8" uuid = { version = "1.27.0", features = ["v4"] } winit = { version = "0.30.13", default-features = false, features = ["x11"] } +winresource = { version = "0.1.34", default-features = false } winreg = "0.56.0" yaml-rust2 = "0.13.0" zbus = "5.19.0" diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index d0c9cd1a..cb35eba6 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -49,7 +49,9 @@ summary = "A developer's notes and snippets manager for the desktop" assets = [ { source = "target/release/devnotes", dest = "/usr/bin/devnotes", mode = "755" }, { source = "packaging/devnotes.desktop", dest = "/usr/share/applications/devnotes.desktop", mode = "644" }, + { source = "icons/32x32.png", dest = "/usr/share/icons/hicolor/32x32/apps/devnotes.png", mode = "644" }, { source = "icons/128x128.png", dest = "/usr/share/icons/hicolor/128x128/apps/devnotes.png", mode = "644" }, + { source = "icons/icon.png", dest = "/usr/share/icons/hicolor/512x512/apps/devnotes.png", mode = "644" }, ] [package.metadata.generate-rpm.requires] @@ -82,6 +84,10 @@ puffin_http = { workspace = true, optional = true } # The one eframe uses, for its X11 switch (D15) and for macOS's menu bar. winit.workspace = true +[build-dependencies] +toml.workspace = true +winresource.workspace = true + [dev-dependencies] # `examples/tours.rs`, which assembles the README's tours, and their test. image.workspace = true diff --git a/crates/app/build.rs b/crates/app/build.rs new file mode 100644 index 00000000..8a4af3e7 --- /dev/null +++ b/crates/app/build.rs @@ -0,0 +1,39 @@ +//! The Windows executable carries its icon and its version: the shell, the installer's shortcuts +//! and the taskbar read them from the file, whatever the window sets. + +use std::{env, fs, path::Path}; + +fn main() { + println!("cargo:rerun-if-changed=Cargo.toml"); + println!("cargo:rerun-if-changed=icons/icon.ico"); + // The script runs on the host: the target says what is being built. + if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") { + return; + } + + let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set by cargo"); + let manifest_dir = Path::new(&manifest_dir); + let name = product_name(manifest_dir); + let icon = manifest_dir.join("icons/icon.ico"); + winresource::WindowsResource::new() + .set_icon(icon.to_str().expect("the repository is at a path of text")) + .set("ProductName", &name) + .set("FileDescription", &name) + .compile() + .expect("the Windows SDK's resource compiler embeds the icon"); +} + +/// The name the packages give the application, `[package.metadata.packager] product-name`; the +/// version is the workspace's, which `winresource` reads from Cargo itself. +fn product_name(manifest_dir: &Path) -> String { + let text = fs::read_to_string(manifest_dir.join("Cargo.toml")).expect("Cargo.toml is readable"); + let manifest: toml::Table = toml::from_str(&text).expect("Cargo.toml is valid TOML"); + manifest + .get("package") + .and_then(|package| package.get("metadata")) + .and_then(|metadata| metadata.get("packager")) + .and_then(|packager| packager.get("product-name")) + .and_then(toml::Value::as_str) + .expect("[package.metadata.packager] product-name is a string") + .to_owned() +} diff --git a/crates/app/tests/windows_resources.rs b/crates/app/tests/windows_resources.rs new file mode 100644 index 00000000..5f9d37b2 --- /dev/null +++ b/crates/app/tests/windows_resources.rs @@ -0,0 +1,48 @@ +//! What Windows reads from the executable itself: the shell, the installer's shortcuts and the +//! taskbar take the icon from the file, not from the window. +#![cfg(windows)] + +const RT_GROUP_ICON: u32 = 14; +const RT_VERSION: u32 = 16; + +#[test] +fn the_executable_carries_its_icon_and_its_version() { + let file = std::fs::read(env!("CARGO_BIN_EXE_devnotes")).unwrap(); + let types = resource_types(&file); + + assert!(types.contains(&RT_GROUP_ICON), "no icon: {types:?}"); + assert!(types.contains(&RT_VERSION), "no version: {types:?}"); +} + +/// The types listed by the file's root resource directory, read from its PE headers; none when it +/// has no resource table. +fn resource_types(file: &[u8]) -> Vec { + let u16_at = |at: usize| usize::from(u16::from_le_bytes([file[at], file[at + 1]])); + let u32_at = |at: usize| u32::from_le_bytes(file[at..at + 4].try_into().unwrap()); + + let pe = u32_at(0x3C) as usize; + let optional = pe + 24; + let directories = optional + if u16_at(optional) == 0x20B { 112 } else { 96 }; + let table = u32_at(directories + 2 * 8); + if table == 0 { + return Vec::new(); + } + + let headers = optional + u16_at(pe + 20); + let root = (0..u16_at(pe + 6)) + .map(|index| headers + 40 * index) + .find_map(|section| { + let start = u32_at(section + 12); + let length = u32_at(section + 8).max(u32_at(section + 16)); + (start..start + length) + .contains(&table) + .then(|| (table - start + u32_at(section + 20)) as usize) + }) + .expect("the resource table lies in a section"); + + let named = u16_at(root + 12); + let numbered = u16_at(root + 14); + (named..named + numbered) + .map(|entry| u32_at(root + 16 + 8 * entry)) + .collect() +} diff --git a/docs/architecture.md b/docs/architecture.md index 6acd69f9..073f90e8 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -6255,8 +6255,10 @@ three invariants `changelog::tests::the_shipped_file_parses_into_something_to_sh **The packages** are built by `cargo build -p app --release`, then `.github/actions/package`: `cargo packager` (NSIS per user on Windows; AppImage and deb on Linux; a `.dmg` and the updater's archive on macOS) and `cargo generate-rpm`, -each signed with the minisign key, each checked to hold the application alone (→ -`docs/releasing.md`, "What gets published"). +each signed with the minisign key, each checked to hold the application alone and to carry the +icon (→ `docs/releasing.md`, "What gets published"). On Windows the icon and the version are +resources of `devnotes.exe`, embedded by `crates/app/build.rs` (`winresource`): the shell, the +installer's shortcuts and the taskbar read the file, whatever the window sets. `packages.yml` runs the same action on a pull request that touches packaging, with a key made for the run, then installs 1.0.0 on a clean runner, refuses a 1.0.1 altered by one byte and installs the whole one through the application's own updater client diff --git a/docs/releasing.md b/docs/releasing.md index 6c0fcdae..5eb7d153 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -134,7 +134,10 @@ more; the Linux ones depend on GTK (the tray alone), xkbcommon, Vulkan and the i library. Each package is checked to hold the application alone (one binary; the identity tool's lists are -compiled into it) before anything is uploaded. +compiled into it) before anything is uploaded, and to carry the icon: the hicolor icons and the +`.desktop` entry on Linux, the `.icns` of the bundle and of the updater's archive on macOS. On +Windows the icon is a resource of `devnotes.exe` itself, held by +`crates/app/tests/windows_resources.rs`. **Signatures.** `cargo-packager` signs every package it writes with the minisign key, and the `.rpm` is signed by `cargo packager signer sign`. The key and its password are the release