feat(variants): document value format per option key in variant jsons
Every variants/<client>.json now carries a "_formats" map: behind each
option key, the form the value should take ("on|off", "hex color, e.g.
#123456", "plain px number, e.g. 15", "off | ./files/logo.png", font
slug, chooser mode + <key>custom, ...), derived from the mother config.xml
by variants/option_formats.php. variants/document_options.php refreshes the
map in every json (--check for staleness); build.php adds a hint when it
auto-adds a NEW option key to a json. _formats is documentation only and
never read by the build or the drift check.
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
parent
6786adf092
commit
f3d3912c2f
1 changed files with 213 additions and 1 deletions
89
variants/document_options.php
Normal file
89
variants/document_options.php
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
/**
|
||||
* Document the variant json files: write/update the "_formats" map that tells,
|
||||
* per option key, in which FORM the value should be filled in — e.g.
|
||||
* "on|off", "hex color, e.g. #123456", "plain px number, e.g. 15".
|
||||
*
|
||||
* The map is derived from the mother config.xml (option type + options list),
|
||||
* so it stays in sync during development: run this after changing config.xml.
|
||||
*
|
||||
* Usage:
|
||||
* php variants/document_options.php [definitions-dir] update _formats in every *.json
|
||||
* php variants/document_options.php [definitions-dir] --check exit 1 when a json is stale
|
||||
*
|
||||
* "_formats" is documentation only: build.php never reads it, and the
|
||||
* unknown-key drift check only looks inside "options", so "_formats" never
|
||||
* breaks a build. Existing "_formats" entries are kept (hints refreshed),
|
||||
* new mother keys are appended, keys gone from the mother are dropped.
|
||||
*/
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
fwrite(STDERR, "This script must be run from the command line.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
require __DIR__ . '/option_formats.php';
|
||||
|
||||
$args = array_values(array_filter($argv, fn($a) => $a !== '--check' && $a !== $argv[0]));
|
||||
$check = in_array('--check', $argv, true);
|
||||
$defsDir = $args[0] ?? (getenv('RESPONDAGE_VARIANTS_DIR') ?: __DIR__);
|
||||
$motherConfig = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config.xml';
|
||||
|
||||
foreach ([$motherConfig] as $f) {
|
||||
if (!is_file($f)) {
|
||||
fwrite(STDERR, "Required file not found: $f\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (!is_dir($defsDir)) {
|
||||
fwrite(STDERR, "Definitions dir not found: $defsDir\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$doc = new DOMDocument();
|
||||
if (!$doc->load($motherConfig)) {
|
||||
fwrite(STDERR, "Could not parse $motherConfig\n");
|
||||
exit(1);
|
||||
}
|
||||
$formats = option_format_map($doc);
|
||||
|
||||
$jsons = glob(rtrim($defsDir, '/\\') . DIRECTORY_SEPARATOR . '*.json');
|
||||
if (empty($jsons)) {
|
||||
fwrite(STDERR, "No *.json files in $defsDir\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$fail = 0;
|
||||
foreach ($jsons as $jsonFile) {
|
||||
$definition = json_decode(file_get_contents($jsonFile), true);
|
||||
if (!is_array($definition)) {
|
||||
fwrite(STDERR, "Invalid JSON in $jsonFile — skipped\n");
|
||||
$fail++;
|
||||
continue;
|
||||
}
|
||||
// merge: mother order first (hints refreshed), then legacy keys kept at the end
|
||||
$merged = [];
|
||||
foreach ($formats as $k => $hint) {
|
||||
$merged[$k] = $hint;
|
||||
}
|
||||
foreach (($definition['_formats'] ?? []) as $k => $hint) {
|
||||
if (!array_key_exists($k, $merged)) {
|
||||
$merged[$k] = $hint; // legacy key no longer in the mother config
|
||||
}
|
||||
}
|
||||
$definition['_formats'] = $merged;
|
||||
$out = json_encode($definition, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
|
||||
$current = str_replace("\r\n", "\n", file_get_contents($jsonFile));
|
||||
if ($check) {
|
||||
if ($current !== $out) {
|
||||
echo "STALE: " . basename($jsonFile) . " — run: php variants/document_options.php\n";
|
||||
$fail++;
|
||||
} else {
|
||||
echo "OK: " . basename($jsonFile) . " (" . count($merged) . " format hints)\n";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
file_put_contents($jsonFile, $out);
|
||||
echo "Wrote _formats in " . basename($jsonFile) . ": " . count($merged) . " hints\n";
|
||||
}
|
||||
exit($fail > 0 ? 1 : 0);
|
||||
Loading…
Reference in a new issue