mirror of
https://github.com/github/spec-kit.git
synced 2026-07-03 12:28:06 +08:00
* feat(init)!: make git extension opt-in and remove --no-git at v0.10.0 - Remove --no-git parameter from specify init command - Remove git extension auto-installation from init flow - Git repository initialization (git init) still runs when git is available - Remove --no-git from all test invocations across the test suite - Update docs to reflect opt-in git extension behavior - Replace TestGitExtensionAutoInstall with TestGitExtensionOptIn tests BREAKING CHANGE: specify init no longer auto-installs the git extension. Use `specify extension add git` to install it explicitly. The --no-git flag has been removed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refactor(scripts): remove git operations from core scripts Git functionality is now entirely managed by the git extension. Core scripts only handle directory-based feature creation and numbering. - Remove has_git(), check_feature_branch(), git branch creation from core - Simplify number detection to use only spec directory scanning - Remove HAS_GIT output from get_feature_paths() - Remove git remote fetching and branch querying - Keep BRANCH_NAME output key for backward compatibility Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refactor: remove all git operations from core - Remove is_git_repo() and init_git_repo() dead code from _utils.py - Remove --branch-numbering from init command - Remove git from 'specify check' (now extension-only) - Update docs: git is optional prerequisite, check command description - Fix tests to reflect no-git-in-core reality (fallback to main) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refactor(scripts): remove directory scanning and branch fallback from core Core scripts now resolve feature context exclusively from: 1. SPECIFY_FEATURE env var (set by git extension) 2. .specify/feature.json (persisted by specify command) Removed find_feature_dir_by_prefix() and directory scanning heuristics — these are the git extension's responsibility. Scripts error clearly when no feature context is available. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat: introduce feature_numbering, deprecate branch_numbering in init-options - specify command template now reads feature_numbering (preferred) with fallback to branch_numbering (deprecated) from init-options.json - Git extension reads git-config.yml > feature_numbering > branch_numbering - init now writes feature_numbering: sequential to init-options.json - Deprecation warning emitted when branch_numbering is used as fallback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: remove trailing whitespace in common.ps1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(scripts): persist SPECIFY_FEATURE_DIRECTORY env var to feature.json When SPECIFY_FEATURE_DIRECTORY is set, get_feature_paths() now writes the value to .specify/feature.json so future sessions without the env var can still resolve the feature directory. The write is idempotent — it skips when the file already contains the same value. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: address review feedback — error messages and docs - Update error messages in common.sh and common.ps1 to reference SPECIFY_FEATURE_DIRECTORY instead of SPECIFY_FEATURE (which no longer resolves feature directories) - Fix get_current_branch comment (returns empty string, not error) - Update upgrade.md to reference SPECIFY_FEATURE_DIRECTORY with correct example paths - Update local-development.md troubleshooting: replace stale 'Git step skipped' row with actionable git extension guidance Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(scripts): harden feature.json persistence - Use json_escape in printf fallback when jq is unavailable (common.sh) - Replace utf8NoBOM encoding with UTF8Encoding($false) for PowerShell 5.1 compatibility (common.ps1) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refactor(scripts): remove dead feature_json_matches_feature_dir functions These guards are no longer needed since the branch-name validation they protected against has been removed from check-prerequisites. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refactor(git-ext): rename create-new-feature to create-new-feature-branch The git extension's script only creates the git branch — rename it to reflect that responsibility. The core create-new-feature.sh/.ps1 handles feature directory creation and feature.json persistence. Also includes fixes from review feedback: - common.sh: _persist_feature_json uses json_escape fallback - common.ps1: Save-FeatureJson uses UTF8Encoding for PS 5.1 compat - common.ps1: case-sensitive path stripping on non-Windows - create-new-feature.sh/ps1: output both SPECIFY_FEATURE and SPECIFY_FEATURE_DIRECTORY - setup-tasks.sh: fix stale 'Validate branch' comment Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(tests): update references to renamed git extension scripts Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(tests): remove duplicate EXT_CREATE_FEATURE assignments Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Manfred Riem <mnriem@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
517 lines
20 KiB
PowerShell
517 lines
20 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
# Common PowerShell functions analogous to common.sh
|
|
|
|
# Find repository root by searching upward for .specify directory
|
|
# This is the primary marker for spec-kit projects
|
|
function Find-SpecifyRoot {
|
|
param([string]$StartDir = (Get-Location).Path)
|
|
|
|
# Normalize to absolute path to prevent issues with relative paths
|
|
# Use -LiteralPath to handle paths with wildcard characters ([, ], *, ?)
|
|
$resolved = Resolve-Path -LiteralPath $StartDir -ErrorAction SilentlyContinue
|
|
$current = if ($resolved) { $resolved.Path } else { $null }
|
|
if (-not $current) { return $null }
|
|
|
|
while ($true) {
|
|
if (Test-Path -LiteralPath (Join-Path $current ".specify") -PathType Container) {
|
|
return $current
|
|
}
|
|
$parent = Split-Path $current -Parent
|
|
if ([string]::IsNullOrEmpty($parent) -or $parent -eq $current) {
|
|
return $null
|
|
}
|
|
$current = $parent
|
|
}
|
|
}
|
|
|
|
# Get repository root, prioritizing .specify directory
|
|
# This prevents using a parent repository when spec-kit is initialized in a subdirectory
|
|
function Get-RepoRoot {
|
|
# First, look for .specify directory (spec-kit's own marker)
|
|
$specifyRoot = Find-SpecifyRoot
|
|
if ($specifyRoot) {
|
|
return $specifyRoot
|
|
}
|
|
|
|
# Final fallback to script location
|
|
# Use -LiteralPath to handle paths with wildcard characters
|
|
return (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "../../..")).Path
|
|
}
|
|
|
|
function Get-CurrentBranch {
|
|
# Return feature name from explicit state only.
|
|
# Feature state is set by SPECIFY_FEATURE (from create-new-feature or
|
|
# the git extension) or implicitly via .specify/feature.json.
|
|
if ($env:SPECIFY_FEATURE) {
|
|
return $env:SPECIFY_FEATURE
|
|
}
|
|
|
|
# No explicit feature set - return empty to signal "unknown".
|
|
return ""
|
|
}
|
|
|
|
|
|
|
|
# Persist a feature_directory value to .specify/feature.json.
|
|
# Writes only when the file is missing or the value differs from what's stored.
|
|
function Save-FeatureJson {
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$RepoRoot,
|
|
[Parameter(Mandatory = $true)][string]$FeatureDirectory
|
|
)
|
|
|
|
# Strip repo root prefix if the value is absolute and under repo root.
|
|
# Use case-insensitive comparison on Windows only (case-sensitive filesystems elsewhere).
|
|
$prefix = $RepoRoot + [System.IO.Path]::DirectorySeparatorChar
|
|
if ($null -ne $IsWindows) { $onWin = $IsWindows } else { $onWin = $true }
|
|
if ($onWin) {
|
|
$cmp = [System.StringComparison]::OrdinalIgnoreCase
|
|
} else {
|
|
$cmp = [System.StringComparison]::Ordinal
|
|
}
|
|
if ($FeatureDirectory.StartsWith($prefix, $cmp)) {
|
|
$FeatureDirectory = $FeatureDirectory.Substring($prefix.Length)
|
|
}
|
|
|
|
$fjPath = Join-Path (Join-Path $RepoRoot '.specify') 'feature.json'
|
|
|
|
# Read current value and skip write when unchanged
|
|
if (Test-Path -LiteralPath $fjPath -PathType Leaf) {
|
|
try {
|
|
$raw = Get-Content -LiteralPath $fjPath -Raw
|
|
$cfg = $raw | ConvertFrom-Json
|
|
if ($cfg.feature_directory -eq $FeatureDirectory) {
|
|
return
|
|
}
|
|
} catch {
|
|
# File is corrupt or unreadable - overwrite it
|
|
}
|
|
}
|
|
|
|
# Ensure .specify/ directory exists
|
|
$specifyDir = Join-Path $RepoRoot '.specify'
|
|
if (-not (Test-Path -LiteralPath $specifyDir -PathType Container)) {
|
|
New-Item -ItemType Directory -Path $specifyDir -Force | Out-Null
|
|
}
|
|
|
|
# Write feature.json
|
|
$json = @{ feature_directory = $FeatureDirectory } | ConvertTo-Json -Compress
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
|
[System.IO.File]::WriteAllText($fjPath, $json, $utf8NoBom)
|
|
}
|
|
|
|
function Get-FeaturePathsEnv {
|
|
$repoRoot = Get-RepoRoot
|
|
$currentBranch = Get-CurrentBranch
|
|
|
|
# Resolve feature directory. Priority:
|
|
# 1. SPECIFY_FEATURE_DIRECTORY env var (explicit override)
|
|
# 2. .specify/feature.json "feature_directory" key (persisted by specify command)
|
|
# 3. Error - no feature context available
|
|
$featureJson = Join-Path $repoRoot '.specify/feature.json'
|
|
if ($env:SPECIFY_FEATURE_DIRECTORY) {
|
|
$featureDir = $env:SPECIFY_FEATURE_DIRECTORY
|
|
# Normalize relative paths to absolute under repo root
|
|
if (-not [System.IO.Path]::IsPathRooted($featureDir)) {
|
|
$featureDir = Join-Path $repoRoot $featureDir
|
|
}
|
|
# Persist to feature.json so future sessions without the env var still work
|
|
Save-FeatureJson -RepoRoot $repoRoot -FeatureDirectory $env:SPECIFY_FEATURE_DIRECTORY
|
|
} elseif (Test-Path $featureJson) {
|
|
$featureJsonRaw = Get-Content -LiteralPath $featureJson -Raw
|
|
try {
|
|
$featureConfig = $featureJsonRaw | ConvertFrom-Json
|
|
} catch {
|
|
[Console]::Error.WriteLine("ERROR: Failed to parse .specify/feature.json: $_")
|
|
exit 1
|
|
}
|
|
if ($featureConfig.feature_directory) {
|
|
$featureDir = $featureConfig.feature_directory
|
|
# Normalize relative paths to absolute under repo root
|
|
if (-not [System.IO.Path]::IsPathRooted($featureDir)) {
|
|
$featureDir = Join-Path $repoRoot $featureDir
|
|
}
|
|
} else {
|
|
[Console]::Error.WriteLine("ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY or ensure .specify/feature.json contains feature_directory.")
|
|
exit 1
|
|
}
|
|
} else {
|
|
[Console]::Error.WriteLine("ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY or run the specify command to create .specify/feature.json.")
|
|
exit 1
|
|
}
|
|
|
|
[PSCustomObject]@{
|
|
REPO_ROOT = $repoRoot
|
|
CURRENT_BRANCH = $currentBranch
|
|
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 " [OK] $Description"
|
|
return $true
|
|
} else {
|
|
Write-Output " [FAIL] $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 " [OK] $Description"
|
|
return $true
|
|
} else {
|
|
Write-Output " [FAIL] $Description"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Get-InvokeSeparator {
|
|
param([string]$RepoRoot = (Get-RepoRoot))
|
|
|
|
if ($null -eq $script:SpecKitInvokeSeparatorCache) {
|
|
$script:SpecKitInvokeSeparatorCache = @{}
|
|
}
|
|
if ($script:SpecKitInvokeSeparatorCache.ContainsKey($RepoRoot)) {
|
|
return $script:SpecKitInvokeSeparatorCache[$RepoRoot]
|
|
}
|
|
|
|
$separator = '.'
|
|
$integrationJson = Join-Path $RepoRoot '.specify/integration.json'
|
|
if (Test-Path -LiteralPath $integrationJson -PathType Leaf) {
|
|
try {
|
|
$state = Get-Content -LiteralPath $integrationJson -Raw | ConvertFrom-Json
|
|
$key = if ($state.default_integration) { [string]$state.default_integration } elseif ($state.integration) { [string]$state.integration } else { '' }
|
|
if ($key -and $state.integration_settings) {
|
|
$settingProperty = $state.integration_settings.PSObject.Properties[$key]
|
|
if ($settingProperty) {
|
|
$setting = $settingProperty.Value
|
|
if ($setting -and ($setting.invoke_separator -eq '.' -or $setting.invoke_separator -eq '-')) {
|
|
$separator = [string]$setting.invoke_separator
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
$separator = '.'
|
|
}
|
|
}
|
|
|
|
$script:SpecKitInvokeSeparatorCache[$RepoRoot] = $separator
|
|
return $separator
|
|
}
|
|
|
|
function Format-SpecKitCommand {
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$CommandName,
|
|
[string]$RepoRoot = (Get-RepoRoot)
|
|
)
|
|
|
|
$separator = Get-InvokeSeparator -RepoRoot $RepoRoot
|
|
$name = $CommandName.TrimStart('/')
|
|
if ($name.StartsWith('speckit.')) {
|
|
$name = $name.Substring(8)
|
|
} elseif ($name.StartsWith('speckit-')) {
|
|
$name = $name.Substring(8)
|
|
}
|
|
$name = $name -replace '\.', $separator
|
|
|
|
return "/speckit$separator$name"
|
|
}
|
|
|
|
# Find a usable Python 3 executable (python3, python, or py -3).
|
|
# Returns the command/arguments as an array, or $null if none found.
|
|
function Get-Python3Command {
|
|
if (Get-Command python3 -ErrorAction SilentlyContinue) { return @('python3') }
|
|
if (Get-Command python -ErrorAction SilentlyContinue) {
|
|
$ver = & python --version 2>&1
|
|
if ($ver -match 'Python 3') { return @('python') }
|
|
}
|
|
if (Get-Command py -ErrorAction SilentlyContinue) {
|
|
$ver = & py -3 --version 2>&1
|
|
if ($ver -match 'Python 3') { return @('py', '-3') }
|
|
}
|
|
return $null
|
|
}
|
|
|
|
# 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 |
|
|
Where-Object { $null -eq $_.Value.enabled -or $_.Value.enabled -ne $false } |
|
|
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
|
|
}
|
|
|
|
# Resolve a template name to composed content using composition strategies.
|
|
# Reads strategy metadata from preset manifests and composes content
|
|
# from multiple layers using prepend, append, or wrap strategies.
|
|
function Resolve-TemplateContent {
|
|
param(
|
|
[Parameter(Mandatory=$true)][string]$TemplateName,
|
|
[Parameter(Mandatory=$true)][string]$RepoRoot
|
|
)
|
|
|
|
$base = Join-Path $RepoRoot '.specify/templates'
|
|
|
|
# Collect all layers (highest priority first)
|
|
$layerPaths = @()
|
|
$layerStrategies = @()
|
|
|
|
# Priority 1: Project overrides (always "replace")
|
|
$override = Join-Path $base "overrides/$TemplateName.md"
|
|
if (Test-Path $override) {
|
|
$layerPaths += $override
|
|
$layerStrategies += 'replace'
|
|
}
|
|
|
|
# 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 |
|
|
Where-Object { $null -eq $_.Value.enabled -or $_.Value.enabled -ne $false } |
|
|
Sort-Object { if ($null -ne $_.Value.priority) { $_.Value.priority } else { 10 } } |
|
|
ForEach-Object { $_.Name }
|
|
}
|
|
} catch {
|
|
$sortedPresets = @()
|
|
}
|
|
}
|
|
|
|
if ($sortedPresets.Count -gt 0) {
|
|
$pyCmd = Get-Python3Command
|
|
if (-not $pyCmd) {
|
|
# Check if any preset has strategy fields that would be ignored
|
|
foreach ($pid in $sortedPresets) {
|
|
$mf = Join-Path $presetsDir "$pid/preset.yml"
|
|
if ((Test-Path $mf) -and (Select-String -Path $mf -Pattern 'strategy:' -Quiet -ErrorAction SilentlyContinue)) {
|
|
Write-Warning "No Python 3 found; preset composition strategies will be ignored"
|
|
break
|
|
}
|
|
}
|
|
}
|
|
$yamlWarned = $false
|
|
foreach ($presetId in $sortedPresets) {
|
|
# Read strategy and file path from preset manifest
|
|
$strategy = 'replace'
|
|
$manifestFilePath = ''
|
|
$manifest = Join-Path $presetsDir "$presetId/preset.yml"
|
|
if ((Test-Path $manifest) -and $pyCmd) {
|
|
try {
|
|
# Use Python to parse YAML manifest for strategy and file path
|
|
$pyArgs = if ($pyCmd.Count -gt 1) { $pyCmd[1..($pyCmd.Count-1)] } else { @() }
|
|
$pyStderrFile = [System.IO.Path]::GetTempFileName()
|
|
$stratResult = & $pyCmd[0] @pyArgs -c @"
|
|
import sys
|
|
try:
|
|
import yaml
|
|
except ImportError:
|
|
print('yaml_missing', file=sys.stderr)
|
|
print('replace\t')
|
|
sys.exit(0)
|
|
try:
|
|
with open(sys.argv[1]) as f:
|
|
data = yaml.safe_load(f)
|
|
for t in data.get('provides', {}).get('templates', []):
|
|
if t.get('name') == sys.argv[2] and t.get('type', 'template') == 'template':
|
|
print(t.get('strategy', 'replace') + '\t' + t.get('file', ''))
|
|
sys.exit(0)
|
|
print('replace\t')
|
|
except Exception:
|
|
print('replace\t')
|
|
"@ $manifest $TemplateName 2>$pyStderrFile
|
|
if ($stratResult) {
|
|
$parts = $stratResult.Trim() -split "`t", 2
|
|
$strategy = $parts[0].ToLowerInvariant()
|
|
if ($parts.Count -gt 1 -and $parts[1]) { $manifestFilePath = $parts[1] }
|
|
}
|
|
if (-not $yamlWarned -and (Test-Path $pyStderrFile) -and (Get-Content $pyStderrFile -Raw -ErrorAction SilentlyContinue) -match 'yaml_missing') {
|
|
Write-Warning "PyYAML not available; composition strategies may be ignored"
|
|
$yamlWarned = $true
|
|
}
|
|
Remove-Item $pyStderrFile -Force -ErrorAction SilentlyContinue
|
|
} catch {
|
|
$strategy = 'replace'
|
|
if ($pyStderrFile) { Remove-Item $pyStderrFile -Force -ErrorAction SilentlyContinue }
|
|
}
|
|
}
|
|
# Try manifest file path first, then convention path
|
|
$candidate = $null
|
|
if ($manifestFilePath) {
|
|
# Reject absolute paths and parent traversal
|
|
if ([System.IO.Path]::IsPathRooted($manifestFilePath) -or $manifestFilePath -match '\.\.[\\/]') {
|
|
$manifestFilePath = ''
|
|
}
|
|
}
|
|
if ($manifestFilePath) {
|
|
$mf = Join-Path $presetsDir "$presetId/$manifestFilePath"
|
|
if (Test-Path $mf) { $candidate = $mf }
|
|
}
|
|
if (-not $candidate) {
|
|
$cf = Join-Path $presetsDir "$presetId/templates/$TemplateName.md"
|
|
if (Test-Path $cf) { $candidate = $cf }
|
|
}
|
|
if ($candidate) {
|
|
$layerPaths += $candidate
|
|
$layerStrategies += $strategy
|
|
}
|
|
}
|
|
} else {
|
|
# Fallback: alphabetical directory order (no registry or parse failure)
|
|
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) {
|
|
$layerPaths += $candidate
|
|
$layerStrategies += 'replace'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Priority 3: Extension-provided templates (always "replace")
|
|
$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) {
|
|
$layerPaths += $candidate
|
|
$layerStrategies += 'replace'
|
|
}
|
|
}
|
|
}
|
|
|
|
# Priority 4: Core templates (always "replace")
|
|
$core = Join-Path $base "$TemplateName.md"
|
|
if (Test-Path $core) {
|
|
$layerPaths += $core
|
|
$layerStrategies += 'replace'
|
|
}
|
|
|
|
if ($layerPaths.Count -eq 0) { return $null }
|
|
|
|
# If the top (highest-priority) layer is replace, it wins entirely --
|
|
# lower layers are irrelevant regardless of their strategies.
|
|
if ($layerStrategies[0] -eq 'replace') {
|
|
return (Get-Content $layerPaths[0] -Raw)
|
|
}
|
|
|
|
# Check if any layer uses a non-replace strategy
|
|
$hasComposition = $false
|
|
foreach ($s in $layerStrategies) {
|
|
if ($s -ne 'replace') { $hasComposition = $true; break }
|
|
}
|
|
|
|
if (-not $hasComposition) {
|
|
return (Get-Content $layerPaths[0] -Raw)
|
|
}
|
|
|
|
# Find the effective base: scan from highest priority (index 0) downward
|
|
# to find the nearest replace layer. Only compose layers above that base.
|
|
$baseIdx = -1
|
|
for ($i = 0; $i -lt $layerPaths.Count; $i++) {
|
|
if ($layerStrategies[$i] -eq 'replace') {
|
|
$baseIdx = $i
|
|
break
|
|
}
|
|
}
|
|
if ($baseIdx -lt 0) { return $null }
|
|
|
|
$content = Get-Content $layerPaths[$baseIdx] -Raw
|
|
|
|
for ($i = $baseIdx - 1; $i -ge 0; $i--) {
|
|
$path = $layerPaths[$i]
|
|
$strat = $layerStrategies[$i]
|
|
$layerContent = Get-Content $path -Raw
|
|
|
|
switch ($strat) {
|
|
'replace' { $content = $layerContent }
|
|
'prepend' { $content = "$layerContent`n`n$content" }
|
|
'append' { $content = "$content`n`n$layerContent" }
|
|
'wrap' {
|
|
if (-not $layerContent.Contains('{CORE_TEMPLATE}')) {
|
|
throw "Wrap strategy missing {CORE_TEMPLATE} placeholder"
|
|
}
|
|
$content = $layerContent.Replace('{CORE_TEMPLATE}', $content)
|
|
}
|
|
default { throw "Unknown strategy: $strat" }
|
|
}
|
|
}
|
|
|
|
return $content
|
|
}
|