diff --git a/migrations/1791470456.sh b/migrations/1791470456.sh new file mode 100644 index 00000000..5213458c --- /dev/null +++ b/migrations/1791470456.sh @@ -0,0 +1,64 @@ +echo "Repair background selections after bundled images moved to WebP" + +theme_webp_source() { + local theme="$1" + local background="$2" + local original_name=${background##*/} + local replacement_name="${original_name%.*}.webp" + local user_theme_backgrounds="$HOME/.config/omarchy/themes/$theme/backgrounds" + local bundled_theme_backgrounds="$OMARCHY_PATH/themes/$theme/backgrounds" + + if [[ -f $user_theme_backgrounds/$original_name || -f $bundled_theme_backgrounds/$original_name ]]; then + return 1 + elif [[ -f $user_theme_backgrounds/$replacement_name ]]; then + printf '%s\n' "$user_theme_backgrounds/$replacement_name" + elif [[ -f $bundled_theme_backgrounds/$replacement_name ]]; then + printf '%s\n' "$bundled_theme_backgrounds/$replacement_name" + else + return 1 + fi +} + +replacement_webp() { + local background="$1" + local theme="${2:-}" + local replacement + + case ${background,,} in + *.jpg | *.jpeg | *.png) replacement="${background%.*}.webp" ;; + *) return 1 ;; + esac + + if [[ -n $theme && ${background%/*} == "$HOME/.local/state/omarchy/current/theme/backgrounds" ]]; then + theme_webp_source "$theme" "$background" >/dev/null || return 1 + printf '%s\n' "$replacement" + return 0 + fi + + [[ ! -e $background && -f $replacement ]] || return 1 + printf '%s\n' "$replacement" +} + +current_dir="$HOME/.local/state/omarchy/current" +current_background="$current_dir/background" +if [[ -L $current_background ]]; then + active_theme=$(cat "$current_dir/theme.name" 2>/dev/null || true) + background=$(readlink "$current_background") + if replacement=$(replacement_webp "$background" "$active_theme"); then + if [[ ! -f $replacement ]]; then + source=$(theme_webp_source "$active_theme" "$background") + cp "$source" "$replacement" + fi + ln -sfn "$replacement" "$current_background" + omarchy-shell -q background set "$replacement" + fi +fi + +for state_file in "$HOME/.local/state/omarchy/theme-backgrounds/"*; do + [[ -f $state_file ]] || continue + + background=$(<"$state_file") + if replacement=$(replacement_webp "$background" "${state_file##*/}"); then + printf '%s\n' "$replacement" >"$state_file" + fi +done diff --git a/test/shell.d/webp-background-migration-test.sh b/test/shell.d/webp-background-migration-test.sh new file mode 100644 index 00000000..1d98eba3 --- /dev/null +++ b/test/shell.d/webp-background-migration-test.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +test_dir=$(mktemp -d) +trap 'rm -rf "$test_dir"' EXIT +home="$test_dir/home" +migration="$ROOT/migrations/1791470456.sh" +mkdir -p "$test_dir/bin" +call_log="$test_dir/shell-calls" +cat >"$test_dir/bin/omarchy-shell" <<'SH' +#!/bin/bash +printf '%s\n' "$*" >>"$CALL_LOG" +SH +chmod +x "$test_dir/bin/omarchy-shell" + +reset_home() { + rm -rf "$home" + mkdir -p "$home/.local/state/omarchy/current/theme/backgrounds" "$home/.local/state/omarchy/theme-backgrounds" + : >"$call_log" +} + +run_migration() { + HOME="$home" OMARCHY_PATH="$ROOT" CALL_LOG="$call_log" PATH="$test_dir/bin:$PATH" \ + bash -euo pipefail "$migration" >"$test_dir/output" +} + +backgrounds="$home/.local/state/omarchy/current/theme/backgrounds" +current="$home/.local/state/omarchy/current/background" +remembered="$home/.local/state/omarchy/theme-backgrounds/tokyo-night" +remembered_png="$home/.local/state/omarchy/theme-backgrounds/catppuccin" + +reset_home +touch "$backgrounds/current.webp" +ln -s "$backgrounds/current.jpg" "$current" +printf '%s\n' "$backgrounds/2-swirl-buck.jpg" >"$remembered" +printf '%s\n' "$backgrounds/omarchy.png" >"$remembered_png" +run_migration +[[ $(readlink "$current") == "$backgrounds/current.webp" ]] || fail "the current background moves from the removed JPEG to its WebP replacement" +[[ $(<"$call_log") == "-q background set $backgrounds/current.webp" ]] || fail "the running shell is told about the repaired current background" +[[ $(<"$remembered") == "$backgrounds/2-swirl-buck.webp" ]] || fail "an inactive theme's remembered JPEG moves to its bundled WebP replacement" +[[ $(<"$remembered_png") == "$backgrounds/omarchy.webp" ]] || fail "an inactive theme's remembered PNG moves to its bundled WebP replacement" +pass "converted bundled backgrounds keep their current and remembered selections" + +run_migration +[[ $(readlink "$current") == "$backgrounds/current.webp" ]] || fail "the repair can be rerun" +[[ $(<"$remembered") == "$backgrounds/2-swirl-buck.webp" ]] || fail "the remembered JPEG repair can be rerun" +[[ $(<"$remembered_png") == "$backgrounds/omarchy.webp" ]] || fail "the remembered PNG repair can be rerun" +pass "the background repair is idempotent" + +reset_home +touch "$backgrounds/kept.jpg" "$backgrounds/kept.webp" +ln -s "$backgrounds/kept.jpg" "$current" +printf '%s\n' "$backgrounds/kept.jpg" >"$remembered" +run_migration +[[ $(readlink "$current") == "$backgrounds/kept.jpg" ]] || fail "an existing JPEG background is preserved" +[[ $(<"$remembered") == "$backgrounds/kept.jpg" ]] || fail "an existing remembered JPEG is preserved" +pass "existing backgrounds are not replaced just because a WebP shares their stem" + +reset_home +ln -s "$backgrounds/missing.jpeg" "$current" +printf '%s\n' "$backgrounds/missing.png" >"$remembered" +run_migration +[[ $(readlink "$current") == "$backgrounds/missing.jpeg" ]] || fail "a dangling selection without an exact WebP replacement is preserved" +[[ $(<"$remembered") == "$backgrounds/missing.png" ]] || fail "a dangling remembered selection without an exact WebP replacement is preserved" +pass "unrelated dangling background paths are left alone" + +reset_home +mkdir -p "$home/.config/omarchy/themes/custom/backgrounds" +touch "$backgrounds/cover.webp" "$home/.config/omarchy/themes/custom/backgrounds/cover.jpg" +custom_remembered="$home/.local/state/omarchy/theme-backgrounds/custom" +printf '%s\n' "$backgrounds/cover.jpg" >"$custom_remembered" +run_migration +[[ $(<"$custom_remembered") == "$backgrounds/cover.jpg" ]] || fail "an inactive custom theme's existing JPEG is not confused with the active theme's WebP" +pass "remembered selections are checked against their named theme" + +reset_home +touch "$backgrounds/2-swirl-buck.jpg" +printf '%s\n' "$backgrounds/2-swirl-buck.jpg" >"$remembered" +run_migration +[[ $(<"$remembered") == "$backgrounds/2-swirl-buck.webp" ]] || fail "an active-theme filename collision does not hide an inactive theme's WebP replacement" +pass "active theme files do not control inactive remembered repairs" + +reset_home +printf 'tokyo-night\n' >"$home/.local/state/omarchy/current/theme.name" +touch "$backgrounds/2-swirl-buck.jpg" +ln -s "$backgrounds/2-swirl-buck.jpg" "$current" +run_migration +[[ -f $backgrounds/2-swirl-buck.webp ]] || fail "the active staged theme receives its bundled WebP replacement" +[[ $(readlink "$current") == "$backgrounds/2-swirl-buck.webp" ]] || fail "a selected staged JPEG moves before a later refresh can strand it" +pass "the active staged theme is repaired before its old bundled image disappears"