Files
website/scripts/generate-config.sh
T
tobias 5e9da58d23 Vorlage fuer eigene Ableger-Installationen: Website + CMS-Code
Bereinigt von Pilot-Spezifika (echte Inhalte, Erzbistum-Hamburg-Bezuege,
Domain-Werte) - install.env und hugo.toml [params] mit generischen
Platzhaltern. docs/ komplett entfernt: Konzept, Manifest, Installations-
anleitungen leben jetzt auf der Projekt-Website statt redundant im Repo,
README verweist dorthin. Decap-CMS-Version von einer floatenden ^3.0.0
auf ein festes 3.15.1 gepinnt.
2026-08-23 14:37:54 +02:00

131 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
# Generates admin/config.yml from admin/config.template.yml, AND
# hugo/hugo.toml from hugo/hugo.template.toml (see below) - both share the
# same "{{VARIABLE}}" substitution step, filled from install.env.
#
# admin/config.yml specifically has a second, independent substitution:
# fields between "# VOKABULAR:<field>:START/END" markers are replaced by a
# closed selection list if a vocabulary entry for <field> exists in the
# content repo under _content/ableger.yml - otherwise the block from the
# template stays unchanged (fallback: free text as before). See the
# Manifest chapter on the project website Teil C, vokabular/README.md and
# install.env.
#
# Runs both locally and in deploy (see the installation guide on the
# project website), AFTER sync-methoden.sh (which fetches
# _content/ableger.yml in the first place). Both admin/config.yml and
# hugo/hugo.toml are generated and not versioned (see .gitignore) -
# admin/config.template.yml and hugo/hugo.template.toml are the files to
# maintain.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TEMPLATE="$ROOT/admin/config.template.yml"
OUT="$ROOT/admin/config.yml"
ABLEGER_YML="$ROOT/_content/ableger.yml"
# shellcheck source=/dev/null
source "$ROOT/install.env"
export GITEA_DOMAIN METHODEN_REPO OAUTH_CLIENT_ID WEBSITE_DOMAIN
# hugo.toml only needs the plain "{{VARIABLE}}" substitution, no VOKABULAR
# blocks - a plain sed pass is enough, no need for the Python step below.
HUGO_TEMPLATE="$ROOT/hugo/hugo.template.toml"
HUGO_OUT="$ROOT/hugo/hugo.toml"
sed -e "s|{{GITEA_DOMAIN}}|$GITEA_DOMAIN|g" \
-e "s|{{METHODEN_REPO}}|$METHODEN_REPO|g" \
-e "s|{{WEBSITE_DOMAIN}}|$WEBSITE_DOMAIN|g" \
"$HUGO_TEMPLATE" > "$HUGO_OUT"
echo "hugo.toml generiert: $HUGO_OUT"
# Export the vocabulary from ableger.yml (content repo) into a temporary
# directory with one text file per field (one allowed value per line) - this
# way the rest of the already-proven logic below (Python, reads from text
# files) stays unchanged. Ruby instead of Python for YAML parsing, since
# PyYAML isn't guaranteed to be available here, whereas Ruby's yaml library
# (standard library) is already used elsewhere in the project (see
# scripts/validate.rb).
VOKABULAR="$(mktemp -d)"
trap 'rm -rf "$VOKABULAR"' EXIT
if [ -f "$ABLEGER_YML" ]; then
ruby -ryaml -e '
daten = YAML.load_file(ARGV[0]) || {}
vokabular = daten["vokabular"] || {}
vokabular.each do |feld, werte|
File.write(File.join(ARGV[1], "#{feld}.txt"), Array(werte).join("\n") + "\n")
end
' "$ABLEGER_YML" "$VOKABULAR"
fi
python3 - "$TEMPLATE" "$OUT" "$VOKABULAR" <<'PYEOF'
import os
import re
import sys
from pathlib import Path
template_path, out_path, vokabular_dir = sys.argv[1:4]
text = Path(template_path).read_text()
vokabular_dir = Path(vokabular_dir)
# Always replace "{{VARIABLE}}" placeholders (see install.env), independent
# of the VOKABULAR mechanism below - no fallback logic needed, install.env
# (unlike _content/ableger.yml) is always versioned in this repo.
for feld in ("GITEA_DOMAIN", "METHODEN_REPO", "OAUTH_CLIENT_ID", "WEBSITE_DOMAIN"):
text = text.replace("{{" + feld + "}}", os.environ[feld])
def render_field(feld, indent, values):
lines = [f'{indent}- label: "{feld_labels.get(feld, feld)}"',
f'{indent} name: "{feld}"',
f'{indent} widget: "select"']
# ONLY multi_fields determines multi-select - "more than one option to
# choose from" (len(values) > 1) is not the same as "multiple values
# selectable at once" and must not be conflated. Found live: "lizenz"
# has 3 options, but exactly ONE applies per entry - the earlier
# condition "len(values) > 1 or feld in multi_fields" incorrectly set
# multiple: true as soon as a second vocabulary value was added. Decap
# then stored lizenz as a list ("- CC0") instead of a plain string as
# soon as someone made a selection in the CMS - unnoticed until a value
# was actually changed.
if feld in multi_fields:
lines.append(f'{indent} multiple: true')
lines.append(f'{indent} options:')
for wert in values:
lines.append(f'{indent} - "{wert}"')
return "\n".join(lines)
feld_labels = {
"zielgruppe": "Zielgruppe",
"kategorie": "Kategorie",
"lizenz": "Lizenz",
"tags": "Tags",
}
multi_fields = {"zielgruppe", "kategorie", "tags"}
def replace_block(match):
feld = match.group("feld")
indent = match.group("indent")
vokabular_file = vokabular_dir / f"{feld}.txt"
start = match.group("start")
end = match.group("end")
if not vokabular_file.exists():
return match.group(0)
values = [line.strip() for line in vokabular_file.read_text().splitlines() if line.strip()]
if not values:
return match.group(0)
block = render_field(feld, indent, values)
return f"{indent}{start}\n{block}\n{indent}{end}"
pattern = re.compile(
r'(?P<indent>[ \t]*)(?P<start># VOKABULAR:(?P<feld>\w+):START)\n'
r'.*?\n'
r'[ \t]*(?P<end># VOKABULAR:(?P=feld):END)',
re.DOTALL,
)
result = pattern.sub(replace_block, text)
Path(out_path).write_text(result)
PYEOF
echo "config.yml generiert: $OUT"