Files
Lyrebird/scripts/sync-claude-from-working.sh
T
2026-06-01 18:27:40 -07:00

138 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# sync-claude-from-working.sh
#
# Source of truth: ~/.claude/agents/ + ~/.claude/skills/ (the maintainer's
# live working copies). This script mirrors them into the repo so contributors
# cloning Lyrebird get an up-to-date reference snapshot.
#
# Repo destinations:
# .claude/agents/ .claude/skills/ — auto-discovered by Claude Code per-project
# claude/agents/ claude/skills/ — installer source for `claude/install.sh`
# (copies into a contributor's ~/.claude)
#
# Usage:
# bash scripts/sync-claude-from-working.sh # dry-run: show what would change
# bash scripts/sync-claude-from-working.sh --apply # perform copies
# bash scripts/sync-claude-from-working.sh --check # exit 1 if any drift exists (CI)
#
# Behaviour:
# - Only files whose name matches an existing repo entry are copied. New
# agents/skills must be added to the repo by hand the first time, then
# this script will keep them in sync.
# - Files in the repo with no working-copy counterpart are reported as
# "orphan" — script does NOT delete them; investigate manually.
# - Lists every Action: COPY / SAME / MISSING-IN-WORKING / ORPHAN-IN-REPO.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SRC_AGENTS="$HOME/.claude/agents"
SRC_SKILLS="$HOME/.claude/skills"
REPO_DIRS=(".claude" "claude")
MODE="dry-run"
for arg in "$@"; do
case "$arg" in
--apply) MODE="apply" ;;
--check) MODE="check" ;;
-h|--help) sed -n '2,25p' "$0" | sed 's/^# \?//'; exit 0 ;;
*) echo "Unknown argument: $arg" >&2; exit 1 ;;
esac
done
if [[ ! -d "$SRC_AGENTS" ]]; then
echo "ERROR: working agents dir not found: $SRC_AGENTS" >&2
exit 1
fi
drift_count=0
copy_count=0
same_count=0
orphan_count=0
missing_count=0
log() { printf " %-26s %s\n" "$1" "$2"; }
sync_file() {
local src="$1" dst="$2"
if [[ ! -f "$src" ]]; then
log "MISSING-IN-WORKING" "$dst (working copy gone — not deleting from repo)"
((missing_count++)) || true
return
fi
if [[ -f "$dst" ]] && cmp -s "$src" "$dst"; then
log "SAME" "$dst"
((same_count++)) || true
return
fi
log "COPY" "$dst"
((copy_count++)) || true
((drift_count++)) || true
if [[ "$MODE" == "apply" ]]; then
mkdir -p "$(dirname "$dst")"
cp "$src" "$dst"
fi
}
for repo_dir in "${REPO_DIRS[@]}"; do
echo ""
echo "=== $repo_dir/agents/ ==="
if [[ -d "$REPO_ROOT/$repo_dir/agents" ]]; then
for repo_file in "$REPO_ROOT/$repo_dir/agents"/*.md; do
[[ -e "$repo_file" ]] || continue
name="$(basename "$repo_file")"
sync_file "$SRC_AGENTS/$name" "$repo_file"
done
# Report working-copy agents that aren't yet in this repo dir.
for src_file in "$SRC_AGENTS"/*.md; do
[[ -e "$src_file" ]] || continue
name="$(basename "$src_file")"
if [[ ! -f "$REPO_ROOT/$repo_dir/agents/$name" ]]; then
log "NEW-IN-WORKING" "$repo_dir/agents/$name (add manually if intended)"
((orphan_count++)) || true
fi
done
fi
echo ""
echo "=== $repo_dir/skills/ ==="
if [[ -d "$REPO_ROOT/$repo_dir/skills" ]]; then
for repo_skill_dir in "$REPO_ROOT/$repo_dir/skills"/*/; do
[[ -d "$repo_skill_dir" ]] || continue
skill_name="$(basename "$repo_skill_dir")"
sync_file "$SRC_SKILLS/$skill_name/skill.md" "$repo_skill_dir/skill.md"
done
for src_skill_dir in "$SRC_SKILLS"/*/; do
[[ -d "$src_skill_dir" ]] || continue
skill_name="$(basename "$src_skill_dir")"
if [[ ! -d "$REPO_ROOT/$repo_dir/skills/$skill_name" ]]; then
log "NEW-IN-WORKING" "$repo_dir/skills/$skill_name (add manually if intended)"
((orphan_count++)) || true
fi
done
fi
done
echo ""
echo "── summary ──────────────────────────────────────────────"
printf " same: %d\n" "$same_count"
printf " copy needed/applied: %d\n" "$copy_count"
printf " new-in-working: %d\n" "$orphan_count"
printf " missing-in-working: %d\n" "$missing_count"
printf " mode: %s\n" "$MODE"
if [[ "$MODE" == "check" ]]; then
if (( drift_count > 0 || orphan_count > 0 )); then
echo ""
echo "DRIFT detected. Run: bash scripts/sync-claude-from-working.sh --apply"
exit 1
fi
echo ""
echo "Repo agent/skill mirror is up to date."
fi
if [[ "$MODE" == "dry-run" && $drift_count -gt 0 ]]; then
echo ""
echo "Dry-run only. To apply: bash scripts/sync-claude-from-working.sh --apply"
fi