mirror of
https://github.com/github/spec-kit.git
synced 2026-03-17 19:03:08 +00:00
* Initial plan * feat(templates): add pluggable template system with packs, catalog, resolver, and CLI commands Co-authored-by: mnriem <15701806+mnriem@users.noreply.github.com> * test(templates): add comprehensive unit tests for template pack system Co-authored-by: mnriem <15701806+mnriem@users.noreply.github.com> * feat(presets): pluggable preset system with template/command overrides, catalog, and resolver - Rename 'template packs' to 'presets' to avoid naming collision with core templates - PresetManifest, PresetRegistry, PresetManager, PresetCatalog, PresetResolver in presets.py - Extract CommandRegistrar to agents.py as shared infrastructure - CLI: specify preset list/add/remove/search/resolve/info - CLI: specify preset catalog list/add/remove - --preset option on specify init - Priority-based preset stacking (--priority, lower = higher precedence) - Command overrides registered into all detected agent directories (17+ agents) - Extension command safety: skip registration if target extension not installed - Multi-catalog support: env var, project config, user config, built-in defaults - resolve_template() / Resolve-Template in bash/PowerShell scripts - Self-test preset: overrides all 6 core templates + 1 command - Scaffold with 4 examples: core/extension template and command overrides - Preset catalog (catalog.json, catalog.community.json) - Documentation: README.md, ARCHITECTURE.md, PUBLISHING.md - 110 preset tests, 253 total tests passing * feat(presets): propagate command overrides to skills via init-options - Add save_init_options() / load_init_options() helpers that persist CLI flags from 'specify init' to .specify/init-options.json - PresetManager._register_skills() overwrites SKILL.md files when --ai-skills was used during init and corresponding skill dirs exist - PresetManager._unregister_skills() restores core template content on preset removal - registered_skills stored in preset registry metadata - 8 new tests covering skill override, skip conditions, and restore * fix: address PR check failures (ruff F541, CodeQL URL substring) - Remove extraneous f-prefix from two f-strings without placeholders - Replace substring URL check in test with startswith/endswith assertions to satisfy CodeQL incomplete URL substring sanitization rule * fix: address Copilot PR review comments - Move save_init_options() before preset install so skills propagation works during 'specify init --preset --ai-skills' - Clean up downloaded ZIP after successful preset install during init - Validate --from URL scheme (require HTTPS, HTTP only for localhost) - Expose unregister_commands() on extensions.py CommandRegistrar wrapper instead of reaching into private _registrar field - Use _get_merged_packs() for search() and get_pack_info() so all active catalogs are searched, not just the highest-priority one - Fix fetch_catalog() cache to verify cached URL matches current URL - Fix PresetResolver: script resolution uses .sh extension, consistent file extensions throughout resolve(), and resolve_with_source() delegates to resolve() to honor template_type parameter - Fix bash common.sh: fall through to directory scan when python3 returns empty preset list - Fix PowerShell Resolve-Template: filter out dot-folders and sort extensions deterministically * fix: narrow empty except blocks and add explanatory comments * fix: address Copilot PR review comments (round 2) - Fix init --preset error masking: distinguish "not found" from real errors - Fix bash resolve_template: skip hidden dirs in extensions (match Python/PS) - Fix temp dir leaks in tests: use temp_dir fixture instead of mkdtemp - Fix self-test catalog entry: add note that it's local-only (no download_url) - Fix Windows path issue in resolve_with_source: use Path.relative_to() - Fix skill restore path: use project's .specify/templates/commands/ not source tree - Add encoding="utf-8" to all file read/write in agents.py - Update test to set up core command templates for skill restoration * fix: remove self-test from catalog.json (local-only preset) * fix: address Copilot PR review comments (round 3) - Fix PS Resolve-Template fallback to skip dot-prefixed dirs (.cache) - Rename _catalog to _catalog_name for consistency with extension system - Enforce install_allowed policy in CLI preset add and download_pack() - Fix shell injection: pass registry path via env var instead of string interpolation * fix: correct PresetError docstring from template to preset * Removed CHANGELOG requirement * Applying review recommendations * Applying review recommendations * Applying review recommendations * Applying review recommendations --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mnriem <15701806+mnriem@users.noreply.github.com>
254 lines
8.7 KiB
Bash
254 lines
8.7 KiB
Bash
#!/usr/bin/env bash
|
|
# Common functions and variables for all scripts
|
|
|
|
# Get repository root, with fallback for non-git repositories
|
|
get_repo_root() {
|
|
if git rev-parse --show-toplevel >/dev/null 2>&1; then
|
|
git rev-parse --show-toplevel
|
|
else
|
|
# Fall back to script location for non-git repos
|
|
local script_dir="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
(cd "$script_dir/../../.." && pwd)
|
|
fi
|
|
}
|
|
|
|
# Get current branch, with fallback for non-git repositories
|
|
get_current_branch() {
|
|
# First check if SPECIFY_FEATURE environment variable is set
|
|
if [[ -n "${SPECIFY_FEATURE:-}" ]]; then
|
|
echo "$SPECIFY_FEATURE"
|
|
return
|
|
fi
|
|
|
|
# Then check git if available
|
|
if git rev-parse --abbrev-ref HEAD >/dev/null 2>&1; then
|
|
git rev-parse --abbrev-ref HEAD
|
|
return
|
|
fi
|
|
|
|
# For non-git repos, try to find the latest feature directory
|
|
local repo_root=$(get_repo_root)
|
|
local specs_dir="$repo_root/specs"
|
|
|
|
if [[ -d "$specs_dir" ]]; then
|
|
local latest_feature=""
|
|
local highest=0
|
|
|
|
for dir in "$specs_dir"/*; do
|
|
if [[ -d "$dir" ]]; then
|
|
local dirname=$(basename "$dir")
|
|
if [[ "$dirname" =~ ^([0-9]{3})- ]]; then
|
|
local number=${BASH_REMATCH[1]}
|
|
number=$((10#$number))
|
|
if [[ "$number" -gt "$highest" ]]; then
|
|
highest=$number
|
|
latest_feature=$dirname
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [[ -n "$latest_feature" ]]; then
|
|
echo "$latest_feature"
|
|
return
|
|
fi
|
|
fi
|
|
|
|
echo "main" # Final fallback
|
|
}
|
|
|
|
# Check if we have git available
|
|
has_git() {
|
|
git rev-parse --show-toplevel >/dev/null 2>&1
|
|
}
|
|
|
|
check_feature_branch() {
|
|
local branch="$1"
|
|
local has_git_repo="$2"
|
|
|
|
# For non-git repos, we can't enforce branch naming but still provide output
|
|
if [[ "$has_git_repo" != "true" ]]; then
|
|
echo "[specify] Warning: Git repository not detected; skipped branch validation" >&2
|
|
return 0
|
|
fi
|
|
|
|
if [[ ! "$branch" =~ ^[0-9]{3}- ]]; then
|
|
echo "ERROR: Not on a feature branch. Current branch: $branch" >&2
|
|
echo "Feature branches should be named like: 001-feature-name" >&2
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
get_feature_dir() { echo "$1/specs/$2"; }
|
|
|
|
# Find feature directory by numeric prefix instead of exact branch match
|
|
# This allows multiple branches to work on the same spec (e.g., 004-fix-bug, 004-add-feature)
|
|
find_feature_dir_by_prefix() {
|
|
local repo_root="$1"
|
|
local branch_name="$2"
|
|
local specs_dir="$repo_root/specs"
|
|
|
|
# Extract numeric prefix from branch (e.g., "004" from "004-whatever")
|
|
if [[ ! "$branch_name" =~ ^([0-9]{3})- ]]; then
|
|
# If branch doesn't have numeric prefix, fall back to exact match
|
|
echo "$specs_dir/$branch_name"
|
|
return
|
|
fi
|
|
|
|
local prefix="${BASH_REMATCH[1]}"
|
|
|
|
# Search for directories in specs/ that start with this prefix
|
|
local matches=()
|
|
if [[ -d "$specs_dir" ]]; then
|
|
for dir in "$specs_dir"/"$prefix"-*; do
|
|
if [[ -d "$dir" ]]; then
|
|
matches+=("$(basename "$dir")")
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Handle results
|
|
if [[ ${#matches[@]} -eq 0 ]]; then
|
|
# No match found - return the branch name path (will fail later with clear error)
|
|
echo "$specs_dir/$branch_name"
|
|
elif [[ ${#matches[@]} -eq 1 ]]; then
|
|
# Exactly one match - perfect!
|
|
echo "$specs_dir/${matches[0]}"
|
|
else
|
|
# Multiple matches - this shouldn't happen with proper naming convention
|
|
echo "ERROR: Multiple spec directories found with prefix '$prefix': ${matches[*]}" >&2
|
|
echo "Please ensure only one spec directory exists per numeric prefix." >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
get_feature_paths() {
|
|
local repo_root=$(get_repo_root)
|
|
local current_branch=$(get_current_branch)
|
|
local has_git_repo="false"
|
|
|
|
if has_git; then
|
|
has_git_repo="true"
|
|
fi
|
|
|
|
# Use prefix-based lookup to support multiple branches per spec
|
|
local feature_dir
|
|
if ! feature_dir=$(find_feature_dir_by_prefix "$repo_root" "$current_branch"); then
|
|
echo "ERROR: Failed to resolve feature directory" >&2
|
|
return 1
|
|
fi
|
|
|
|
# Use printf '%q' to safely quote values, preventing shell injection
|
|
# via crafted branch names or paths containing special characters
|
|
printf 'REPO_ROOT=%q\n' "$repo_root"
|
|
printf 'CURRENT_BRANCH=%q\n' "$current_branch"
|
|
printf 'HAS_GIT=%q\n' "$has_git_repo"
|
|
printf 'FEATURE_DIR=%q\n' "$feature_dir"
|
|
printf 'FEATURE_SPEC=%q\n' "$feature_dir/spec.md"
|
|
printf 'IMPL_PLAN=%q\n' "$feature_dir/plan.md"
|
|
printf 'TASKS=%q\n' "$feature_dir/tasks.md"
|
|
printf 'RESEARCH=%q\n' "$feature_dir/research.md"
|
|
printf 'DATA_MODEL=%q\n' "$feature_dir/data-model.md"
|
|
printf 'QUICKSTART=%q\n' "$feature_dir/quickstart.md"
|
|
printf 'CONTRACTS_DIR=%q\n' "$feature_dir/contracts"
|
|
}
|
|
|
|
# Check if jq is available for safe JSON construction
|
|
has_jq() {
|
|
command -v jq >/dev/null 2>&1
|
|
}
|
|
|
|
# Escape a string for safe embedding in a JSON value (fallback when jq is unavailable).
|
|
# Handles backslash, double-quote, and control characters (newline, tab, carriage return).
|
|
json_escape() {
|
|
local s="$1"
|
|
s="${s//\\/\\\\}"
|
|
s="${s//\"/\\\"}"
|
|
s="${s//$'\n'/\\n}"
|
|
s="${s//$'\t'/\\t}"
|
|
s="${s//$'\r'/\\r}"
|
|
printf '%s' "$s"
|
|
}
|
|
|
|
check_file() { [[ -f "$1" ]] && echo " ✓ $2" || echo " ✗ $2"; }
|
|
check_dir() { [[ -d "$1" && -n $(ls -A "$1" 2>/dev/null) ]] && echo " ✓ $2" || echo " ✗ $2"; }
|
|
|
|
# Resolve a template name to a file path using the priority stack:
|
|
# 1. .specify/templates/overrides/
|
|
# 2. .specify/presets/<preset-id>/templates/ (sorted by priority from .registry)
|
|
# 3. .specify/extensions/<ext-id>/templates/
|
|
# 4. .specify/templates/ (core)
|
|
resolve_template() {
|
|
local template_name="$1"
|
|
local repo_root="$2"
|
|
local base="$repo_root/.specify/templates"
|
|
|
|
# Priority 1: Project overrides
|
|
local override="$base/overrides/${template_name}.md"
|
|
[ -f "$override" ] && echo "$override" && return 0
|
|
|
|
# Priority 2: Installed presets (sorted by priority from .registry)
|
|
local presets_dir="$repo_root/.specify/presets"
|
|
if [ -d "$presets_dir" ]; then
|
|
local registry_file="$presets_dir/.registry"
|
|
if [ -f "$registry_file" ] && command -v python3 >/dev/null 2>&1; then
|
|
# Read preset IDs sorted by priority (lower number = higher precedence)
|
|
local sorted_presets
|
|
sorted_presets=$(SPECKIT_REGISTRY="$registry_file" python3 -c "
|
|
import json, sys, os
|
|
try:
|
|
with open(os.environ['SPECKIT_REGISTRY']) as f:
|
|
data = json.load(f)
|
|
presets = data.get('presets', {})
|
|
for pid, meta in sorted(presets.items(), key=lambda x: x[1].get('priority', 10)):
|
|
print(pid)
|
|
except Exception:
|
|
sys.exit(1)
|
|
" 2>/dev/null)
|
|
if [ $? -eq 0 ] && [ -n "$sorted_presets" ]; then
|
|
while IFS= read -r preset_id; do
|
|
local candidate="$presets_dir/$preset_id/templates/${template_name}.md"
|
|
[ -f "$candidate" ] && echo "$candidate" && return 0
|
|
done <<< "$sorted_presets"
|
|
else
|
|
# python3 returned empty list — fall through to directory scan
|
|
for preset in "$presets_dir"/*/; do
|
|
[ -d "$preset" ] || continue
|
|
local candidate="$preset/templates/${template_name}.md"
|
|
[ -f "$candidate" ] && echo "$candidate" && return 0
|
|
done
|
|
fi
|
|
else
|
|
# Fallback: alphabetical directory order (no python3 available)
|
|
for preset in "$presets_dir"/*/; do
|
|
[ -d "$preset" ] || continue
|
|
local candidate="$preset/templates/${template_name}.md"
|
|
[ -f "$candidate" ] && echo "$candidate" && return 0
|
|
done
|
|
fi
|
|
fi
|
|
|
|
# Priority 3: Extension-provided templates
|
|
local ext_dir="$repo_root/.specify/extensions"
|
|
if [ -d "$ext_dir" ]; then
|
|
for ext in "$ext_dir"/*/; do
|
|
[ -d "$ext" ] || continue
|
|
# Skip hidden directories (e.g. .backup, .cache)
|
|
case "$(basename "$ext")" in .*) continue;; esac
|
|
local candidate="$ext/templates/${template_name}.md"
|
|
[ -f "$candidate" ] && echo "$candidate" && return 0
|
|
done
|
|
fi
|
|
|
|
# Priority 4: Core templates
|
|
local core="$base/${template_name}.md"
|
|
[ -f "$core" ] && echo "$core" && return 0
|
|
|
|
# Return success with empty output so callers using set -e don't abort;
|
|
# callers check [ -n "$TEMPLATE" ] to detect "not found".
|
|
return 0
|
|
}
|
|
|