Files
sqlpage--sqlpage/scripts/generate_configuration_reference.py
2026-06-12 23:44:32 +02:00

55 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Generate the official-site configuration reference from the JSON Schema."""
import argparse
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SCHEMA_PATH = ROOT / "sqlpage/sqlpage.schema.json"
OUTPUT_PATH = ROOT / "examples/official-site/configuration.sql"
def sql(value: str) -> str:
return value.replace("'", "''")
def display_type(spec: dict) -> str:
value = spec.get("type", "")
if isinstance(value, list):
value = [item for item in value if item != "null"]
return " or ".join(value)
return value
def display_default(spec: dict) -> str:
if "default" not in spec:
return "—"
return json.dumps(spec["default"], ensure_ascii=False)
parser = argparse.ArgumentParser()
parser.add_argument("--check", action="store_true", help="fail when the checked-in reference is stale")
args = parser.parse_args()
schema = json.loads(SCHEMA_PATH.read_text())
properties = [(name, spec) for name, spec in schema["properties"].items() if name != "$schema"]
rows = "\n".join(
f"select '{sql(name)}' as option, '{sql(display_type(spec))}' as type, "
f"'{sql(display_default(spec))}' as default_value, '{sql(spec['description'])}' as description"
+ (" union all" if index < len(properties) - 1 else ";")
for index, (name, spec) in enumerate(properties)
)
content = f"""-- Generated by scripts/generate_configuration_reference.py from sqlpage/sqlpage.schema.json.
-- Do not edit this file directly.
select 'dynamic' as component, json_patch(json_extract(properties, '$[0]'), json_object(
'title', 'SQLPage configuration reference'
)) as properties from example where component = 'shell' limit 1;
select 'text' as component;
select '# SQLPage configuration reference\n\nSQLPage reads `sqlpage/sqlpage.json`. Add the [`$schema`](https://json-schema.org/understanding-json-schema/reference/schema) property to get validation and editor completion. Every option can also be supplied as an uppercase environment variable; environment variables override file values.\n\nThe [JSON Schema]({schema['$id']}) is the source of truth for this reference and SQLPage''s in-memory configuration structure.' as contents_md;
select 'table' as component, true as sort, true as search;
{rows}
"""
if args.check:
if OUTPUT_PATH.read_text() != content:
raise SystemExit(f"{OUTPUT_PATH.relative_to(ROOT)} is stale; run {Path(__file__).relative_to(ROOT)}")
else:
OUTPUT_PATH.write_text(content)