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