playbook/scripts/install_codex_skills.sh

76 lines
2.0 KiB
Bash

#!/usr/bin/env sh
set -eu
# Install Codex skills from this Playbook snapshot into CODEX_HOME.
# - Source: <snapshot>/codex/skills/<skill-name>/
# - Dest: $CODEX_HOME/skills/<skill-name>/ (default CODEX_HOME=~/.codex)
#
# Usage:
# sh scripts/install_codex_skills.sh # install all skills
# sh scripts/install_codex_skills.sh style-cleanup code-review-workflow
# sh scripts/install_codex_skills.sh --local # install to <cwd>/.codex
#
# Notes:
# - Codex loads skills at startup; restart `codex` after installation.
# - Existing destination skill dirs are backed up with a timestamp suffix.
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
SRC="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd -P)"
SKILLS_SRC_ROOT="$SRC/codex/skills"
LOCAL_MODE=0
if [ "${1:-}" = "--local" ] || [ "${1:-}" = "-l" ]; then
LOCAL_MODE=1
shift
fi
if [ "$LOCAL_MODE" -eq 1 ]; then
LOCAL_CODEX_HOME="$(pwd -P)/.codex"
CODEX_HOME="${CODEX_HOME:-$LOCAL_CODEX_HOME}"
fi
CODEX_HOME="${CODEX_HOME:-$HOME/.codex}"
SKILLS_DST_ROOT="$CODEX_HOME/skills"
if [ ! -d "$SKILLS_SRC_ROOT" ]; then
echo "ERROR: skills source dir not found: $SKILLS_SRC_ROOT" >&2
exit 1
fi
mkdir -p "$SKILLS_DST_ROOT"
timestamp="$(date +%Y%m%d%H%M%S 2>/dev/null || echo bak)"
install_one() {
name="$1"
src_dir="$SKILLS_SRC_ROOT/$name"
dst_dir="$SKILLS_DST_ROOT/$name"
if [ ! -d "$src_dir" ]; then
echo "ERROR: skill not found: $name ($src_dir)" >&2
exit 1
fi
if [ -e "$dst_dir" ]; then
mv "$dst_dir" "$SKILLS_DST_ROOT/$name.bak.$timestamp"
echo "Backed up existing skill: $name -> $name.bak.$timestamp"
fi
cp -R "$src_dir" "$dst_dir"
echo "Installed: $name"
}
if [ "$#" -gt 0 ]; then
for name in "$@"; do
install_one "$name"
done
else
for dir in "$SKILLS_SRC_ROOT"/*; do
[ -d "$dir" ] || continue
name="$(basename -- "$dir")"
case "$name" in
""|.*) continue ;;
esac
install_one "$name"
done
fi
echo "Done. Skills installed to: $SKILLS_DST_ROOT"