#!/usr/bin/env bash # ============================================================================= # lib/scaffold.sh # Directory structure creation and template rendering engine. # ============================================================================= # Canonical directory layout lives in one place (lib/structure.sh). source "$(dirname "${BASH_SOURCE[0]}")/structure.sh" render_template() { local template_file="$1" local output_file="$2" [[ ! -f "$template_file" ]] && { error "Template not found: ${template_file}"; return 1; } local content content=$(<"$template_file") # HARDENING: collapse any “spaced” placeholders from a formatter # { { KEY } } -> {{KEY}} (KEY = UPPERCASE/underscore) # Defense in depth: if Prettier or a copy-paste breaks the syntax again, # the scaffold fixes itself. sed is a core utility (like tr/date already used here). content=$(sed -E 's/\{[[:space:]]*\{[[:space:]]*([A-Z_]+)[[:space:]]*\}[[:space:]]*\}/{{\1}}/g' <<<"$content") # Defaults (:-) so master-repo templates render even when GENOME_* are unset # (scaffold_master runs before any genome; set -u would otherwise abort here). local genome_name_upper genome_name_upper=$(tr '[:lower:]' '[:upper:]' <<< "${GENOME_NAME:-}") # Placeholder replacement content="${content//\{\{GENOME_NAME\}\}/${GENOME_NAME:-}}" content="${content//\{\{GENOME_NAME_UPPER\}\}/${genome_name_upper}}" content="${content//\{\{GENOME_DESC\}\}/${GENOME_DESC:-}}" content="${content//\{\{FORGEJO_URL\}\}/${FORGEJO_URL:-}}" content="${content//\{\{FORGEJO_USER\}\}/${FORGEJO_USER:-}}" content="${content//\{\{VAULTWARDEN_URL\}\}/${VAULTWARDEN_URL:-}}" content="${content//\{\{MASTER_REPO\}\}/${MASTER_REPO:-}}" # linked project reference (optional) — empty registry field renders as 'none' content="${content//\{\{LINKED_PROJECT\}\}/${GENOME_LINKED:-none}}" content="${content//\{\{DATE\}\}/$(date +%Y-%m-%d)}" mkdir -p "$(dirname "$output_file")" printf '%s\n' "$content" > "$output_file" } scaffold_genome() { local base="$1" info "Building directory structure in ${base}..." for dir in "${GENOME_DIRS[@]}"; do mkdir -p "${base}/${dir}" touch "${base}/${dir}/.gitkeep" done # Core templates render_template "${TEMPLATES_DIR}/gitattributes" "${base}/.gitattributes" render_template "${TEMPLATES_DIR}/gitignore" "${base}/.gitignore" render_template "${TEMPLATES_DIR}/agents-genome.md" "${base}/AGENTS.md" render_template "${TEMPLATES_DIR}/wiki-index.md" "${base}/wiki/index.md" render_template "${TEMPLATES_DIR}/wiki-log.md" "${base}/wiki/log.md" success "Scaffold completed for genome: ${GENOME_NAME}" } install_precommit_hook() { local repo_path="$1" local hooks_dir hooks_dir="$(git -C "$repo_path" rev-parse --git-path hooks)" local hook_path="${hooks_dir}/pre-commit" mkdir -p "$hooks_dir" cp "${TEMPLATES_DIR}/pre-commit.sh" "$hook_path" chmod +x "$hook_path" success "Pre-commit security hook installed at: $hook_path" } scaffold_master() { local base="$1" render_template "${TEMPLATES_DIR}/agents-master.md" "${base}/AGENTS.md" render_template "${TEMPLATES_DIR}/readme-master.md" "${base}/README.md" success "Master repository scaffold completed." }