Add reusable context template
This commit is contained in:
81
scripts/render.sh
Executable file
81
scripts/render.sh
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
if [[ $# -ne 2 ]]; then
|
||||
echo "Uso: $0 config.json /ruta/destino" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
CONFIG_PATH="$1"
|
||||
DEST_PATH="$2"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
TEMPLATE_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
if [[ ! -f "$CONFIG_PATH" ]]; then
|
||||
echo "Config no encontrada: $CONFIG_PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
python3 - "$CONFIG_PATH" "$DEST_PATH" "$TEMPLATE_ROOT" <<'PY'
|
||||
import json, sys, pathlib, shutil
|
||||
|
||||
config_path = pathlib.Path(sys.argv[1]).expanduser().resolve()
|
||||
dest_root = pathlib.Path(sys.argv[2]).expanduser().resolve()
|
||||
template_root = pathlib.Path(sys.argv[3]).resolve()
|
||||
|
||||
with open(config_path) as f:
|
||||
config = json.load(f)
|
||||
|
||||
def flatten(prefix, value, store):
|
||||
if isinstance(value, dict):
|
||||
for k, v in value.items():
|
||||
flatten(f"{prefix}{k.upper()}_", v, store)
|
||||
elif isinstance(value, list):
|
||||
store[prefix[:-1]] = ", ".join(str(v) for v in value)
|
||||
else:
|
||||
store[prefix[:-1]] = str(value)
|
||||
|
||||
replacements = {}
|
||||
for key, val in config.items():
|
||||
if isinstance(val, (dict, list)):
|
||||
flatten(f"{key.upper()}_", val, replacements)
|
||||
else:
|
||||
replacements[key.upper()] = str(val)
|
||||
|
||||
def render_text(text: str) -> str:
|
||||
for key, value in replacements.items():
|
||||
text = text.replace(f"{{{{{key}}}}}", value)
|
||||
return text
|
||||
|
||||
template_paths = [
|
||||
"CULTURA-AGENTE.template.md",
|
||||
"agents.template.md",
|
||||
"memory.template.md",
|
||||
"context/README.template.md",
|
||||
"context/hosts.template.md",
|
||||
"context/traefik.template.md",
|
||||
"context/cron.template.md",
|
||||
"context/n8n.template.md",
|
||||
"context/git.template.md",
|
||||
"context/docker.template.md",
|
||||
"context/scripts.template.md",
|
||||
"context/opencode.template.md",
|
||||
"context/otros.template.md",
|
||||
]
|
||||
|
||||
for rel in template_paths:
|
||||
template = template_root / rel
|
||||
if not template.exists():
|
||||
print(f"Aviso: {rel} no existe, omitiendo", file=sys.stderr)
|
||||
continue
|
||||
relative = template.relative_to(template_root)
|
||||
target_name = relative.name.replace(".template", "")
|
||||
target_rel = relative.parent / target_name
|
||||
target_path = dest_root / target_rel
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
content = template.read_text()
|
||||
rendered = render_text(content)
|
||||
target_path.write_text(rendered)
|
||||
print(f"Renderizado {target_rel}")
|
||||
|
||||
PY
|
||||
Reference in New Issue
Block a user