skills: install only prdlk/skills, prune other sources; prompt to replace non-checkout external targets

This commit is contained in:
root committed 2026-11-06 15:24:01 -05:00
1 parent af95ada4a1
commit a9d933e3bc
11 files changed
+240 -355

No files matched your search

@@ -1,15 +1,17 @@
#!/bin/sh
# Sync agent skills with .chezmoidata/skills.yaml through the Skills CLI:
# install every declared skill (one `skills add` per source), then remove
# skills a previous run installed that are no longer declared.
# install every skill in the one declared source, then remove every
# CLI-installed skill that came from anywhere else.
# skills hash: {{ .skills | toJson | sha256sum }}
set -u
command -v npx >/dev/null 2>&1 || { echo "warning: npx not found — skipping skills sync" >&2; exit 0; }
command -v jq >/dev/null 2>&1 || { echo "warning: jq not found — skipping skills sync" >&2; exit 0; }
src={{ .chezmoi.sourceDir | quote }}
state=${XDG_STATE_HOME:-$HOME/.local/state}/chezmoi/skills.txt
declared='{{ range .skills.sources }}{{ range .skills }} {{ . }}{{ end }}{{ end }} '
source={{ .skills.source | quote }}
agents={{ .skills.agents | join " " | quote }}
lock=${XDG_STATE_HOME:-$HOME/.local/state}/skills/.skill-lock.json
skills() { npx -y {{ .skills.cli | quote }} "$@" </dev/null; }
@@ -23,48 +25,28 @@ for dir in "$HOME/.agents/skills"/* "$HOME/.claude/skills"/*; do
rm -rf "$dir"
fi
done
lock=$HOME/.agents/.skill-lock.json
if [ -L "$lock" ]; then
case $(readlink "$lock") in "$src"/*) rm -f "$lock" ;; esac
old_lock=$HOME/.agents/.skill-lock.json
if [ -L "$old_lock" ]; then
case $(readlink "$old_lock") in "$src"/*) rm -f "$old_lock" ;; esac
fi
# State file of the old per-skill sync; the CLI's lock file replaces it.
rm -f "${XDG_STATE_HOME:-$HOME/.local/state}/chezmoi/skills.txt"
# Remove what the last sync installed but skills.yaml no longer lists.
stale=''
if [ -f "$state" ]; then
while IFS= read -r name; do
case $declared in *" $name "*) ;; *) stale="$stale $name" ;; esac
done <"$state"
fi
if [ -n "$stale" ]; then
echo "skills: removing$stale"
# shellcheck disable=SC2086 # one word per skill name
skills remove -g -y $stale || echo "warning: skills remove failed:$stale" >&2
fi
failed=''
log=$(mktemp)
trap 'rm -f "$log"' EXIT
add() {
source=$1 agents=$2
shift 2
echo "skills: $source → $*"
# shellcheck disable=SC2086 # agents is a space-separated list
if ! skills add "$source" -g -y -a $agents --skill "$@" >"$log" 2>&1; then
failed="$failed $source"
tail -n 8 "$log" >&2
fi
}
{{ range .skills.sources -}}
{{ $agents := $.skills.agents -}}
{{ if hasKey . "agents" }}{{ $agents = .agents }}{{ end -}}
add {{ .source | quote }} {{ $agents | join " " | quote }}{{ range .skills }} {{ . | quote }}{{ end }}
{{ end -}}
mkdir -p "$(dirname "$state")"
printf '%s\n' $declared >"$state"
if [ -n "$failed" ]; then
# Non-zero keeps the old hash, so the next `chezmoi apply` retries.
echo "error: skills add failed for:$failed" >&2
echo "skills: $source → all skills"
# shellcheck disable=SC2086 # agents is a space-separated list
if ! skills add "$source" -g -y -a $agents --skill '*'; then
# Non-zero keeps the old hash, so the next `chezmoi apply` retries. Nothing
# is removed while the install is broken.
echo "error: skills add failed for $source" >&2
exit 1
fi
# The lock records each skill's source as owner/repo, also for tree URLs.
repo=$(printf '%s\n' "$source" | sed -E 's#^https://github\.com/##; s#(\.git)?(/tree/.*)?/?$##')
stale=$(jq -r --arg repo "$repo" \
'.skills // {} | to_entries[] | select(.value.source != $repo) | .key' "$lock" 2>/dev/null)
if [ -n "$stale" ]; then
echo "skills: removing" $stale
# shellcheck disable=SC2086 # one word per skill name
skills remove -g -y $stale || { echo "error: skills remove failed" >&2; exit 1; }
fi