Files
holocron/scripts/sync-plugin-content.sh
Defame1297 b0936ad386 fix(kyberforge): force plugin.json regeneration on every real sync
apm pack silently skips regenerating plugin.json when it already exists
("already exists; skipping plugin.json generation. Re-run with --force to
overwrite it."). sync-plugin-content.sh never passed --force, so
.claude-plugin/plugin.json and .github/plugin/plugin.json were written
once on first sync and never refreshed again -- a name/version/description
edit in a plugin's apm.yml would silently stop propagating, with no gate
catching it (check-manifests.sh only checks presence; check-plugin-content-
sync explicitly excludes plugin.json; apm-pack-check-clean is scoped to
marketplace.json only).

Pass --force on real-mode syncs only (--check must not mutate the plugin
root, so plugin.json staleness there stays a known, undetected gap until
the next real sync -- documented in the script's header comment).

Regenerating surfaced a second, unrelated bug: apm's writer and
pretty-format-json's --autofix disagree on JSON key order (semantic vs.
alphabetical), so every real sync would otherwise flip plugin.json's key
order right back after a commit re-alphabetized it. Excluded the four
apm-generated manifests (plugin.json x2, marketplace.json x2) from
pretty-format-json -- their exact formatting is dictated by apm's own
writer as compiled output, not this repo's generic JSON prettifier.

Verified idempotent: a second real sync after this fix produces zero
further diff.

Refs: #90
2026-08-13 18:05:29 +00:00

171 lines
6.0 KiB
Bash
Executable File

#!/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 <bundle> -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/<name>/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] <plugin-dir> [<plugin-dir> ...]" >&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)"
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
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