|
31 | 31 |
|
32 | 32 | // Run only when executed as a script, so the functions below can be loaded and tested on their own. |
33 | 33 | if (realpath((string) ($_SERVER['SCRIPT_FILENAME'] ?? '')) === __FILE__) { |
34 | | - exit(main(dirname(__DIR__) . '/icon-sets.json')); |
| 34 | + exit(main(dirname(__DIR__) . '/icon-sets.json', dirname(__DIR__) . '/docs/installation.md')); |
35 | 35 | } |
36 | 36 |
|
37 | | -function main(string $manifestPath): int |
| 37 | +function main(string $manifestPath, ?string $docPath = null): int |
38 | 38 | { |
39 | 39 | $raw = @file_get_contents($manifestPath); |
40 | 40 |
|
@@ -113,9 +113,48 @@ function main(string $manifestPath): int |
113 | 113 | file_put_contents($manifestPath, $encoded . "\n"); |
114 | 114 | echo "Wrote {$manifestPath}\n"; |
115 | 115 |
|
| 116 | + // docs/installation.md shows each set's count, and InstallationPackTableTest fails when it |
| 117 | + // disagrees with the catalogue. Writing the catalogue alone opened a pull request that |
| 118 | + // could never go green (core#179), so the table moves with it. |
| 119 | + if ($docPath !== null && is_file($docPath)) { |
| 120 | + $doc = (string) file_get_contents($docPath); |
| 121 | + $updated = withPackTableCounts($doc, $manifest['sets']); |
| 122 | + |
| 123 | + if ($updated !== $doc) { |
| 124 | + file_put_contents($docPath, $updated); |
| 125 | + echo "Wrote {$docPath}\n"; |
| 126 | + } |
| 127 | + } |
| 128 | + |
116 | 129 | return 0; |
117 | 130 | } |
118 | 131 |
|
| 132 | +/** |
| 133 | + * The installation guide with each pack-table row's count set from the catalogue. |
| 134 | + * |
| 135 | + * Only the count cell of a row whose package is in the catalogue changes; rows, order and |
| 136 | + * repositories are left as written, so a set added or removed still fails the table test |
| 137 | + * rather than being papered over here. |
| 138 | + * |
| 139 | + * @param list<array<string, mixed>> $sets |
| 140 | + */ |
| 141 | +function withPackTableCounts(string $doc, array $sets): string |
| 142 | +{ |
| 143 | + $counts = []; |
| 144 | + |
| 145 | + foreach ($sets as $set) { |
| 146 | + if (isset($set['package'], $set['icon_count']) && is_int($set['icon_count'])) { |
| 147 | + $counts[(string) $set['package']] = number_format($set['icon_count']); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return (string) preg_replace_callback( |
| 152 | + '/^(\| `(ichava\/[\w-]+)` \| )([^|]+?)( \| `https:\/\/github\.com\/[\w\/.-]+` \|)$/m', |
| 153 | + static fn (array $m): string => isset($counts[$m[2]]) ? $m[1] . $counts[$m[2]] . $m[4] : $m[0], |
| 154 | + $doc, |
| 155 | + ); |
| 156 | +} |
| 157 | + |
119 | 158 | /** |
120 | 159 | * The catalogue without its sync timestamp, for comparing two runs. |
121 | 160 | * |
|
0 commit comments