#!/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 correctly by a separate apm code path (core/plugin_manifest.py, run as # part of the same `apm pack` invocation), and .mcp.json is hand-authored at the # plugin root per ADR-0015 (it is not an .apm/ primitive). # # 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" } sync_one() { local plugin_dir="${1%/}" local apm_dir="$plugin_dir/.apm" if [[ ! -d "$apm_dir" ]]; then echo "SKIP $plugin_dir: no .apm/ directory" >&2 return 0 fi local name scratch bundle_dir pack_log name="$(basename "$plugin_dir")" scratch="$SCRATCH_ROOT/$name" mkdir -p "$scratch" pack_log="$(mktemp)" if ! (cd "$plugin_dir" && apm pack --format plugin -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 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 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" } for plugin_dir in "$@"; do sync_one "$plugin_dir" 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