mirror of
https://github.com/github/spec-kit.git
synced 2026-03-17 02:43: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>
205 lines
6.4 KiB
PowerShell
205 lines
6.4 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
# Common PowerShell functions analogous to common.sh
|
|
|
|
function Get-RepoRoot {
|
|
try {
|
|
$result = git rev-parse --show-toplevel 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
return $result
|
|
}
|
|
} catch {
|
|
# Git command failed
|
|
}
|
|
|
|
# Fall back to script location for non-git repos
|
|
return (Resolve-Path (Join-Path $PSScriptRoot "../../..")).Path
|
|
}
|
|
|
|
function Get-CurrentBranch {
|
|
# First check if SPECIFY_FEATURE environment variable is set
|
|
if ($env:SPECIFY_FEATURE) {
|
|
return $env:SPECIFY_FEATURE
|
|
}
|
|
|
|
# Then check git if available
|
|
try {
|
|
$result = git rev-parse --abbrev-ref HEAD 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
return $result
|
|
}
|
|
} catch {
|
|
# Git command failed
|
|
}
|
|
|
|
# For non-git repos, try to find the latest feature directory
|
|
$repoRoot = Get-RepoRoot
|
|
$specsDir = Join-Path $repoRoot "specs"
|
|
|
|
if (Test-Path $specsDir) {
|
|
$latestFeature = ""
|
|
$highest = 0
|
|
|
|
Get-ChildItem -Path $specsDir -Directory | ForEach-Object {
|
|
if ($_.Name -match '^(\d{3})-') {
|
|
$num = [int]$matches[1]
|
|
if ($num -gt $highest) {
|
|
$highest = $num
|
|
$latestFeature = $_.Name
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($latestFeature) {
|
|
return $latestFeature
|
|
}
|
|
}
|
|
|
|
# Final fallback
|
|
return "main"
|
|
}
|
|
|
|
function Test-HasGit {
|
|
try {
|
|
git rev-parse --show-toplevel 2>$null | Out-Null
|
|
return ($LASTEXITCODE -eq 0)
|
|
} catch {
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Test-FeatureBranch {
|
|
param(
|
|
[string]$Branch,
|
|
[bool]$HasGit = $true
|
|
)
|
|
|
|
# For non-git repos, we can't enforce branch naming but still provide output
|
|
if (-not $HasGit) {
|
|
Write-Warning "[specify] Warning: Git repository not detected; skipped branch validation"
|
|
return $true
|
|
}
|
|
|
|
if ($Branch -notmatch '^[0-9]{3}-') {
|
|
Write-Output "ERROR: Not on a feature branch. Current branch: $Branch"
|
|
Write-Output "Feature branches should be named like: 001-feature-name"
|
|
return $false
|
|
}
|
|
return $true
|
|
}
|
|
|
|
function Get-FeatureDir {
|
|
param([string]$RepoRoot, [string]$Branch)
|
|
Join-Path $RepoRoot "specs/$Branch"
|
|
}
|
|
|
|
function Get-FeaturePathsEnv {
|
|
$repoRoot = Get-RepoRoot
|
|
$currentBranch = Get-CurrentBranch
|
|
$hasGit = Test-HasGit
|
|
$featureDir = Get-FeatureDir -RepoRoot $repoRoot -Branch $currentBranch
|
|
|
|
[PSCustomObject]@{
|
|
REPO_ROOT = $repoRoot
|
|
CURRENT_BRANCH = $currentBranch
|
|
HAS_GIT = $hasGit
|
|
FEATURE_DIR = $featureDir
|
|
FEATURE_SPEC = Join-Path $featureDir 'spec.md'
|
|
IMPL_PLAN = Join-Path $featureDir 'plan.md'
|
|
TASKS = Join-Path $featureDir 'tasks.md'
|
|
RESEARCH = Join-Path $featureDir 'research.md'
|
|
DATA_MODEL = Join-Path $featureDir 'data-model.md'
|
|
QUICKSTART = Join-Path $featureDir 'quickstart.md'
|
|
CONTRACTS_DIR = Join-Path $featureDir 'contracts'
|
|
}
|
|
}
|
|
|
|
function Test-FileExists {
|
|
param([string]$Path, [string]$Description)
|
|
if (Test-Path -Path $Path -PathType Leaf) {
|
|
Write-Output " ✓ $Description"
|
|
return $true
|
|
} else {
|
|
Write-Output " ✗ $Description"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Test-DirHasFiles {
|
|
param([string]$Path, [string]$Description)
|
|
if ((Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer } | Select-Object -First 1)) {
|
|
Write-Output " ✓ $Description"
|
|
return $true
|
|
} else {
|
|
Write-Output " ✗ $Description"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# 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)
|
|
function Resolve-Template {
|
|
param(
|
|
[Parameter(Mandatory=$true)][string]$TemplateName,
|
|
[Parameter(Mandatory=$true)][string]$RepoRoot
|
|
)
|
|
|
|
$base = Join-Path $RepoRoot '.specify/templates'
|
|
|
|
# Priority 1: Project overrides
|
|
$override = Join-Path $base "overrides/$TemplateName.md"
|
|
if (Test-Path $override) { return $override }
|
|
|
|
# Priority 2: Installed presets (sorted by priority from .registry)
|
|
$presetsDir = Join-Path $RepoRoot '.specify/presets'
|
|
if (Test-Path $presetsDir) {
|
|
$registryFile = Join-Path $presetsDir '.registry'
|
|
$sortedPresets = @()
|
|
if (Test-Path $registryFile) {
|
|
try {
|
|
$registryData = Get-Content $registryFile -Raw | ConvertFrom-Json
|
|
$presets = $registryData.presets
|
|
if ($presets) {
|
|
$sortedPresets = $presets.PSObject.Properties |
|
|
Sort-Object { if ($null -ne $_.Value.priority) { $_.Value.priority } else { 10 } } |
|
|
ForEach-Object { $_.Name }
|
|
}
|
|
} catch {
|
|
# Fallback: alphabetical directory order
|
|
$sortedPresets = @()
|
|
}
|
|
}
|
|
|
|
if ($sortedPresets.Count -gt 0) {
|
|
foreach ($presetId in $sortedPresets) {
|
|
$candidate = Join-Path $presetsDir "$presetId/templates/$TemplateName.md"
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
} else {
|
|
# Fallback: alphabetical directory order
|
|
foreach ($preset in Get-ChildItem -Path $presetsDir -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -notlike '.*' }) {
|
|
$candidate = Join-Path $preset.FullName "templates/$TemplateName.md"
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
}
|
|
}
|
|
|
|
# Priority 3: Extension-provided templates
|
|
$extDir = Join-Path $RepoRoot '.specify/extensions'
|
|
if (Test-Path $extDir) {
|
|
foreach ($ext in Get-ChildItem -Path $extDir -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -notlike '.*' } | Sort-Object Name) {
|
|
$candidate = Join-Path $ext.FullName "templates/$TemplateName.md"
|
|
if (Test-Path $candidate) { return $candidate }
|
|
}
|
|
}
|
|
|
|
# Priority 4: Core templates
|
|
$core = Join-Path $base "$TemplateName.md"
|
|
if (Test-Path $core) { return $core }
|
|
|
|
return $null
|
|
}
|
|
|