Files
My-Brain-Is-Full-Crew/scripts/lib.sh
Giacomo Nunziati 53b605379e feat: multi-platform adapter architecture (Claude Code, Gemini CLI, OpenCode) (#32)
* Fix istall/update scripts

* test: capture pre-refactor install snapshot for regression

Adds take-snapshot.sh script and the resulting snapshot/ directory,
capturing the exact vault state produced by launchme.sh before the
framework-agnosticity refactor begins.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Summary: Refactor agents/skills/hooks/mcp in agentic-platform-agnostic templates.

refactor: rename source CLAUDE.md → DISPATCHER.md (framework-neutral)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

refactor: convert agent frontmatter from tools: to neutral capabilities:

Replace Claude Code-specific `tools:` frontmatter with framework-agnostic
`mode: subagent` and `capabilities: [...]` in all 8 agent files.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

refactor: add neutral hook trigger manifests (.hook.yaml)

refactor: hooks read neutral JSON schema (args.* instead of tool_input.*)

refactor: convert .mcp.json to neutral mcp/servers.yaml

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Implement agentic-platform adapters skeleton.

build: add adapters/lib.sh skeleton with vocabulary constants

test: bash test runner for adapter helpers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(adapters): parse_frontmatter helper with tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(adapters): parse_capabilities helper with tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(adapters): should_include helper with tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(adapters): parse_hook_yaml helper with tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(adapters): agent_body helper with tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(adapters): enumerate_agents and enumerate_hooks helpers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Implement agentic-platform adapter for Claude Code.

build(adapters): claude-code adapter skeleton with capability/event tables

build(claude-code): adapter_translate_dispatcher with test

build(claude-code): adapter_translate_references with test

build(claude-code): adapter_translate_skills with tests

build(claude-code): adapter_translate_agents with capability→tools mapping

build(claude-code): hook wrapper template (CC native → neutral schema)

build(claude-code): adapter_translate_hooks with wrapper generation

build(claude-code): adapter_translate_mcp with hand-rolled YAML parser

build(claude-code): adapter_finalize and complete adapter_build wiring

build: scripts/build.sh dispatches to per-framework adapter

Also fix adapter_translate_hooks and adapter_translate_agents to use
while-read loops (avoiding word-splitting on paths with spaces) and
guard grep calls with || true to survive set -eo pipefail when hooks
have no match-tool field. Remove scripts/build.sh from .gitignore.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Refactor install/update scripts to support agentic-platform agnosticity.

refactor(lib.sh): generalize install_claude_md → install_dispatcher

New signature takes the full destination path instead of just the vault
dir, allowing callers to install CLAUDE.md, AGENTS.md, or any dispatcher
file to an explicit location.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat(launchme): support --framework flag, build dist/ before install

Add --framework and --target arg parsing. Run build.sh before installing
to populate dist/<framework>/. All install_* calls now read from
dist/<framework>/ instead of the raw source dirs. MCP is now handled
automatically by the adapter (no interactive prompt). Replaced
install_claude_md with install_dispatcher.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat(updateme): support --framework flag, build dist/ before update

Add --framework and --target arg parsing. Run build.sh before installing
to populate dist/<framework>/. All install_* calls now read from
dist/<framework>/ instead of raw source dirs. Replaced install_claude_md
with install_dispatcher.

Also fix set -e compatibility in lib.sh: add || true to all conditional
[[ ... ]] && info "..." logging lines so they don't abort the script
when VERBOSE_COPY=0.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: regression runner diffs dist/claude-code against pre-refactor snapshot

- Add tests/regression/run.sh that builds dist/claude-code and compares
  against snapshot, excluding runtime-only artifacts (.mbifc-manifest,
  .mcp.json, .claude-plugin/plugin.json)
- Fix adapters/lib.sh agent_body: preserve '---' section dividers in body
  (awk now only skips '---' while still inside frontmatter, fm < 2)
- Fix adapters/claude-code/adapter.sh: change 'read' capability to expand
  to only 'Read', appending 'Glob, Grep' at end of tools list to match
  snapshot ordering
- Update snapshot to reflect intentional refactor changes: hook JSON schema
  (.args.* instead of .tool_input.*), wrapper scripts, settings.json with
  wrapper paths, and consistent tool ordering for postman/sorter

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Implement opencode adapter.

Co-Authored-By: win0na <winnie@winneon.moe>

feat(lib.sh): add install_plugins helper for opencode JS plugins

build(adapters): opencode adapter skeleton with capability/event tables

build(opencode): adapter_translate_dispatcher (DISPATCHER.md → AGENTS.md)

build(opencode): adapter_translate_references and adapter_translate_skills

Implements Task 4 and Task 5:
- adapter_translate_references: Copies reference markdown files to .opencode/references/
- adapter_translate_skills: Copies skill SKILL.md files to .opencode/skills/<name>/ with exclude filtering

Both functions respect framework filtering via should_include().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(opencode): adapter_translate_agents with capability→permission mapping

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(opencode): bash-executor template for spawning hook scripts

build(opencode): plugin-stub template for mbifc-hooks.js

build(opencode): adapter_translate_hooks with JS plugin generation

Implements _oc_hook_registry_json and adapter_translate_hooks in the
opencode adapter. Copies hook scripts to .opencode/hooks/, generates a
single .opencode/plugins/mbifc-hooks.js by inlining bash-executor.js and
synthesising a hook registry from *.hook.yaml files. Uses python3 for
template substitution to safely handle multi-line JS content. Adds 3
unit tests (copies scripts, registry entries, noop when no hooks dir).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(opencode): adapter_translate_mcp with local/remote handling

build(opencode): adapter_finalize and complete adapter_build wiring

Add adapter_finalize placeholder and wire adapter_translate_mcp into
adapter_build; add end-to-end integration test (14/14 pass).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat(launchme): branch on --framework for opencode install layout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat(updateme): branch on --framework for opencode install layout

Mirror the same case "$FRAMEWORK" block from launchme.sh: framework-specific
DIST_COMPONENTS_DIR, VAULT_COMPONENTS_DIR, DISPATCHER_SRC/DST, MCP_SRC/DST,
HAS_PLUGINS; conditional install_plugins; conditional install_settings;
framework-aware vault-setup check; framework-neutral summary messages.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix adapters to follow the same template.

fix: restore adapter_build() contract, revert function renames

Both adapters now export adapter_build() and adapter_translate_*() as
the uniform public contract. scripts/build.sh sources one adapter and
calls adapter_build uniformly. Private helpers (_oc_*) and vocabulary
tables (cc_capability_to_tools, oc_capability_to_permission, etc.)
retain their prefixes. CC regression and OC unit tests all pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

fix(tests): restore test_oc_ prefix on adapter_build end-to-end test

* Fix agent format in opencode adapter

* refactor: rename --framework to --platform across all scripts and tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Modify generic name for model tiers

Co-Authored-By: win0na <winnie@winneon.moe>

refactor: neutral model vocabulary (low/mid/high) in source agents

feat(claude-code): cc_model_to_native() maps low/mid/high to haiku/sonnet/opus

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat(opencode): update oc_model_to_provider() for low/mid/high vocabulary

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add gemini-cli adapter

Co-Authored-By: win0na <winnie@winneon.moe>

build(gemini-cli): adapter skeleton with capability/event/model tables

build(gemini-cli): adapter_translate_dispatcher (DISPATCHER.md → GEMINI.md)

build(gemini-cli): adapter_translate_references and adapter_translate_skills

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(gemini-cli): adapter_translate_agents with capability→tools mapping

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(gemini-cli): adapter_translate_hooks with wrapper scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

feat(install): add gemini-cli platform to launchme.sh and updateme.sh

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Implement preserving config merge for opencode.

Co-Authored-By: win0na <winnie@winneon.moe>

feat(opencode): config-merge.sh with formatting-preserving JSON merge

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

build(opencode): source config-merge.sh from adapter

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat(install): use oc_config_merge for opencode.json instead of overwrite

Source config-merge.sh from install scripts for opencode platform so
user keys in opencode.json are preserved on reinstall and update.
Fix in-place merge by writing to a temp file before moving to output.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add mcp files to gitignore.

* Fix claude-specific references in agents, skills and references

* Fix: remove claude-specific reference from hooks.

build: add platform_dir and dispatcher_name to all hook wrapper/plugin templates

feat(hooks): platform-aware path checks using platform_dir and dispatcher_name from JSON input

test: update regression snapshot for platform-aware hook wrappers and scripts

* Added interactive platform choice in launchme, and platform auto-detection in updateme.

* Fix: remove claude-specific references from documentation

* Update documentation to reflect the new platform-agnostic architecture

* fix: address Copilot review feedback on PR #32

- tests/run.sh: check source return code, report failures
- tests/regression/run.sh: use mktemp + trap cleanup instead of fixed /tmp paths
- tests/regression/run.sh: include .mcp.json in regression comparison
- tests/regression/take-snapshot.sh: use --platform flag instead of stale scripted input
- adapters/opencode/templates/plugin-stub.js.tmpl: include stdout in hook block error message

* fix: address Copilot review round 2

- config-merge.sh: reword comment to only promise indentation preservation (not full formatting)
- take-snapshot.sh: copy required artifacts explicitly, optional ones with existence check
- adapters/lib.sh: document parse_hook_yaml single-trigger limitation

* fix: address Copilot review round 3

- adapters/opencode/adapter.sh: replace python3 template substitution with
  pure bash (while-read loop with case matching), removing python3 dependency
- adapters/lib.sh: should_include now falls back to plain YAML key read for
  files without frontmatter delimiters (fixes hook .yaml exclude: support)

* fix: address Copilot review round 4

- scripts/launchme.sh: fix double-dot in FW_DIR_NAME display (basename
  already includes the dot, e.g. ".claude")
- scripts/launchme.sh: replace undefined MCP_ANSWER with check on MCP_DST
  existence for summary banner

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 23:28:28 +02:00

450 lines
17 KiB
Bash
Executable File

#!/usr/bin/env bash
# =============================================================================
# My Brain Is Full - Crew :: Shared library
# Sourced by launchme.sh and updateme.sh — do NOT execute directly.
# =============================================================================
# ── Colors ────────────────────────────────────────────────────────────────────
if [[ -t 1 ]]; then
GREEN='\033[0;32m'; CYAN='\033[0;36m'; YELLOW='\033[1;33m'
RED='\033[0;31m'; BOLD='\033[1m'; DIM='\033[2m'; NC='\033[0m'
else
GREEN=''; CYAN=''; YELLOW=''; RED=''; BOLD=''; DIM=''; NC=''
fi
# ── Logging ───────────────────────────────────────────────────────────────────
# Everything is on stderr to keep stdout clean for machine-readable output in updateme.sh.
info() { echo -e " ${CYAN}>${NC} $*" >&2; }
success() { echo -e " ${GREEN}${NC} $*" >&2; }
warn() { echo -e " ${YELLOW}!${NC} $*" >&2; }
die() { echo -e "\n ${RED}Error: $*${NC}\n" >&2; exit 1; }
# ── Path resolution ───────────────────────────────────────────────────────────
# Sets SCRIPT_DIR, REPO_DIR, VAULT_DIR globals.
# Usage: resolve_paths "${BASH_SOURCE[0]}"
resolve_paths() {
SCRIPT_DIR="$(cd "$(dirname "$1")" && pwd)"
REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
VAULT_DIR="$(cd "$REPO_DIR/.." && pwd)"
[[ -d "$REPO_DIR/agents" ]] || die "Can't find agents/ in $REPO_DIR — are you running this from the repo?"
[[ -d "$REPO_DIR/references" ]] || die "Can't find references/ in $REPO_DIR"
}
# ── Change tracking ───────────────────────────────────────────────────────────
# Set by copy_if_changed and merge_marked_file after every call.
_LAST_CHANGED=0
# Controls per-file logging in install_* functions.
# Set VERBOSE_COPY=1 in the caller for per-file change output (used by updateme.sh).
VERBOSE_COPY=0
# ── copy_if_changed <src> <dst> ───────────────────────────────────────────────
# Copies src to dst only when they differ or dst doesn't exist.
# Sets _LAST_CHANGED=1 if a copy was made, 0 otherwise.
copy_if_changed() {
local src="$1" dst="$2"
_LAST_CHANGED=0
if ! diff -q "$src" "$dst" >/dev/null 2>&1; then
mkdir -p "$(dirname "$dst")"
cp "$src" "$dst"
_LAST_CHANGED=1
fi
}
# ── _insert_after_start_marker <dst> <content> ───────────────────────────────
# Inserts <content> immediately after the MBIFC:CUSTOM_AGENTS_START line in dst.
# dst must already exist and contain the marker.
_insert_after_start_marker() {
local dst="$1" content="$2"
[[ -z "$content" ]] && return 0
local start_line
start_line=$(grep -n '<!-- MBIFC:CUSTOM_AGENTS_START -->' "$dst" | head -1 | cut -d: -f1)
[[ -z "$start_line" ]] && return 0
local saved_file tmpfile
saved_file="$(mktemp)"
tmpfile="$(mktemp)"
# printf '%s\n' ensures a trailing newline so the next file line starts cleanly.
# bash $() strips trailing newlines, so content never has one already.
printf '%s\n' "$content" > "$saved_file"
{
head -n "$start_line" "$dst"
cat "$saved_file"
tail -n +"$((start_line + 1))" "$dst"
} > "$tmpfile"
mv "$tmpfile" "$dst"
rm -f "$saved_file"
}
# ── merge_marked_file <src> <dst> ─────────────────────────────────────────────
# Copies src to dst, preserving content between <!-- MBIFC:CUSTOM_AGENTS_START -->
# and <!-- MBIFC:CUSTOM_AGENTS_END --> markers from the existing dst.
#
# Handles three cases:
# 1. dst doesn't exist → plain copy
# 2. Both src and dst have markers → marker-based merge (primary path)
# 3. src has markers, dst doesn't → migration: extracts legacy custom rows
# from pre-marker installations
#
# Sets _LAST_CHANGED=1 if dst was written.
merge_marked_file() {
local src="$1" dst="$2"
_LAST_CHANGED=0
# Case 1: dst doesn't exist yet
if [[ ! -f "$dst" ]]; then
mkdir -p "$(dirname "$dst")"
cp "$src" "$dst"
_LAST_CHANGED=1
return 0
fi
local src_has_markers=0 dst_has_markers=0
grep -q '<!-- MBIFC:CUSTOM_AGENTS_START -->' "$src" 2>/dev/null && src_has_markers=1
grep -q '<!-- MBIFC:CUSTOM_AGENTS_START -->' "$dst" 2>/dev/null && dst_has_markers=1
# src has no markers → standard copy-if-changed
if [[ $src_has_markers -eq 0 ]]; then
copy_if_changed "$src" "$dst"
return 0
fi
# Case 3: src has markers, dst doesn't → migration from old format
if [[ $dst_has_markers -eq 0 ]]; then
local custom_rows=""
local CORE_NAMES="architect scribe sorter seeker connector librarian transcriber postman"
while IFS= read -r row; do
local aname
aname=$(printf '%s' "$row" | awk -F'|' '{gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2); print $2}')
if [[ -n "$aname" ]] && ! echo " $CORE_NAMES " | grep -qw " $aname "; then
custom_rows="${custom_rows}${row}"$'\n'
fi
done < <(grep "^|" "$dst" 2>/dev/null \
| grep -v "^|[[:space:]]*Name[[:space:]]*|" \
| grep -v "^|[-:[:space:]]*|")
cp "$src" "$dst"
_LAST_CHANGED=1
if [[ -n "$custom_rows" ]]; then
_insert_after_start_marker "$dst" "$custom_rows"
warn "Migrated legacy custom agents in $(basename "$dst") to MBIFC marker format"
fi
return 0
fi
# Case 2: both have markers → extract saved content, build merged result,
# only update dst if the result actually differs (ensures idempotency).
local saved_content
saved_content=$(awk \
'/<!-- MBIFC:CUSTOM_AGENTS_START -->/{f=1; next}
/<!-- MBIFC:CUSTOM_AGENTS_END -->/{f=0}
f{print}' \
"$dst")
local merged; merged="$(mktemp)"
cp "$src" "$merged"
[[ -n "$saved_content" ]] && _insert_after_start_marker "$merged" "$saved_content"
if ! diff -q "$merged" "$dst" >/dev/null 2>&1; then
cp "$merged" "$dst"
_LAST_CHANGED=1
[[ $VERBOSE_COPY -eq 1 && -n "$saved_content" ]] && info "Merged: $(basename "$src") (custom content preserved)"
fi
rm -f "$merged"
}
# ── Manifest helpers ──────────────────────────────────────────────────────────
# Single unified manifest at $VAULT_DIR/.{framework}/.mbifc-manifest
# Format: INI-style with [section] headers, one entry per line.
#
# PLATFORM_VAULT_DIR must be set before calling these functions (e.g.
# $VAULT_DIR/.claude for claude-code, $VAULT_DIR/.opencode for opencode).
# Defaults to $VAULT_DIR/.claude for backwards compatibility.
manifest_read() {
local section="$1"
local file="${PLATFORM_VAULT_DIR:-$VAULT_DIR/.claude}/.mbifc-manifest"
[[ -f "$file" ]] || return 0
awk -v sec="[$section]" '
$0 == sec { found=1; next }
found && /^\[[a-zA-Z0-9_-]+\]$/ { exit }
found && NF > 0 { print }
' "$file"
}
manifest_write() {
local section="$1"; shift
local entries=("$@")
local file="${PLATFORM_VAULT_DIR:-$VAULT_DIR/.claude}/.mbifc-manifest"
mkdir -p "$(dirname "$file")"
local tmpfile; tmpfile="$(mktemp)"
local in_section=0 section_written=0
if [[ -f "$file" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "[$section]" ]]; then
in_section=1; continue
fi
if [[ "$line" =~ ^\[[a-zA-Z0-9_-]+\]$ ]] && [[ $in_section -eq 1 ]]; then
in_section=0
# Write the replacement section, then the next section header
{ printf '[%s]\n' "$section"
for e in "${entries[@]}"; do [[ -n "$e" ]] && printf '%s\n' "$e"; done
printf '\n'
} >> "$tmpfile"
section_written=1
fi
[[ $in_section -eq 0 ]] && printf '%s\n' "$line" >> "$tmpfile"
done < "$file"
fi
# Section wasn't encountered, or was at end of file with no following section
if [[ $section_written -eq 0 ]]; then
{ printf '[%s]\n' "$section"
for e in "${entries[@]}"; do [[ -n "$e" ]] && printf '%s\n' "$e"; done
printf '\n'
} >> "$tmpfile"
fi
mv "$tmpfile" "$file"
}
manifest_remove() {
local section="$1" name="$2"
local file="${PLATFORM_VAULT_DIR:-$VAULT_DIR/.claude}/.mbifc-manifest"
[[ -f "$file" ]] || return 0
local tmpfile; tmpfile="$(mktemp)"
local in_section=0
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "[$section]" ]]; then in_section=1; printf '%s\n' "$line" >> "$tmpfile"; continue; fi
if [[ "$line" =~ ^\[[a-zA-Z0-9_-]+\]$ ]]; then in_section=0; fi
if [[ $in_section -eq 1 && "$line" == "$name" ]]; then continue; fi
printf '%s\n' "$line" >> "$tmpfile"
done < "$file"
mv "$tmpfile" "$file"
}
# Converts legacy per-directory .core-manifest files to the unified format.
# Runs only when legacy files are present; idempotent thereafter.
manifest_migrate() {
local _fw_dir="${PLATFORM_VAULT_DIR:-$VAULT_DIR/.claude}"
local agents_mf="$_fw_dir/agents/.core-manifest"
local refs_mf="$_fw_dir/references/.core-manifest"
[[ -f "$agents_mf" ]] || [[ -f "$refs_mf" ]] || return 0
warn "Migrating legacy manifests to unified .mbifc-manifest..."
if [[ -f "$agents_mf" ]]; then
local entries=()
while IFS= read -r line || [[ -n "$line" ]]; do [[ -n "$line" ]] && entries+=("$line"); done < "$agents_mf"
manifest_write "agents" "${entries[@]}"
rm "$agents_mf"
fi
if [[ -f "$refs_mf" ]]; then
local entries=()
while IFS= read -r line || [[ -n "$line" ]]; do [[ -n "$line" ]] && entries+=("$line"); done < "$refs_mf"
manifest_write "references" "${entries[@]}"
rm "$refs_mf"
fi
success "Manifest migrated to $(basename "$_fw_dir")/.mbifc-manifest"
}
# ── Deprecation ───────────────────────────────────────────────────────────────
# deprecate_removed <section> <src_dir> <dst_dir>
# Moves files listed in the manifest for <section> that no longer exist in
# <src_dir> to $PLATFORM_VAULT_DIR/deprecated/, prepending a DEPRECATED header.
# Prints the count of deprecated files to stdout.
deprecate_removed() {
local section="$1" src_dir="$2" dst_dir="$3"
local count=0
while IFS= read -r name; do
[[ -z "$name" ]] && continue
[[ -f "$src_dir/$name" ]] && continue # still in repo — keep it
local vault_file="$dst_dir/$name"
[[ -f "$vault_file" ]] || continue # not present — skip
[[ "$name" == *"-DEPRECATED"* ]] && continue # already deprecated
local dep_name="${name%.md}-DEPRECATED.md"
local dep_dir="${PLATFORM_VAULT_DIR:-$VAULT_DIR/.claude}/deprecated"
mkdir -p "$dep_dir"
[[ -f "$dep_dir/$dep_name" ]] && continue # already done in a prior run
mv "$vault_file" "$dep_dir/$dep_name"
{ printf '########\nDEPRECATED DO NOT USE\n########\n\n'
cat "$dep_dir/$dep_name"
} > "$dep_dir/$dep_name.tmp"
mv "$dep_dir/$dep_name.tmp" "$dep_dir/$dep_name"
manifest_remove "$section" "$name"
warn "Deprecated: $name$(basename "${PLATFORM_VAULT_DIR:-$VAULT_DIR/.claude}")/deprecated/$dep_name"
count=$((count + 1))
done < <(manifest_read "$section")
printf '%d' "$count"
}
# ── Component installers ──────────────────────────────────────────────────────
# Each function installs one component type and prints the changed-file count
# to stdout. All respect VERBOSE_COPY for per-file logging.
#
# USER_MUTABLE_REFS: space-separated list of reference filenames that may
# contain user-added content between MBIFC markers. These use merge_marked_file
# instead of copy_if_changed.
USER_MUTABLE_REFS="agents-registry.md agents.md"
install_agents() {
local src_dir="$1" dst_dir="$2"
local count=0 manifest=()
mkdir -p "$dst_dir"
for src in "$src_dir/"*.md; do
[[ -f "$src" ]] || continue
local name; name="$(basename "$src")"
manifest+=("$name")
copy_if_changed "$src" "$dst_dir/$name"
if [[ $_LAST_CHANGED -eq 1 ]]; then
[[ $VERBOSE_COPY -eq 1 ]] && info "Updated agent: $name" || true
count=$((count + 1))
fi
done
manifest_write "agents" "${manifest[@]}"
printf '%d' "$count"
}
install_refs() {
local src_dir="$1" dst_dir="$2"
local count=0 manifest=()
mkdir -p "$dst_dir"
for src in "$src_dir/"*.md; do
[[ -f "$src" ]] || continue
local name; name="$(basename "$src")"
local dst="$dst_dir/$name"
manifest+=("$name")
if [[ " $USER_MUTABLE_REFS " == *" $name "* ]]; then
merge_marked_file "$src" "$dst"
else
copy_if_changed "$src" "$dst"
fi
if [[ $_LAST_CHANGED -eq 1 ]]; then
[[ $VERBOSE_COPY -eq 1 ]] && info "Updated reference: $name" || true
count=$((count + 1))
fi
done
manifest_write "references" "${manifest[@]}"
printf '%d' "$count"
}
install_skills() {
local src_dir="$1" dst_dir="$2"
local count=0 manifest=()
[[ -d "$src_dir" ]] || { printf '0'; return 0; }
for skill_src in "$src_dir/"*/; do
[[ -f "${skill_src}SKILL.md" ]] || continue
local name; name="$(basename "$skill_src")"
manifest+=("$name")
mkdir -p "$dst_dir/$name"
copy_if_changed "${skill_src}SKILL.md" "$dst_dir/$name/SKILL.md"
if [[ $_LAST_CHANGED -eq 1 ]]; then
[[ $VERBOSE_COPY -eq 1 ]] && info "Updated skill: $name" || true
count=$((count + 1))
fi
done
manifest_write "skills" "${manifest[@]}"
printf '%d' "$count"
}
install_hooks() {
local src_dir="$1" dst_dir="$2"
local count=0 manifest=()
[[ -d "$src_dir" ]] || { printf '0'; return 0; }
mkdir -p "$dst_dir"
for src in "$src_dir/"*.sh; do
[[ -f "$src" ]] || continue
local name; name="$(basename "$src")"
local dst="$dst_dir/$name"
manifest+=("$name")
copy_if_changed "$src" "$dst"
if [[ $_LAST_CHANGED -eq 1 ]]; then
chmod +x "$dst"
[[ $VERBOSE_COPY -eq 1 ]] && info "Updated hook: $name" || true
count=$((count + 1))
fi
done
manifest_write "hooks" "${manifest[@]}"
printf '%d' "$count"
}
# install_plugins <src_dir> <dst_dir>
# Copies *.js plugin files from src to dst. Mirrors install_hooks but for
# opencode plugins (the opencode framework expects JavaScript files under
# .opencode/plugins/). Tracked in the manifest under key "plugins".
install_plugins() {
local src_dir="$1" dst_dir="$2"
local count=0 manifest=()
[[ -d "$src_dir" ]] || { printf '0'; return 0; }
mkdir -p "$dst_dir"
for src in "$src_dir/"*.js; do
[[ -f "$src" ]] || continue
local name; name="$(basename "$src")"
local dst="$dst_dir/$name"
manifest+=("$name")
copy_if_changed "$src" "$dst"
if [[ $_LAST_CHANGED -eq 1 ]]; then
[[ $VERBOSE_COPY -eq 1 ]] && info "Updated plugin: $name" || true
count=$((count + 1))
fi
done
manifest_write "plugins" "${manifest[@]}"
printf '%d' "$count"
}
# install_settings <src_json> <dst_dir>
# Always syncs settings.json from src to dst when they differ.
# Creates a .bak of the previous version so users can recover custom entries.
# Sets _LAST_CHANGED.
install_settings() {
local src="$1" dst_dir="$2"
local dst="$dst_dir/settings.json"
_LAST_CHANGED=0
[[ -f "$src" ]] || return 0
mkdir -p "$dst_dir"
if [[ ! -f "$dst" ]]; then
cp "$src" "$dst"
_LAST_CHANGED=1
elif ! diff -q "$src" "$dst" >/dev/null 2>&1; then
cp "$dst" "${dst}.bak"
cp "$src" "$dst"
_LAST_CHANGED=1
[[ $VERBOSE_COPY -eq 1 ]] && info "Updated settings.json (previous version saved as settings.json.bak)" || true
[[ $VERBOSE_COPY -eq 1 ]] && info "For custom hooks, use settings.local.json instead" || true
fi
}
# install_dispatcher <src_file> <dst_path>
# Copies the source dispatcher file to the destination.
# Sets _LAST_CHANGED.
install_dispatcher() {
local src="$1" dst="$2"
[[ -f "$src" ]] || return 0
copy_if_changed "$src" "$dst"
[[ $_LAST_CHANGED -eq 1 && $VERBOSE_COPY -eq 1 ]] && info "Updated $(basename "$dst")" || true
}
# ── UI helpers ────────────────────────────────────────────────────────────────
print_banner() {
local title="$1"
echo ""
echo -e "${BOLD}╔══════════════════════════════════════════╗${NC}"
echo -e "${BOLD}║ My Brain Is Full - Crew :: ${title}${NC}"
echo -e "${BOLD}╚══════════════════════════════════════════╝${NC}"
echo ""
}