Constructor Pattern (RULE ZERO). Zero behaviour change, zero flag
drift — all original CLI flags preserved verbatim.
Before: install.sh — 1238 LOC monolith
After: install.sh — 138 LOC dispatcher (sources libs in order)
install/lib-*.sh — 16 cubes, max 183 LOC (lib-menu)
Cubes:
lib-log 21 LOC — logging primitives
lib-backup 63 LOC — rollback trap + BACKUP_PAIRS
lib-profile 115 LOC — MANIFEST.toml profile resolution
lib-args 92 LOC — CLI parsing + --help heredoc
lib-menu 183 LOC — whiptail/dialog/plain-text interactive picker
lib-plan 150 LOC — dry-run --no-execute output
lib-prereqs 91 LOC — hard + soft dependency checks
lib-primitives 131 LOC — primitive copy + MANIFEST drive
lib-rust 114 LOC — cargo workspace build + pre-built support
lib-scaffold 144 LOC — agent/skill/block scaffolding
lib-bridges 31 LOC — project-bridge install
lib-hooks 104 LOC — settings.json jq merge
lib-agents 77 LOC — assembled agent output
lib-skills 23 LOC — skill copy
lib-wizard 20 LOC — sleep-setup wizard invocation
lib-summary 59 LOC — post-install summary
Invariants preserved:
- macOS bash 3.2 compat (no associative arrays, no [[ ]], no ${,,})
- rollback trap wired via setup_backup_trap early in dispatcher
- jq-merge behaviour verbatim in lib-hooks
- scoped Cargo.toml regeneration in lib-rust
Function LOC limits: largest non-heredoc fn 22 LOC (check_soft_prereqs).
Three functions kept >30 LOC because heredoc-dominated (print_help,
print_summary, profile_members); splitting would fragment logical unit.
62 unique function names across cubes, zero duplicates (grep-verified).
bash -n passes on all 17 files. Runtime smoke test deferred to user's
shell (bash-readonly sandbox constraint).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
63 lines
1.7 KiB
Bash
63 lines
1.7 KiB
Bash
# shellcheck shell=bash
|
|
# lib-backup.sh — rollback trap + backup_dir / backup_file helpers.
|
|
#
|
|
# Every successful backup_dir / per-file backup appends a "ORIGINAL|BACKUP"
|
|
# pair to BACKUP_PAIRS. On ERR the trap walks the list in reverse and
|
|
# atomically swaps BACKUP back onto ORIGINAL. A boolean guard makes
|
|
# rollback idempotent.
|
|
#
|
|
# Requires: say / warn / err from lib-log.sh.
|
|
# Sourced by install.sh; no top-level execution except global var init and
|
|
# `trap rollback ERR` inside setup_backup_trap.
|
|
|
|
BACKUP_PAIRS=()
|
|
ROLLED_BACK=0
|
|
|
|
rollback() {
|
|
[ "$ROLLED_BACK" = "1" ] && return 0
|
|
ROLLED_BACK=1
|
|
if [ "${#BACKUP_PAIRS[@]}" -eq 0 ]; then
|
|
err "install failed at line ${BASH_LINENO[0]:-?}; no backups to restore"
|
|
return 0
|
|
fi
|
|
warn "install failed — rolling back ${#BACKUP_PAIRS[@]} backup(s)"
|
|
local i pair orig bak
|
|
for (( i=${#BACKUP_PAIRS[@]}-1; i>=0; i-- )); do
|
|
pair="${BACKUP_PAIRS[$i]}"
|
|
orig="${pair%%|*}"
|
|
bak="${pair#*|}"
|
|
if [ -e "$bak" ]; then
|
|
if [ -d "$orig" ] || [ -f "$orig" ]; then
|
|
rm -rf "$orig"
|
|
fi
|
|
mv "$bak" "$orig"
|
|
say " restored $orig from $bak"
|
|
fi
|
|
done
|
|
err "install failed at line ${BASH_LINENO[0]:-?}; rolled back"
|
|
}
|
|
|
|
setup_backup_trap() {
|
|
trap rollback ERR
|
|
}
|
|
|
|
backup_dir() {
|
|
local target="$1"
|
|
[ -d "$target" ] || return 0
|
|
if [ -z "$(find "$target" -type f -print -quit 2>/dev/null)" ]; then
|
|
return 0
|
|
fi
|
|
local backup="${target}.bak-$(date +%s)"
|
|
cp -a "$target" "$backup"
|
|
BACKUP_PAIRS+=("$target|$backup")
|
|
say "backed up existing $target to $backup"
|
|
}
|
|
|
|
backup_file() {
|
|
local target="$1"
|
|
[ -f "$target" ] || return 0
|
|
local backup="${target}.bak-$(date +%s)"
|
|
mv "$target" "$backup"
|
|
BACKUP_PAIRS+=("$target|$backup")
|
|
say "backed up existing $target to $backup"
|
|
}
|