diff --git a/examples/harnesses b/examples/harnesses new file mode 100644 index 0000000..9f386bc --- /dev/null +++ b/examples/harnesses @@ -0,0 +1,6 @@ +# One harness per line: a known name or a path to its config folder +# claude +# codex +# opencode +# ~/.claudeDodolo +# /home/otheruser/.codex diff --git a/examples/skills b/examples/skills new file mode 100644 index 0000000..6316483 --- /dev/null +++ b/examples/skills @@ -0,0 +1,5 @@ +# One skill per line: [=|>|>=] +# https://git.bjphoster.com/skills/settle +# https://git.bjphoster.com/skills/ship=0.1.0 +# https://git.bjphoster.com/skills/ship>=0.1.0 +# https://git.bjphoster.com/skills/settle=be3f9d3646 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..3825c27 --- /dev/null +++ b/install.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +set -euo pipefail + +BIN_DIR="${BIN_DIR:-$HOME/.local/bin}" +LOG_FILE="${LOG_FILE:-${XDG_STATE_HOME:-$HOME/.local/state}/skills-autoupdate.log}" +SCHEDULE="${SCHEDULE:-17 * * * *}" +SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TARGET="$BIN_DIR/skills-autoupdate" +MARKER="# skills-autoupdate" + +usage() { + cat </dev/null | grep -vF "$MARKER" || true +} + +cron=1 +case "${1:-}" in + --no-cron) cron=0 ;; + --uninstall) + rm -f "$TARGET" + if command -v crontab >/dev/null; then + kept="$(current_crontab)" + printf '%s\n' "$kept" | sed '/^$/d' | crontab - + fi + echo "removed $TARGET and the crontab entry" + exit 0 + ;; + -h | --help) usage; exit 0 ;; + "") ;; + *) usage >&2; exit 2 ;; +esac + +install -Dm755 "$SRC_DIR/skills-autoupdate" "$TARGET" +echo "installed $TARGET" + +mkdir -p "$HOME/.config" +for f in skills harnesses; do + if [ ! -e "$HOME/.config/$f" ]; then + install -m644 "$SRC_DIR/examples/$f" "$HOME/.config/$f" + echo "created ~/.config/$f" + fi +done + +if [ "$cron" -eq 1 ]; then + command -v crontab >/dev/null || { echo "crontab not found, install cron or use --no-cron" >&2; exit 1; } + mkdir -p "$(dirname "$LOG_FILE")" + kept="$(current_crontab)" + printf '%s\n%s\n' "$kept" "$SCHEDULE \"$TARGET\" >>\"$LOG_FILE\" 2>&1 $MARKER" | sed '/^$/d' | crontab - + echo "crontab entry set: $SCHEDULE, log in $LOG_FILE" +fi diff --git a/skills-autoupdate b/skills-autoupdate new file mode 100755 index 0000000..52f955b --- /dev/null +++ b/skills-autoupdate @@ -0,0 +1,314 @@ +#!/usr/bin/env bash +set -uo pipefail + +VERSION="0.1.0" +SKILLS_FILE="${SKILLS_FILE:-$HOME/.config/skills}" +HARNESSES_FILE="${HARNESSES_FILE:-$HOME/.config/harnesses}" +LOCK_FILE="${SKILLS_AUTOUPDATE_LOCK:-${XDG_CACHE_HOME:-$HOME/.cache}/skills-autoupdate.lock}" + +export GIT_TERMINAL_PROMPT=0 +export GIT_ASKPASS=/bin/true +export GIT_SSH_COMMAND="${GIT_SSH_COMMAND:-ssh -o BatchMode=yes}" + +failures=0 + +usage() { + cat <") version_gt "$v" "$ref" || continue ;; + ">=") [ "$v" = "$ref" ] || version_gt "$v" "$ref" || continue ;; + esac + if [ -z "$best" ] || version_gt "$v" "$best_v"; then + best="$t" + best_v="$v" + fi + done < <(git -C "$dir" tag -l | grep -E '^v?[0-9]') + [ -n "$best" ] && printf '%s\n' "$best" +} + +describe() { + git -C "$1" describe --tags --always --exact-match HEAD 2>/dev/null || git -C "$1" rev-parse --short HEAD +} + +sync_skill() { + local harness="$1" dir="$2" url="$3" op="$4" ref="$5" + local tag + tag="[$harness] ${dir##*/}" + local before after target branch origin + + if [ ! -e "$dir" ]; then + if ! git clone -q "$url" "$dir" 2>&1; then + fail "$tag: clone of $url failed" + return + fi + log "$tag: cloned $url" + before="" + else + if [ ! -d "$dir/.git" ]; then + warn "$tag: $dir exists but is not a git checkout, skipped" + return + fi + origin="$(git -C "$dir" remote get-url origin 2>/dev/null || true)" + if [ "${origin%.git}" != "${url%.git}" ]; then + warn "$tag: origin is '$origin', expected '$url', skipped" + return + fi + if [ -n "$(git -C "$dir" status --porcelain 2>/dev/null)" ]; then + warn "$tag: local changes present, skipped" + return + fi + if ! git -C "$dir" fetch -q --prune --tags --force origin 2>&1; then + fail "$tag: fetch failed" + return + fi + before="$(git -C "$dir" rev-parse HEAD)" + fi + + case "$op" in + "") + git -C "$dir" remote set-head origin -a >/dev/null 2>&1 || true + branch="$(git -C "$dir" symbolic-ref -q --short refs/remotes/origin/HEAD)" + branch="${branch#origin/}" + if [ -z "$branch" ]; then + fail "$tag: cannot find the default branch" + return + fi + if ! git -C "$dir" checkout -q "$branch" 2>&1 || ! git -C "$dir" merge -q --ff-only "origin/$branch" 2>&1; then + fail "$tag: cannot fast-forward $branch to origin/$branch" + return + fi + ;; + "=") + target="$(git -C "$dir" rev-parse -q --verify "refs/tags/$ref^{commit}" 2>/dev/null || + git -C "$dir" rev-parse -q --verify "refs/tags/v${ref#v}^{commit}" 2>/dev/null || + git -C "$dir" rev-parse -q --verify "$ref^{commit}" 2>/dev/null)" + if [ -z "$target" ]; then + fail "$tag: ref '$ref' not found" + return + fi + if [ "$target" != "$(git -C "$dir" rev-parse HEAD)" ] && ! git -C "$dir" checkout -q --detach "$target" 2>&1; then + fail "$tag: checkout of $ref failed" + return + fi + ;; + ">" | ">=") + target="$(best_tag "$dir" "$op" "$ref")" + if [ -z "$target" ]; then + warn "$tag: no tag $op $ref, left at $(describe "$dir")" + return + fi + if [ "$(git -C "$dir" rev-parse "refs/tags/$target^{commit}")" != "$(git -C "$dir" rev-parse HEAD)" ] && + ! git -C "$dir" checkout -q --detach "refs/tags/$target" 2>&1; then + fail "$tag: checkout of $target failed" + return + fi + ;; + esac + + after="$(git -C "$dir" rev-parse HEAD)" + if [ -z "$before" ]; then + log "$tag: at $(describe "$dir")" + elif [ "$before" != "$after" ]; then + log "$tag: updated ${before:0:10} -> $(describe "$dir")" + else + log "$tag: up to date at $(describe "$dir")" + fi +} + +main() { + case "${1:-}" in + -h | --help) usage; exit 0 ;; + -V | --version) echo "skills-autoupdate $VERSION"; exit 0 ;; + "") ;; + *) usage >&2; exit 2 ;; + esac + + command -v git >/dev/null || { log "ERROR git not found"; exit 1; } + [ -r "$SKILLS_FILE" ] || { log "ERROR $SKILLS_FILE not found"; exit 1; } + [ -r "$HARNESSES_FILE" ] || { log "ERROR $HARNESSES_FILE not found"; exit 1; } + + if command -v flock >/dev/null; then + mkdir -p "$(dirname "$LOCK_FILE")" + exec 9>"$LOCK_FILE" + flock -n 9 || { log "another run is in progress, exiting"; exit 0; } + fi + + local -a h_names=() h_roots=() s_urls=() s_ops=() s_refs=() s_names=() + local raw line name root real seen="" i j + + while IFS= read -r raw || [ -n "$raw" ]; do + line="$(strip_comment "$raw")" + [ -z "$line" ] && continue + if name="$(normalize_name "$(printf '%s' "$line" | tr '[:upper:]' '[:lower:]')")"; then + root="$(default_root "$name")" + else + root="$(expand_path "$line")" + if [ ! -d "$root" ]; then + warn "harness path $root not found, skipped" + continue + fi + if ! name="$(detect_harness "$root")"; then + warn "cannot recognize the harness in $root, skipped" + continue + fi + fi + if [ ! -d "$root" ]; then + log "[$name] $root not found, harness not installed, skipped" + continue + fi + real="$(cd "$root" && pwd -P)" + case "$seen" in + *"|$real|"*) continue ;; + esac + seen="$seen|$real|" + h_names+=("$name") + h_roots+=("$root") + done <"$HARNESSES_FILE" + + while IFS= read -r raw || [ -n "$raw" ]; do + line="$(strip_comment "$raw")" + [ -z "$line" ] && continue + if [[ ! "$line" =~ ^([^=\>[:space:]]+)(\>=|\>|=)?([^[:space:]]*)$ ]]; then + warn "invalid skill line '$line', skipped" + continue + fi + if [ -n "${BASH_REMATCH[2]}" ] && [ -z "${BASH_REMATCH[3]}" ]; then + warn "missing version in '$line', skipped" + continue + fi + s_urls+=("${BASH_REMATCH[1]}") + s_ops+=("${BASH_REMATCH[2]}") + s_refs+=("${BASH_REMATCH[3]}") + name="${BASH_REMATCH[1]%/}" + name="${name##*/}" + name="${name##*:}" + s_names+=("${name%.git}") + done <"$SKILLS_FILE" + + [ "${#h_names[@]}" -eq 0 ] && { log "no harness found, nothing to do"; exit 0; } + [ "${#s_urls[@]}" -eq 0 ] && { log "no skill listed, nothing to do"; exit 0; } + + for i in "${!h_names[@]}"; do + root="$(skills_dir "${h_roots[$i]}")" + if ! mkdir -p "$root"; then + fail "[${h_names[$i]}] cannot create $root" + continue + fi + for j in "${!s_urls[@]}"; do + sync_skill "${h_names[$i]}" "$root/${s_names[$j]}" "${s_urls[$j]}" "${s_ops[$j]}" "${s_refs[$j]}" + done + done + + [ "$failures" -eq 0 ] || { log "finished with $failures error(s)"; exit 1; } + log "finished" +} + +main "$@"