Skip to content

Commit a067d4c

Browse files
Copilotmnriem
andauthored
feat(presets): Composition strategies (prepend, append, wrap) for templates, commands, and scripts (#2133)
* fix: rebase onto upstream/main, resolve conflicts with PR #2189 upstream/main merged PR #2189 (wrap-only strategy) which overlaps with our comprehensive composition strategies (prepend/append/wrap). Resolved conflicts keeping our implementation as source of truth: - README: keep our future considerations (composition is now fully implemented, not a future item) - presets.py: keep our composition architecture (_reconcile_composed_commands, collect_all_layers, resolve_content) while preserving #2189's _substitute_core_template which is used by agents.py for skill generation - tests: keep both test sets (our composition tests + #2189's wrap tests), removed TestReplayWrapsForCommand and TestInstallRemoveWrapLifecycle which test the superseded _replay_wraps_for_command API; our composition tests cover equivalent scenarios - Restored missing _unregister_commands call in remove() that was lost during #2189 merge * fix: re-create skill directory in _reconcile_skills after removal After _unregister_skills removes a skill directory, _register_skills skips writing because the dir no longer passes the is_dir() check. Fix by ensuring the skill subdirectory exists before calling _register_skills so the next winning preset's content gets registered. Fixes the Claude E2E failure where removing a top-priority override preset left skill-based agents without any SKILL.md file. * fix: address twenty-third round of Copilot PR review feedback - Protect reconciliation in remove(): wrap _reconcile_composed_commands and _reconcile_skills in try/except so failures emit a warning instead of leaving the project in an inconsistent state - Protect reconciliation in install(): same pattern for post-install reconciliation so partial installs don't lack cleanup - Inherit scripts/agent_scripts from base frontmatter: when composing commands, merge scripts and agent_scripts keys from the base command's frontmatter into the top layer's frontmatter if missing, preventing composed commands from losing required script references - Add tier-5 bundled core fallback to collect_all_layers(): check the bundled core_pack (wheel) or repo-root templates (source checkout) when .specify/templates/ doesn't contain the core file, matching resolve()'s tier-5 fallback so composition can always find a base layer * fix: address twenty-fourth round of Copilot PR review feedback - Use yaml.safe_load for frontmatter parsing in resolve_content instead of CommandRegistrar.parse_frontmatter which uses naive find('---',3); strip strategy key from final frontmatter to prevent leaking internal composition directives into rendered agent command files - Filter _reconcile_skills to specific commands: use _FilteredManifest wrapper so only the commands being reconciled get their skills updated, preventing accidental overwrites of other commands' skills that may be owned by higher-priority presets * fix: address twenty-fifth round of Copilot PR review feedback - Support legacy command-frontmatter strategy: when preset.yml doesn't declare a strategy, check the command file's YAML frontmatter for strategy: wrap as a fallback so legacy wrap presets participate in composition and multi-preset chaining - Guard skill dir creation in _reconcile_skills: only re-create the skill directory if the skill was previously managed (listed in some preset's registered_skills), avoiding creation of new skill dirs that _register_skills would normally skip * fix: add explanatory comment to empty except in legacy frontmatter parsing * fix: address twenty-sixth round of Copilot PR review feedback - Unregister stale commands when composition fails: when resolve_content returns None during reconciliation (base layer removed), unregister the command from non-skill agents and emit a warning - Load extension aliases during reconciliation: _register_command_from_path now checks extension.yml for aliases when the winning layer is an extension, so alias files are restored after preset removal - Use line-based fence detection for legacy frontmatter strategy fallback: scan for --- on its own line instead of split('---',2) to avoid mis-parsing YAML values containing --- * fix: address twenty-seventh round of Copilot PR review feedback - Handle non-preset winners in _reconcile_skills: when the winning layer is core/extension/project-override, restore skills via _unregister_skills so skill-based agents stay consistent with the priority stack - Update base_frontmatter_text on replace layers: when a higher-priority replace layer occurs during composition, update both top and base frontmatter so scripts/agent_scripts inheritance reflects the effective base beneath the top composed layer * fix: address twenty-eighth round of Copilot PR review feedback - Parse only interior lines in _parse_fm_yaml: use lines[1:-1] instead of filtering all --- lines, preventing corruption when YAML values contain a line that is exactly --- - Omit empty frontmatter: skip re-rendering when top_fm is empty dict to avoid emitting ---/{}/--- for intentionally empty frontmatter - Update scaffold wrap comment: mention both {CORE_TEMPLATE} and $CORE_SCRIPT placeholders for templates/commands vs scripts - Clarify shell composition scope in ARCHITECTURE.md: note that bash/PS1 resolve_template_content only handles templates; command/script composition is handled by the Python resolver * fix: address twenty-ninth round of Copilot PR review feedback - Fix TestCollectAllLayers docstring: reference collect_all_layers() - Add default/unknown strategy handling in bash/PS1 composition: error on unrecognized strategy values instead of silently skipping - Fix comment: .composed/ is a persistent dir, not temporary - Fix comment: legacy fallback checks all valid strategies, not just wrap - Cache PresetRegistry in _reconcile_skills: build presets_by_priority once instead of constructing registry per-command * fix: address thirtieth round of Copilot PR review feedback - Guard legacy frontmatter fallback: only check command file frontmatter for strategy when the manifest entry doesn't explicitly include the strategy key, preventing override of manifest-declared strategies - Document rollback limitation: note that mid-registration failures may leave orphaned agent command files since partial progress isn't captured by the local vars * fix: handle project override skills and extension context in reconciliation * fix: add comment to empty except in extension registration fallback * fix: filter extension commands in reconciliation and fix type annotation * fix: filter extension commands from post-install reconciliation Apply the same extension-installed check used in _register_commands to the reconciliation command list, preventing reconciliation from registering commands for extensions that are not installed. * fix: skip convention fallback for explicit file paths and add stem fallback to tier-5 When a preset manifest provides an explicit file path that does not exist, skip the convention-based fallback to avoid masking typos. Also add speckit.<stem> to <stem>.md fallback in tier-5 bundled/source core lookup for consistency with tier-4. * fix: scan past non-replace layers to find base in resolve_content The base-finding scan now skips non-replace layers below a replace layer instead of stopping at the first non-replace. This fixes the case where a low-priority append/prepend layer sits below a replace that should serve as the base for composition. * fix: add context_note to non-skill agent registration for extensions Add context_note parameter to register_commands_for_non_skill_agents and pass extension name/id during reconciliation so rendered command files preserve the extension-specific context markers. * fix: Optional type, rollback safety, and override skill restoration - Fix context_note type to Optional[str] - Wrap shutil.rmtree in try/except during install rollback - Separate override-backed skills from core/extension in _reconcile_skills * fix: align bash/PS1 base-finding with Python resolver Rewrite bash and PowerShell composition loops to find the effective base replace layer first (scanning bottom-up, skipping non-replace layers below it), then compose only from the base upward. This prevents evaluation of irrelevant lower layers (e.g. a wrap with no placeholder below a replace) and matches resolve_content behavior. * fix: PS1 no-python warning, integration hook for override skills, alias cleanup - Warn when no Python 3 found in PS1 and presets use composition strategies - Apply post_process_skill_content integration hook when restoring override-backed skills so agent-specific flags are preserved - Unregister command aliases alongside primary name when composition fails to prevent orphaned alias files * fix: include aliases in removed_cmd_names during preset removal Read aliases from preset manifest before deleting pack_dir so alias command files are included in unregistration and reconciliation. * fix: add comment to empty except in alias extraction during removal * fix: scan top-down for effective base in all resolvers Change base-finding to scan from highest priority downward to find the nearest replace layer, then compose only layers above it. Prevents evaluation of irrelevant lower layers (e.g. a wrap without placeholder below a higher-priority replace) across Python, bash, and PowerShell. * fix: align CLI composition chain display with top-down base-finding Show only contributing layers (base and above) in preset resolve output, matching resolve_content top-down semantics. Layers below the effective base are omitted since they do not contribute. * fix: guard corrupted registry entries and make manifest authoritative - Add isinstance(meta, dict) guard in bash registry parsing so corrupted entries are skipped instead of breaking priority ordering - Only use convention-based file lookup when the manifest does not list the requested template, making preset.yml authoritative and preventing stray on-disk files from creating unintended layers * fix: align resolve() with manifest file paths and match extension context_note - Update resolve() preset tier to consult manifest file paths before convention-based lookup, matching collect_all_layers behavior - Use exact extension context_note format matching extensions.CommandRegistrar - Update test to declare template in manifest (authoritative manifest) * revert: restore resolve() convention-based behavior for backwards compatibility resolve() is the existing public API used by shell scripts and other callers. Changing it to manifest-authoritative breaks backward compat for presets that rely on convention-based file lookup. Only the new collect_all_layers/resolve_content path uses manifest-authoritative logic. * fix: only pre-compose when this preset is the top composing layer Skip composition in _register_commands when a higher-priority replace layer already wins for the command. Register the raw file instead and let reconciliation write the correct final content. * fix: deduplicate PyYAML warnings and use self.registry in reconciliation - Emit PyYAML-missing warning once per function call in bash/PS1 instead of per-preset to avoid spamming stderr - Use self.registry.list_by_priority() in reconciliation methods instead of constructing new PresetRegistry instances to avoid redundant I/O and potential consistency issues * fix: document strategy handling consistency between layers and registrar Composed output already strips strategy from frontmatter (resolve_content pops it). Raw file registration preserves legacy frontmatter strategy for backward compat; reconciliation corrects the final state. * fix: correct stale comments for alias tracking and base-finding algorithm * security: validate manifest file paths in bash/PowerShell resolvers Reject absolute paths and parent directory traversal (..) in the manifest-declared file field before joining with the preset directory. Matches the Python-side validation in PresetManifest._validate(). --------- Co-authored-by: Manfred Riem <15701806+mnriem@users.noreply.github.com>
1 parent 8fefd2a commit a067d4c

9 files changed

Lines changed: 2299 additions & 856 deletions

File tree

presets/ARCHITECTURE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ The resolution is implemented three times to ensure consistency:
4141
- **Bash**: `resolve_template()` in `scripts/bash/common.sh`
4242
- **PowerShell**: `Resolve-Template` in `scripts/powershell/common.ps1`
4343

44+
### Composition Strategies
45+
46+
Templates, commands, and scripts support a `strategy` field that controls how a preset's content is combined with lower-priority content instead of fully replacing it:
47+
48+
| Strategy | Description | Templates | Commands | Scripts |
49+
|----------|-------------|-----------|----------|---------|
50+
| `replace` (default) | Fully replaces lower-priority content ||||
51+
| `prepend` | Places content before lower-priority content (separated by a blank line) ||||
52+
| `append` | Places content after lower-priority content (separated by a blank line) ||||
53+
| `wrap` | Content contains `{CORE_TEMPLATE}` (templates/commands) or `$CORE_SCRIPT` (scripts) placeholder replaced with lower-priority content ||||
54+
55+
Composition is recursive — multiple composing presets chain. The `PresetResolver.resolve_content()` method walks the full priority stack bottom-up and applies each layer's strategy.
56+
57+
Content resolution functions for composition:
58+
- **Python**: `PresetResolver.resolve_content()` in `src/specify_cli/presets.py` (templates, commands, and scripts)
59+
- **Bash**: `resolve_template_content()` in `scripts/bash/common.sh` (templates only; command/script composition is handled by the Python resolver)
60+
- **PowerShell**: `Resolve-TemplateContent` in `scripts/powershell/common.ps1` (templates only; command/script composition is handled by the Python resolver)
61+
4462
## Command Registration
4563

4664
When a preset is installed with `type: "command"` entries, the `PresetManager` registers them into all detected agent directories using the shared `CommandRegistrar` from `src/specify_cli/agents.py`.

presets/README.md

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,37 @@ specify preset add healthcare-compliance --priority 5 # overrides enterprise-sa
6161
specify preset add pm-workflow --priority 1 # overrides everything
6262
```
6363

64-
Presets **override**, they don't merge. If two presets both provide `spec-template`, the one with the lowest priority number wins entirely.
64+
Presets **override by default**, they don't merge. If two presets both provide `spec-template` with the default `replace` strategy, the one with the lowest priority number wins entirely. However, presets can use **composition strategies** to augment rather than replace content.
65+
66+
### Composition Strategies
67+
68+
Presets can declare a `strategy` per template to control how content is combined. The `name` field identifies which template to compose with in the priority stack, while `file` points to the actual content file (which can differ from the convention path `templates/<name>.md`):
69+
70+
```yaml
71+
provides:
72+
templates:
73+
- type: "template"
74+
name: "spec-template"
75+
file: "templates/spec-addendum.md"
76+
strategy: "append" # adds content after the core template
77+
```
78+
79+
| Strategy | Description |
80+
|----------|-------------|
81+
| `replace` (default) | Fully replaces the lower-priority template |
82+
| `prepend` | Places content **before** the resolved lower-priority template, separated by a blank line |
83+
| `append` | Places content **after** the resolved lower-priority template, separated by a blank line |
84+
| `wrap` | Content contains `{CORE_TEMPLATE}` placeholder (or `$CORE_SCRIPT` for scripts) replaced with the lower-priority content |
85+
86+
**Supported combinations:**
87+
88+
| Type | `replace` | `prepend` | `append` | `wrap` |
89+
|------|-----------|-----------|----------|--------|
90+
| **template** | ✓ (default) | ✓ | ✓ | ✓ |
91+
| **command** | ✓ (default) | ✓ | ✓ | ✓ |
92+
| **script** | ✓ (default) | — | — | ✓ |
93+
94+
Multiple composing presets chain recursively. For example, a security preset with `prepend` and a compliance preset with `append` will produce: security header + core content + compliance footer.
6595

6696
## Catalog Management
6797

@@ -108,13 +138,5 @@ See [scaffold/](scaffold/) for a scaffold you can copy to create your own preset
108138

109139
The following enhancements are under consideration for future releases:
110140

111-
- **Composition strategies** — Allow presets to declare a `strategy` per template instead of the default `replace`:
112-
113-
| Type | `replace` | `prepend` | `append` | `wrap` |
114-
|------|-----------|-----------|----------|--------|
115-
| **template** | ✓ (default) ||||
116-
| **command** | ✓ (default) ||||
117-
| **script** | ✓ (default) ||||
118-
119-
For artifacts and commands (which are LLM directives), `wrap` injects preset content before and after the core template using a `{CORE_TEMPLATE}` placeholder (implemented). For scripts, `wrap` would run custom logic before/after the core script via a `$CORE_SCRIPT` variable (not yet implemented).
120-
- **Script overrides** — Enable presets to provide alternative versions of core scripts (e.g. `create-new-feature.sh`) for workflow customization. A `strategy: "wrap"` option could allow presets to run custom logic before/after the core script without fully replacing it.
141+
- **Structural merge strategies** — Parsing Markdown sections for per-section granularity (e.g., "replace only ## Security").
142+
- **Conflict detection** — `specify preset lint` / `specify preset doctor` for detecting composition conflicts.

presets/scaffold/preset.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ provides:
3232
templates:
3333
# CUSTOMIZE: Define your template overrides
3434
# Templates are document scaffolds (spec-template.md, plan-template.md, etc.)
35+
#
36+
# Strategy options (optional, defaults to "replace"):
37+
# replace - Fully replaces the lower-priority template (default)
38+
# prepend - Places this content BEFORE the lower-priority template
39+
# append - Places this content AFTER the lower-priority template
40+
# wrap - Uses {CORE_TEMPLATE} placeholder (templates/commands) or
41+
# $CORE_SCRIPT placeholder (scripts), replaced with lower-priority content
42+
#
43+
# Note: Scripts only support "replace" and "wrap" strategies.
3544
- type: "template"
3645
name: "spec-template"
3746
file: "templates/spec-template.md"
@@ -45,6 +54,26 @@ provides:
4554
# description: "Custom plan template"
4655
# replaces: "plan-template"
4756

57+
# COMPOSITION EXAMPLES:
58+
# The `file` field points to the content file (can differ from the
59+
# convention path `templates/<name>.md`). The `name` field identifies
60+
# which template to compose with in the priority stack.
61+
#
62+
# Append additional sections to an existing template:
63+
# - type: "template"
64+
# name: "spec-template"
65+
# file: "templates/spec-addendum.md"
66+
# description: "Add compliance section to spec template"
67+
# strategy: "append"
68+
#
69+
# Wrap a command with preamble/sign-off:
70+
# - type: "command"
71+
# name: "speckit.specify"
72+
# file: "commands/specify-wrapper.md"
73+
# description: "Wrap specify command with compliance checks"
74+
# strategy: "wrap"
75+
# # In the wrapper file, use {CORE_TEMPLATE} where the original content goes
76+
4877
# OVERRIDE EXTENSION TEMPLATES:
4978
# Presets sit above extensions in the resolution stack, so you can
5079
# override templates provided by any installed extension.

scripts/bash/common.sh

Lines changed: 225 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,9 @@ try:
320320
with open(os.environ['SPECKIT_REGISTRY']) as f:
321321
data = json.load(f)
322322
presets = data.get('presets', {})
323-
for pid, meta in sorted(presets.items(), key=lambda x: x[1].get('priority', 10)):
324-
print(pid)
323+
for pid, meta in sorted(presets.items(), key=lambda x: x[1].get('priority', 10) if isinstance(x[1], dict) else 10):
324+
if isinstance(meta, dict) and meta.get('enabled', True) is not False:
325+
print(pid)
325326
except Exception:
326327
sys.exit(1)
327328
" 2>/dev/null); then
@@ -373,3 +374,225 @@ except Exception:
373374
return 1
374375
}
375376

377+
# Resolve a template name to composed content using composition strategies.
378+
# Reads strategy metadata from preset manifests and composes content
379+
# from multiple layers using prepend, append, or wrap strategies.
380+
#
381+
# Usage: CONTENT=$(resolve_template_content "template-name" "$REPO_ROOT")
382+
# Returns composed content string on stdout; exit code 1 if not found.
383+
resolve_template_content() {
384+
local template_name="$1"
385+
local repo_root="$2"
386+
local base="$repo_root/.specify/templates"
387+
388+
# Collect all layers (highest priority first)
389+
local -a layer_paths=()
390+
local -a layer_strategies=()
391+
392+
# Priority 1: Project overrides (always "replace")
393+
local override="$base/overrides/${template_name}.md"
394+
if [ -f "$override" ]; then
395+
layer_paths+=("$override")
396+
layer_strategies+=("replace")
397+
fi
398+
399+
# Priority 2: Installed presets (sorted by priority from .registry)
400+
local presets_dir="$repo_root/.specify/presets"
401+
if [ -d "$presets_dir" ]; then
402+
local registry_file="$presets_dir/.registry"
403+
local sorted_presets=""
404+
if [ -f "$registry_file" ] && command -v python3 >/dev/null 2>&1; then
405+
if sorted_presets=$(SPECKIT_REGISTRY="$registry_file" python3 -c "
406+
import json, sys, os
407+
try:
408+
with open(os.environ['SPECKIT_REGISTRY']) as f:
409+
data = json.load(f)
410+
presets = data.get('presets', {})
411+
for pid, meta in sorted(presets.items(), key=lambda x: x[1].get('priority', 10) if isinstance(x[1], dict) else 10):
412+
if isinstance(meta, dict) and meta.get('enabled', True) is not False:
413+
print(pid)
414+
except Exception:
415+
sys.exit(1)
416+
" 2>/dev/null); then
417+
if [ -n "$sorted_presets" ]; then
418+
local yaml_warned=false
419+
while IFS= read -r preset_id; do
420+
# Read strategy and file path from preset manifest
421+
local strategy="replace"
422+
local manifest_file=""
423+
local manifest="$presets_dir/$preset_id/preset.yml"
424+
if [ -f "$manifest" ] && command -v python3 >/dev/null 2>&1; then
425+
# Requires PyYAML; falls back to replace/convention if unavailable
426+
local result
427+
local py_stderr
428+
py_stderr=$(mktemp)
429+
result=$(SPECKIT_MANIFEST="$manifest" SPECKIT_TMPL="$template_name" python3 -c "
430+
import sys, os
431+
try:
432+
import yaml
433+
except ImportError:
434+
print('yaml_missing', file=sys.stderr)
435+
print('replace\t')
436+
sys.exit(0)
437+
try:
438+
with open(os.environ['SPECKIT_MANIFEST']) as f:
439+
data = yaml.safe_load(f)
440+
for t in data.get('provides', {}).get('templates', []):
441+
if t.get('name') == os.environ['SPECKIT_TMPL'] and t.get('type', 'template') == 'template':
442+
print(t.get('strategy', 'replace') + '\t' + t.get('file', ''))
443+
sys.exit(0)
444+
print('replace\t')
445+
except Exception:
446+
print('replace\t')
447+
" 2>"$py_stderr")
448+
local parse_status=$?
449+
if [ $parse_status -eq 0 ] && [ -n "$result" ]; then
450+
IFS=$'\t' read -r strategy manifest_file <<< "$result"
451+
strategy=$(printf '%s' "$strategy" | tr '[:upper:]' '[:lower:]')
452+
fi
453+
if [ "$yaml_warned" = false ] && grep -q 'yaml_missing' "$py_stderr" 2>/dev/null; then
454+
echo "Warning: PyYAML not available; composition strategies may be ignored" >&2
455+
yaml_warned=true
456+
fi
457+
rm -f "$py_stderr"
458+
fi
459+
# Try manifest file path first, then convention path
460+
local candidate=""
461+
if [ -n "$manifest_file" ]; then
462+
# Reject absolute paths and parent traversal
463+
case "$manifest_file" in
464+
/*|*../*|../*) manifest_file="" ;;
465+
esac
466+
fi
467+
if [ -n "$manifest_file" ]; then
468+
local mf="$presets_dir/$preset_id/$manifest_file"
469+
[ -f "$mf" ] && candidate="$mf"
470+
fi
471+
if [ -z "$candidate" ]; then
472+
local cf="$presets_dir/$preset_id/templates/${template_name}.md"
473+
[ -f "$cf" ] && candidate="$cf"
474+
fi
475+
if [ -n "$candidate" ]; then
476+
layer_paths+=("$candidate")
477+
layer_strategies+=("$strategy")
478+
fi
479+
done <<< "$sorted_presets"
480+
fi
481+
else
482+
# python3 failed — fall back to unordered directory scan (replace only)
483+
for preset in "$presets_dir"/*/; do
484+
[ -d "$preset" ] || continue
485+
local candidate="$preset/templates/${template_name}.md"
486+
if [ -f "$candidate" ]; then
487+
layer_paths+=("$candidate")
488+
layer_strategies+=("replace")
489+
fi
490+
done
491+
fi
492+
else
493+
# No python3 or registry — fall back to unordered directory scan (replace only)
494+
for preset in "$presets_dir"/*/; do
495+
[ -d "$preset" ] || continue
496+
local candidate="$preset/templates/${template_name}.md"
497+
if [ -f "$candidate" ]; then
498+
layer_paths+=("$candidate")
499+
layer_strategies+=("replace")
500+
fi
501+
done
502+
fi
503+
fi
504+
505+
# Priority 3: Extension-provided templates (always "replace")
506+
local ext_dir="$repo_root/.specify/extensions"
507+
if [ -d "$ext_dir" ]; then
508+
for ext in "$ext_dir"/*/; do
509+
[ -d "$ext" ] || continue
510+
case "$(basename "$ext")" in .*) continue;; esac
511+
local candidate="$ext/templates/${template_name}.md"
512+
if [ -f "$candidate" ]; then
513+
layer_paths+=("$candidate")
514+
layer_strategies+=("replace")
515+
fi
516+
done
517+
fi
518+
519+
# Priority 4: Core templates (always "replace")
520+
local core="$base/${template_name}.md"
521+
if [ -f "$core" ]; then
522+
layer_paths+=("$core")
523+
layer_strategies+=("replace")
524+
fi
525+
526+
local count=${#layer_paths[@]}
527+
[ "$count" -eq 0 ] && return 1
528+
529+
# Check if any layer uses a non-replace strategy
530+
local has_composition=false
531+
for s in "${layer_strategies[@]}"; do
532+
[ "$s" != "replace" ] && has_composition=true && break
533+
done
534+
535+
# If the top (highest-priority) layer is replace, it wins entirely —
536+
# lower layers are irrelevant regardless of their strategies.
537+
if [ "${layer_strategies[0]}" = "replace" ]; then
538+
cat "${layer_paths[0]}"
539+
return 0
540+
fi
541+
542+
if [ "$has_composition" = false ]; then
543+
cat "${layer_paths[0]}"
544+
return 0
545+
fi
546+
547+
# Find the effective base: scan from highest priority (index 0) downward
548+
# to find the nearest replace layer. Only compose layers above that base.
549+
local base_idx=-1
550+
local i
551+
for (( i=0; i<count; i++ )); do
552+
if [ "${layer_strategies[$i]}" = "replace" ]; then
553+
base_idx=$i
554+
break
555+
fi
556+
done
557+
558+
if [ $base_idx -lt 0 ]; then
559+
return 1 # no base layer found
560+
fi
561+
562+
# Read the base content; compose layers above the base (higher priority)
563+
local content
564+
content=$(cat "${layer_paths[$base_idx]}"; printf x)
565+
content="${content%x}"
566+
567+
for (( i=base_idx-1; i>=0; i-- )); do
568+
local path="${layer_paths[$i]}"
569+
local strat="${layer_strategies[$i]}"
570+
local layer_content
571+
# Preserve trailing newlines
572+
layer_content=$(cat "$path"; printf x)
573+
layer_content="${layer_content%x}"
574+
575+
case "$strat" in
576+
replace) content="$layer_content" ;;
577+
prepend) content="$(printf '%s\n\n%s' "$layer_content" "$content")" ;;
578+
append) content="$(printf '%s\n\n%s' "$content" "$layer_content")" ;;
579+
wrap)
580+
case "$layer_content" in
581+
*'{CORE_TEMPLATE}'*) ;;
582+
*) echo "Error: wrap strategy missing {CORE_TEMPLATE} placeholder" >&2; return 1 ;;
583+
esac
584+
while [[ "$layer_content" == *'{CORE_TEMPLATE}'* ]]; do
585+
local before="${layer_content%%\{CORE_TEMPLATE\}*}"
586+
local after="${layer_content#*\{CORE_TEMPLATE\}}"
587+
layer_content="${before}${content}${after}"
588+
done
589+
content="$layer_content"
590+
;;
591+
*) echo "Error: unknown strategy '$strat'" >&2; return 1 ;;
592+
esac
593+
done
594+
595+
printf '%s' "$content"
596+
return 0
597+
}
598+

0 commit comments

Comments
 (0)