KeiSeiKit-1.0/install.sh
Parfii-bot 03d1dc7362 refactor(v0.16): split install.sh monolith (1238 LOC) into 17 cubes
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>
2026-04-22 15:09:35 +08:00

138 lines
5 KiB
Bash
Executable file

#!/usr/bin/env bash
# KeiSeiKit — Constructor-Pattern Agent Kit installer
# Idempotent: safe to re-run. Never overwrites settings.json or existing user manifests.
#
# Usage:
# ./install.sh # interactive menu on TTY; profile=minimal on non-TTY
# ./install.sh --profile=<name> # minimal|core|frontend|ops|dev|mcp|full (skips menu)
# ./install.sh --add=<name>[,<name>] # install one or more primitives on top of current state
# ./install.sh --remove=<name> # remove a single primitive
# ./install.sh --list # list installed primitives (name | kind | desc | path)
# ./install.sh --with-bridges # also render cross-tool bridges into $PWD
# ./install.sh --activate-hooks # jq-merge settings-snippet.json into ~/.claude/settings.json
# ./install.sh --yes # skip confirm screen after menu (automation)
# ./install.sh --no-execute # parse menu+confirm, print plan, exit (testing)
#
# Internals: this file is a thin orchestrator. All implementation lives in
# install/lib-*.sh cubes (Constructor Pattern: 1 file = 1 concern, <200 LOC).
set -euo pipefail
# --- paths ----------------------------------------------------------------
KIT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOME_DIR="${HOME:?HOME not set}"
AGENTS_DIR="$HOME_DIR/.claude/agents"
HOOKS_DIR="$HOME_DIR/.claude/hooks"
SKILLS_DIR="$HOME_DIR/.claude/skills"
MANIFEST="$KIT_DIR/_primitives/MANIFEST.toml"
INSTALLED_FILE="$AGENTS_DIR/_primitives/.installed"
LIB_DIR="$KIT_DIR/install"
# --- source cubes (order matters: logs -> backup -> profile -> rest) ------
# shellcheck source=install/lib-log.sh
source "$LIB_DIR/lib-log.sh"
# shellcheck source=install/lib-backup.sh
source "$LIB_DIR/lib-backup.sh"
# shellcheck source=install/lib-profile.sh
source "$LIB_DIR/lib-profile.sh"
# shellcheck source=install/lib-args.sh
source "$LIB_DIR/lib-args.sh"
# shellcheck source=install/lib-menu.sh
source "$LIB_DIR/lib-menu.sh"
# shellcheck source=install/lib-plan.sh
source "$LIB_DIR/lib-plan.sh"
# shellcheck source=install/lib-prereqs.sh
source "$LIB_DIR/lib-prereqs.sh"
# shellcheck source=install/lib-primitives.sh
source "$LIB_DIR/lib-primitives.sh"
# shellcheck source=install/lib-rust.sh
source "$LIB_DIR/lib-rust.sh"
# shellcheck source=install/lib-scaffold.sh
source "$LIB_DIR/lib-scaffold.sh"
# shellcheck source=install/lib-bridges.sh
source "$LIB_DIR/lib-bridges.sh"
# shellcheck source=install/lib-hooks.sh
source "$LIB_DIR/lib-hooks.sh"
# shellcheck source=install/lib-agents.sh
source "$LIB_DIR/lib-agents.sh"
# shellcheck source=install/lib-skills.sh
source "$LIB_DIR/lib-skills.sh"
# shellcheck source=install/lib-wizard.sh
source "$LIB_DIR/lib-wizard.sh"
# shellcheck source=install/lib-summary.sh
source "$LIB_DIR/lib-summary.sh"
# --- parse flags + install rollback trap ---------------------------------
parse_args "$@"
setup_backup_trap
# --- --list short-circuit -------------------------------------------------
if [ "$LIST_MODE" = "1" ]; then
[ -f "$MANIFEST" ] || { err "MANIFEST.toml missing: $MANIFEST"; exit 2; }
cmd_list
exit 0
fi
# --- incremental --add / --remove short-circuit --------------------------
if [ -n "$ADD_LIST" ] || [ -n "$REMOVE_NAME" ]; then
run_incremental_change
exit 0
fi
# --- interactive menu (option C hybrid) ----------------------------------
# Runs ONLY when: no selection flag passed AND stdin+stdout are TTY AND
# --list / --add / --remove short-circuits above did NOT fire.
run_menu_if_needed || exit 1
# --- resolve profile (default=minimal) -----------------------------------
PROFILE="${PROFILE:-minimal}"
case "$PROFILE" in
minimal|core|frontend|ops|dev|mcp|full|custom) ;;
*)
err "unknown profile: $PROFILE. Valid: minimal | core | frontend | ops | dev | mcp | full"
exit 1
;;
esac
say "profile: $PROFILE"
# --- prerequisites -------------------------------------------------------
check_prereqs
# --- confirm screen + --no-execute ---------------------------------------
CONFIRM_LABEL="$PROFILE"
[ "$PROFILE" = "custom" ] && CONFIRM_LABEL="custom ($CUSTOM_PRIMS)"
CONFIRM_INPUT="$(printf '%s\n' $PROFILE_PRIMS | grep -v '^$' || true)"
if ! printf '%s\n' "$CONFIRM_INPUT" | show_confirm_screen "$CONFIRM_LABEL"; then
say "install declined at confirm screen — aborting"
exit 1
fi
if [ "$NO_EXECUTE" = "1" ]; then
say "--no-execute: plan resolved, exiting before install"
exit 0
fi
# --- execute install phases ----------------------------------------------
setup_target_dirs
scaffold_memory_index
install_blocks
run_primitives_phase
install_bridges
install_manifests
build_assembler
generate_agents
install_hooks
install_skills
maybe_activate_hooks
# Bail out cleanly if the rollback trap already fired (activate_hooks err path).
if [ "${ROLLED_BACK:-0}" = "1" ]; then
exit 2
fi
# --- optional post-install hooks ------------------------------------------
[ "$WITH_BRIDGES" = "1" ] && render_bridges
[ "$WITH_SLEEP_SYNC" = "1" ] && run_sleep_wizard
# --- final summary --------------------------------------------------------
print_summary