build_mini_docs.php 1.1 KB

12345678910111213141516171819202122232425262728
  1. <?php
  2. declare(strict_types=1);
  3. /** Publish the canonical OpenAPI file for the static documentation page. No application bootstrap or database access. */
  4. $root = dirname(__DIR__);
  5. $source = $root . '/docs/openapi/water-mini-v1.json';
  6. $target = $root . '/public/docs/mini/openapi.json';
  7. $content = file_get_contents($source);
  8. if ($content === false) {
  9. throw new RuntimeException('Cannot read the canonical OpenAPI file.');
  10. }
  11. $specification = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
  12. if (($specification['openapi'] ?? '') !== '3.0.3' || empty($specification['paths'])) {
  13. throw new RuntimeException('Invalid mini API specification.');
  14. }
  15. if (in_array('--check', $argv, true)) {
  16. if (!is_file($target) || file_get_contents($target) !== $content) {
  17. fwrite(STDERR, "Published OpenAPI is stale. Run: php scripts/build_mini_docs.php\n");
  18. exit(1);
  19. }
  20. echo "Published OpenAPI matches the canonical file.\n";
  21. exit;
  22. }
  23. if (file_put_contents($target, $content, LOCK_EX) !== strlen($content)) {
  24. throw new RuntimeException('Cannot publish OpenAPI.');
  25. }
  26. echo "Published: /docs/mini/index.html (OpenAPI synchronized).\n";