Files
fish-config/functions/agents-init.fish
T

200 lines
8.0 KiB
Fish

# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
# SYNOPSIS
# agents-init [--agents] [--plugins] [-h | --help]
#
# DESCRIPTION
# Scaffolds an AGENTS/ sub-repository inside a project directory. Creates
# a self-contained git repo for agent specifications, moves any existing
# agent-related files into it, and replaces them with symlinks so the outer
# project never tracks agent files directly.
#
# With no flags, runs both --agents and --plugins setup. At the end of
# every invocation, commits any uncommitted changes in the AGENTS/ sub-repo
# so that agent-made edits are captured automatically.
#
# ARGUMENTS
# --agents Set up AGENTS/ repo + AGENTS.md / CLAUDE.md symlinks only
# --plugins Set up AGENTS/ repo + plugins dirs + docs/ symlinks only
# -h, --help Show this help message and exit
#
# RETURNS
# 0 Setup completed successfully
# 1 Fatal error (git init failed, move failed, etc.)
#
# EXAMPLE
# agents-init
# agents-init --agents
# agents-init --plugins
function agents-init --description 'scaffold AGENTS/ sub-repo with agent spec files and plugin dirs'
set -l c_head (set_color --bold cyan)
set -l c_cmd (set_color --bold white)
set -l c_flag (set_color yellow)
set -l c_ok (set_color green)
set -l c_warn (set_color yellow)
set -l c_dim (set_color brblack)
set -l c_err (set_color red)
set -l c_reset (set_color normal)
argparse 'h/help' 'agents' 'plugins' -- $argv
or return 1
if set -q _flag_help
echo "$c_head""Usage:$c_reset $c_cmd""agents-init$c_reset $c_flag""[--agents] [--plugins] [-h | --help]$c_reset"
echo
echo " Scaffold an AGENTS/ sub-repository for tracking agent specifications."
echo
echo "$c_head""Options:$c_reset"
echo " $c_flag-h$c_reset, $c_flag--help$c_reset Show this help message"
echo " $c_flag--agents$c_reset Set up AGENTS.md / CLAUDE.md symlinks only"
echo " $c_flag--plugins$c_reset Set up plugins dirs and docs/ symlinks only"
echo " $c_dim(no flags)$c_reset Run both --agents and --plugins setup"
return 0
end
# No flags → run both modes
set -l do_agents 0
set -l do_plugins 0
set -q _flag_agents; and set do_agents 1
set -q _flag_plugins; and set do_plugins 1
if test $do_agents -eq 0; and test $do_plugins -eq 0
set do_agents 1
set do_plugins 1
end
# Resolve target root: git root if available, otherwise cwd
set -l root (git rev-parse --show-toplevel 2>/dev/null)
test -z "$root"; and set root (pwd)
set -l agents_dir "$root/AGENTS"
set -l plugins_dir "$agents_dir/plugins"
# ─────────────────────── Always: AGENTS/ sub-repo ───────────────────────
set -l did_init 0 # ponytail: set to 1 after git init — consumed by auto-commit in Task 5
if not test -d "$agents_dir"
if not mkdir -p "$agents_dir"
echo "$c_err""Error: could not create AGENTS/$c_reset" >&2
return 1
end
echo "$c_ok→ Created AGENTS/$c_reset"
end
if not test -d "$agents_dir/.git"
git -C "$agents_dir" init -q
or begin
echo "$c_err""Error: git init failed in AGENTS/$c_reset" >&2
return 1
end
echo "$c_ok→ Initialized git repo in AGENTS/$c_reset"
set did_init 1
end
# Ensure plugins dirs with .gitkeep
for dir in "$plugins_dir" "$plugins_dir/superpowers" "$plugins_dir/plans" "$plugins_dir/specs"
if not test -d "$dir"
if not mkdir -p "$dir"
echo "$c_err""Error: could not create "(string replace "$root/" "" "$dir")"/$c_reset" >&2
return 1
end
echo "$c_ok→ Created "(string replace "$root/" "" "$dir")"/$c_reset"
end
test -f "$dir/.gitkeep"; or touch "$dir/.gitkeep"
end
# ── Auto-commit (runs at end — defined here as a closure-style block) ─────
# Implemented in Task 5; placeholder comment keeps structure clear.
# ──────────────────────────── --agents mode ──────────────────────────────
if test $do_agents -eq 1
# Move AGENTS.md from project root into sub-repo if it's a real file
if test -f "$root/AGENTS.md"; and not test -L "$root/AGENTS.md"
if not mv "$root/AGENTS.md" "$agents_dir/AGENTS.md"
echo "$c_err""Error: could not move AGENTS.md → AGENTS/AGENTS.md$c_reset" >&2
return 1
end
echo "$c_ok→ Moved AGENTS.md → AGENTS/AGENTS.md$c_reset"
end
# Create empty AGENTS.md in sub-repo if still missing
if not test -f "$agents_dir/AGENTS.md"
touch "$agents_dir/AGENTS.md"
echo "$c_ok→ Created AGENTS/AGENTS.md$c_reset"
end
# Symlink AGENTS.md → AGENTS/AGENTS.md in project root
if not test -L "$root/AGENTS.md"
if not ln -s AGENTS/AGENTS.md "$root/AGENTS.md"
echo "$c_err""Error: could not create AGENTS.md symlink$c_reset" >&2
return 1
end
echo "$c_ok→ Linked AGENTS.md → AGENTS/AGENTS.md$c_reset"
end
# Move real CLAUDE.md into sub-repo if it exists as a regular file
if test -f "$root/CLAUDE.md"; and not test -L "$root/CLAUDE.md"
if not mv "$root/CLAUDE.md" "$agents_dir/CLAUDE.md"
echo "$c_err""Error: could not move CLAUDE.md → AGENTS/CLAUDE.md$c_reset" >&2
return 1
end
echo "$c_ok→ Moved CLAUDE.md → AGENTS/CLAUDE.md$c_reset"
end
# Symlink CLAUDE.md → AGENTS/AGENTS.md in project root
if not test -L "$root/CLAUDE.md"
if not ln -s AGENTS/AGENTS.md "$root/CLAUDE.md"
echo "$c_err""Error: could not create CLAUDE.md symlink$c_reset" >&2
return 1
end
echo "$c_ok→ Linked CLAUDE.md → AGENTS/AGENTS.md$c_reset"
end
# Update .gitignore
for pattern in "AGENTS/" "AGENTS.md" "CLAUDE.md"
_agents_init_ensure_gitignore "$root" "$pattern"
end
end
# ─────────────────────────── --plugins mode ──────────────────────────────
if test $do_plugins -eq 1
set -l docs_dir "$root/docs"
if not test -d "$docs_dir"
if not mkdir -p "$docs_dir"
echo "$c_err""Error: could not create docs/$c_reset" >&2
return 1
end
echo "$c_ok→ Created docs/$c_reset"
end
for plug in superpowers plans specs
set -l docs_target "$docs_dir/$plug"
set -l agents_target "$plugins_dir/$plug"
# If a real directory exists in docs/, move its contents to AGENTS/plugins/
if test -d "$docs_target"; and not test -L "$docs_target"
if test -n "$(ls -A $docs_target 2>/dev/null)"
if not cp -r "$docs_target/." "$agents_target/"
echo "$c_err""Error: could not move docs/$plug → AGENTS/plugins/$plug$c_reset" >&2
return 1
end
end
rm -rf "$docs_target"
echo "$c_ok→ Moved docs/$plug → AGENTS/plugins/$plug$c_reset"
end
# Create symlink docs/<plug> → ../AGENTS/plugins/<plug>
if not test -L "$docs_target"
if not ln -s "../AGENTS/plugins/$plug" "$docs_target"
echo "$c_err""Error: could not create docs/$plug symlink$c_reset" >&2
return 1
end
echo "$c_ok→ Linked docs/$plug → AGENTS/plugins/$plug$c_reset"
end
_agents_init_ensure_gitignore "$root" "docs/$plug"
end
end
end