#!/usr/bin/env bash # Shared helpers for the hoxay stable component (macOS). # # Sourced by install.sh / update.sh / verify.sh / launch.sh / rollback.sh. # The component manages exactly one product at a time; the managed artifact # keeps the name the build produced (hoxay.app for v0.8.0 and later, # herdr-t3.app for v0.7.0 and earlier), so a pre-rename and a post-rename # build can coexist in ~/Applications while the `current_app` pointer selects # the active one. # Upgrade identity. The bundle id is not the product name: it stays with the # value the herdr-t3 era used so existing installs upgrade in place # (hoxay ADR-0038, rebuildup/hoxay docs/adr/ADR-0038.md). BUNDLE_ID="com.github.rebuildup.herdr-t3" LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister" log() { printf '[hoxay] %s\n' "$*"; } die() { log "ERROR: $*" >&2; exit 1; } # The managed state dir was renamed together with the product (pc-setup # ADR-0030). An explicit override always wins; the legacy variable stays # readable so an existing machine keeps working without editing its shell. state_dir() { if [[ -n "${HOXAY_STATE_DIR:-}" ]]; then printf '%s' "$HOXAY_STATE_DIR" else printf '%s' "${XDG_DATA_HOME:-$HOME/.local/share}/hoxay" fi } legacy_state_dir() { printf '%s' "${HERDR_T3_STATE_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/herdr-t3}" } apps_dir() { if [[ -n "${HOXAY_APPS_DIR:-}" ]]; then printf '%s' "$HOXAY_APPS_DIR" elif [[ -n "${HERDR_T3_APPS_DIR:-}" ]]; then printf '%s' "$HERDR_T3_APPS_DIR" else printf '%s' "$HOME/Applications" fi } # One-time rename of the managed state dir. The release clones and the # current/previous pointers move with it, so rolling back to a pre-rename # release still works without a rebuild. migrate_legacy_state_dir() { if [[ -n "${HOXAY_STATE_DIR:-}" ]]; then return 0 fi local legacy current legacy="$(legacy_state_dir)" current="$(state_dir)" if [[ "$legacy" == "$current" ]]; then return 0 fi if [[ -e "$current" ]]; then return 0 fi if [[ ! -e "$legacy" ]]; then return 0 fi mv "$legacy" "$current" || die "cannot move legacy state dir $legacy -> $current" # `current_app` holds an absolute path into the moved directory — rewrite # it, or the pointer keeps addressing the old location. local app_file="$current/current_app" if [[ -f "$app_file" ]]; then local app app="$(cat "$app_file")" if [[ "$app" == "$legacy"* ]]; then printf '%s\n' "$current${app#"$legacy"}" > "$app_file" fi fi log "state dir renamed: $legacy -> $current" } # Print the CFBundleIdentifier of an app bundle (empty when unreadable). bundle_id_of() { # $1 = .app path /usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "$1/Contents/Info.plist" 2>/dev/null || true } # Print the CFBundleExecutable of an app bundle (empty when unreadable). bundle_executable_of() { # $1 = .app path /usr/libexec/PlistBuddy -c "Print CFBundleExecutable" "$1/Contents/Info.plist" 2>/dev/null || true } # Print the path of the app's executable inside the bundle (empty when the # bundle carries no readable executable declaration). app_executable_path() { # $1 = .app path local exe exe="$(bundle_executable_of "$1")" if [[ -z "$exe" ]]; then return 0 fi printf '%s/Contents/MacOS/%s' "$1" "$exe" } # Print the built app of a release checkout (empty when the release has no # build output). electron-builder writes `release//.app`. built_app_of() { # $1 = release checkout dir find "$1/release" -maxdepth 2 -name '*.app' -type d 2>/dev/null | head -1 } # Print every app in the apps dir that carries the managed bundle id. apps_with_bundle_id() { # $1 = apps dir local app id for app in "$1"/*.app; do if [[ ! -d "$app" ]]; then continue fi id="$(bundle_id_of "$app")" if [[ "$id" == "$BUNDLE_ID" ]]; then printf '%s\n' "$app" fi done } # Register the active app with Launch Services and unregister every other # app that carries the same bundle id, so the bundle resolves to exactly one # app: the managed one. The unregistered app stays on disk as the rollback # artifact. register_active_app() { # $1 = active .app path local other for other in "$APPS_DIR"/*.app; do if [[ ! -d "$other" ]]; then continue fi if [[ "$other" == "$1" ]]; then continue fi if [[ "$(bundle_id_of "$other")" != "$BUNDLE_ID" ]]; then continue fi if [[ -x "$LSREGISTER" ]]; then "$LSREGISTER" -u "$other" >/dev/null 2>&1 || true fi log "legacy app kept on disk but unregistered: $other" done if [[ -x "$LSREGISTER" ]]; then "$LSREGISTER" -f "$1" >/dev/null 2>&1 || true fi } # Quit a running app before its bundle is replaced. The app owns no herdr # session, pane or agent: those keep running inside herdr. quit_running_app() { # $1 = .app path local exe_path exe_path="$(app_executable_path "$1")" if [[ -z "$exe_path" ]]; then return 0 fi if ! pgrep -f "$exe_path" >/dev/null 2>&1; then return 0 fi log "quitting running $(basename "$1") before replacing it" osascript -e "tell application id \"$BUNDLE_ID\" to quit" >/dev/null 2>&1 || true local _ for _ in $(seq 1 25); do if ! pgrep -f "$exe_path" >/dev/null 2>&1; then return 0 fi sleep 0.2 done # Graceful quit failed; SIGTERM is safe for the same reason. pkill -f "$exe_path" || true for _ in $(seq 1 25); do if ! pgrep -f "$exe_path" >/dev/null 2>&1; then return 0 fi sleep 0.2 done die "$(basename "$1") is still running; quit it and retry" } # Quit every installed app that carries the managed bundle id, so switching # between a pre-rename and a post-rename build never leaves two instances # of the same app running. quit_running_apps() { local app while IFS= read -r app; do if [[ -n "$app" ]]; then quit_running_app "$app" fi done <