#!/usr/bin/env bash set -euo pipefail # Mirrors each plugin's .apm/{agents,skills,prompts,commands,instructions,extensions,hooks} # into flat plugin-root directories (agents/, skills/, commands/, instructions/, # extensions/, hooks.json) -- Claude Code's and GitHub Copilot's plugin loaders # convention-scan those flat paths at the plugin root; neither has any awareness of # apm's .apm/ nesting (confirmed via `strings` on the installed claude binary and a # live `claude --plugin-dir -p ...` discoverability test -- see issue #90). # `apm pack --format plugin` already implements the correct .apm/ -> plugin-convention # mapping (it's built for distributable bundles under build/, a directory nothing in # marketplace.json points at); this script reuses that mapping and copies the relevant # subset back into the plugin root as compiled output -- same status as # .claude-plugin/plugin.json, never hand-edited when .apm/ is present. # # plugin.json, apm.lock.yaml, and .mcp.json from the bundle are deliberately NOT # copied: .claude-plugin/plugin.json + .github/plugin/plugin.json are already # generated in-place at the plugin root by a separate apm code path # (core/plugin_manifest.py, run as part of the same `apm pack` invocation, keyed off # cwd rather than -o), and .mcp.json is hand-authored at the plugin root per ADR-0015 # (it is not an .apm/ primitive). Real-mode syncs pass --force so that path actually # refreshes both files from current apm.yml/.apm/ content -- apm pack silently skips # regenerating an existing plugin.json otherwise ("already exists; skipping plugin.json # generation"), which would let them go stale after a name/version/description edit. # --check does NOT pass --force (it must not mutate the plugin root), so plugin.json # staleness is not currently detected by --check -- only fixed by the next real sync. # # hooks.json is only synced when .apm/hooks/ actually produces one -- a plugin with # no .apm/hooks/ content is left alone even if a root-level hooks.json already exists # (pre-existing scaffolding outside this script's concern). # # tests/ subdirectories (e.g. .apm/skills//tests/*.bats) are excluded from the # mirror -- they are dev-time fixtures a plugin host never needs to discover, and several # reference their own repo root via a hardcoded relative walk-up (e.g. # `../../../../../../`) sized for the .apm/-nested depth. Mirroring them verbatim would # duplicate each file one directory level shallower than that walk-up expects, breaking # the duplicate and double-running the original under any repo-wide bats/test discovery. usage() { echo "Usage: $0 [--check] [ ...]" >&2 exit 1 } CHECK=0 if [[ "${1:-}" == "--check" ]]; then CHECK=1 shift fi [[ $# -ge 1 ]] || usage if ! command -v apm &>/dev/null; then echo "Error: apm is required but not installed (see kyberforge:apm-install)" >&2 exit 1 fi # Convention subdirectories apm's plugin exporter can populate from .apm/. MIRROR_DIRS=(agents skills commands instructions extensions) FAIL=0 SCRATCH_ROOT="$(mktemp -d)" trap 'rm -rf "$SCRATCH_ROOT"' EXIT sync_dir() { local plugin_dir="$1" bundle_dir="$2" d="$3" local src="$bundle_dir/$d" dst="$plugin_dir/$d" if [[ "$CHECK" -eq 1 ]]; then if [[ -d "$src" ]]; then if [[ ! -d "$dst" ]]; then echo "DRIFT $dst: missing (would be created from .apm/)" >&2 FAIL=1 elif ! diff -rq -x tests "$src" "$dst" >/dev/null 2>&1; then echo "DRIFT $dst: out of sync with .apm/" >&2 diff -rq -x tests "$src" "$dst" 2>&1 | sed 's/^/ /' >&2 FAIL=1 fi elif [[ -d "$dst" ]]; then echo "DRIFT $dst: stale, no longer produced from .apm/" >&2 FAIL=1 fi return 0 fi if [[ -d "$src" ]]; then rm -rf "$dst" mkdir -p "$dst" cp -a "$src/." "$dst/" find "$dst" -type d -name tests -exec rm -rf {} + elif [[ -d "$dst" ]]; then rm -rf "$dst" fi } sync_hooks_json() { local plugin_dir="$1" bundle_dir="$2" local src="$bundle_dir/hooks.json" dst="$plugin_dir/hooks.json" # No .apm/hooks/ content -- hooks.json (if any) is out of scope for this script. [[ -f "$src" ]] || return 0 # apm's bundle exporter writes hooks.json without a trailing newline, which # end-of-file-fixer (pre-commit) would flag on every regeneration -- normalize both # sides of the comparison (and the real write) to exactly one trailing newline # instead of fighting that hook on every sync. if [[ "$CHECK" -eq 1 ]]; then local normalized_src normalized_src="$(mktemp)" printf '%s\n' "$(cat "$src")" >"$normalized_src" if [[ ! -f "$dst" ]] || ! diff -q "$normalized_src" "$dst" >/dev/null 2>&1; then echo "DRIFT $dst: out of sync with .apm/hooks/" >&2 FAIL=1 fi rm -f "$normalized_src" return 0 fi printf '%s\n' "$(cat "$src")" >"$dst" } # Runs entirely inside a backgrounded subshell (see the dispatch loop below), so # FAIL here is that subshell's own copy -- it never touches the parent's FAIL # and must be handed back via status_file instead. sync_one() { local plugin_dir="${1%/}" status_file="$2" local apm_dir="$plugin_dir/.apm" FAIL=0 if [[ ! -d "$apm_dir" ]]; then echo "SKIP $plugin_dir: no .apm/ directory" >&2 echo "$FAIL" >"$status_file" return 0 fi local name scratch bundle_dir pack_log name="$(basename "$plugin_dir")" scratch="$SCRATCH_ROOT/$name" mkdir -p "$scratch" pack_log="$(mktemp)" local force_flag=() [[ "$CHECK" -eq 0 ]] && force_flag=(--force) if ! (cd "$plugin_dir" && apm pack --format plugin "${force_flag[@]}" -o "$scratch") >"$pack_log" 2>&1; then echo "FAIL $plugin_dir: apm pack failed:" >&2 sed 's/^/ /' "$pack_log" >&2 rm -f "$pack_log" FAIL=1 echo "$FAIL" >"$status_file" return 0 fi rm -f "$pack_log" bundle_dir="$(find "$scratch" -mindepth 1 -maxdepth 1 -type d | head -1)" if [[ -z "$bundle_dir" ]]; then echo "FAIL $plugin_dir: apm pack produced no bundle directory under $scratch" >&2 FAIL=1 echo "$FAIL" >"$status_file" return 0 fi local d for d in "${MIRROR_DIRS[@]}"; do sync_dir "$plugin_dir" "$bundle_dir" "$d" done sync_hooks_json "$plugin_dir" "$bundle_dir" echo "$FAIL" >"$status_file" } # Each plugin's `apm pack` is an independent CLI invocation dominated by fixed # process-startup cost, not by per-plugin work -- run them concurrently rather # than paying that startup cost N times serially. Output is buffered per plugin # (not streamed) so concurrent DRIFT/FAIL messages from different plugins never # interleave; it's flushed in stable $@ order once every job has finished. declare -a plugin_dirs=("$@") for plugin_dir in "${plugin_dirs[@]}"; do name="$(basename "${plugin_dir%/}")" (sync_one "$plugin_dir" "$SCRATCH_ROOT/$name.status") >"$SCRATCH_ROOT/$name.log" 2>&1 & done wait for plugin_dir in "${plugin_dirs[@]}"; do name="$(basename "${plugin_dir%/}")" cat "$SCRATCH_ROOT/$name.log" >&2 status="$(cat "$SCRATCH_ROOT/$name.status" 2>/dev/null || echo 1)" [[ "$status" -ne 0 ]] && FAIL=1 done if [[ "$FAIL" -ne 0 ]]; then if [[ "$CHECK" -eq 1 ]]; then echo "Plugin content mirror is out of sync with .apm/. Fix: bash scripts/sync-plugin-content.sh $*" >&2 fi exit 1 fi