|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @return array<string, int> |
| 7 | + */ |
| 8 | +function snapshot(string $sourceDir): array |
| 9 | +{ |
| 10 | + $files = []; |
| 11 | + $iterator = new RecursiveIteratorIterator( |
| 12 | + new RecursiveDirectoryIterator($sourceDir, FilesystemIterator::SKIP_DOTS), |
| 13 | + ); |
| 14 | + |
| 15 | + /** @var SplFileInfo $file */ |
| 16 | + foreach ($iterator as $file) { |
| 17 | + $path = substr($file->getPathname(), strlen($sourceDir)); |
| 18 | + |
| 19 | + if ( |
| 20 | + str_starts_with($path, '_static/') |
| 21 | + || str_starts_with($path, 'images/') |
| 22 | + || str_ends_with($path, '.rst_') |
| 23 | + ) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + $files[$path] = $file->getMTime(); |
| 28 | + } |
| 29 | + |
| 30 | + return $files; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * The page that a changed source file belongs to: itself for an RST file, |
| 35 | + * the page named by the folder for a sample. |
| 36 | + */ |
| 37 | +function owning_page(string $path, string $sourceDir): ?string |
| 38 | +{ |
| 39 | + if (str_ends_with($path, '.rst')) { |
| 40 | + return substr($path, 0, -4); |
| 41 | + } |
| 42 | + |
| 43 | + for ($directory = dirname($path); $directory !== '.'; $directory = dirname($directory)) { |
| 44 | + if (is_file($sourceDir . $directory . '.rst')) { |
| 45 | + return $directory; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return null; |
| 50 | +} |
| 51 | + |
| 52 | +function convert(string $page = ''): bool |
| 53 | +{ |
| 54 | + $start = microtime(true); |
| 55 | + |
| 56 | + exec( |
| 57 | + sprintf( |
| 58 | + 'php %s %s 2>&1', |
| 59 | + escapeshellarg(__DIR__ . '/convert.php'), |
| 60 | + $page === '' ? '' : escapeshellarg($page), |
| 61 | + ), |
| 62 | + $output, |
| 63 | + $exitCode, |
| 64 | + ); |
| 65 | + |
| 66 | + printf( |
| 67 | + "[%s] %s %s (%.1fs)\n", |
| 68 | + date('H:i:s'), |
| 69 | + $exitCode === 0 ? 'Converted' : 'Failed', |
| 70 | + $page === '' ? 'the whole guide' : $page, |
| 71 | + microtime(true) - $start, |
| 72 | + ); |
| 73 | + |
| 74 | + foreach ($output as $line) { |
| 75 | + if ($exitCode !== 0 || preg_match('/warning|not found/i', $line)) { |
| 76 | + echo ' ', $line, "\n"; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $exitCode === 0; |
| 81 | +} |
| 82 | + |
| 83 | +function main(): void |
| 84 | +{ |
| 85 | + $sourceDir = rtrim((string) getenv('DOCS_PROJECT_PATH'), '/') . '/'; |
| 86 | + $wholeGuideThreshold = 15; |
| 87 | + |
| 88 | + if (!is_dir($sourceDir)) { |
| 89 | + fwrite(STDERR, "Set DOCS_PROJECT_PATH to the user guide source.\n"); |
| 90 | + exit(1); |
| 91 | + } |
| 92 | + |
| 93 | + $known = snapshot($sourceDir); |
| 94 | + |
| 95 | + printf("Watching %s\n", $sourceDir); |
| 96 | + |
| 97 | + while (true) { |
| 98 | + usleep(500000); |
| 99 | + |
| 100 | + if (snapshot($sourceDir) === $known) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + // An editor or `git stash pop` may still be writing files. |
| 105 | + usleep(300000); |
| 106 | + |
| 107 | + $current = snapshot($sourceDir); |
| 108 | + $changed = array_keys(array_diff_assoc($current, $known) + array_diff_assoc($known, $current)); |
| 109 | + $addedOrRemoved = array_keys(array_diff_key($current, $known) + array_diff_key($known, $current)); |
| 110 | + $known = $current; |
| 111 | + |
| 112 | + $pages = array_unique(array_filter(array_map( |
| 113 | + fn (string $path): ?string => owning_page($path, $sourceDir), |
| 114 | + $changed, |
| 115 | + ))); |
| 116 | + $pageListChanged = array_filter( |
| 117 | + $addedOrRemoved, |
| 118 | + fn (string $path): bool => str_ends_with($path, '.rst'), |
| 119 | + ) !== []; |
| 120 | + |
| 121 | + if ($pageListChanged || count($pages) >= $wholeGuideThreshold) { |
| 122 | + convert(); |
| 123 | + |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + foreach ($pages as $page) { |
| 128 | + convert($page); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +main(); |
0 commit comments