-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-nav-value.php
90 lines (64 loc) · 2.16 KB
/
generate-nav-value.php
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
<?php
require_once 'loader.php';
// if not cli, warn and exit
if (php_sapi_name() !== 'cli') {
echo "This script must be run from the command line\n";
exit(1);
}
$directories = glob('web/*', GLOB_ONLYDIR);
$out_var = [];
foreach ($directories as $directory) {
$just_slug = str_replace('web/', '', $directory);
$yaml_file = "$directory/$just_slug.yaml";
if (!file_exists($yaml_file)) {
echo "No YAML file for $just_slug\n";
// prompt user to (c)continue or (q)quit
echo "Continue? (c)ontinue or (q)uit: ";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim($line) != 'c') {
echo "Aborted\n";
exit;
}
continue;
}
$yaml = file_get_contents($yaml_file);
$yaml = Spyc::YAMLLoad($yaml);
$out_var[$just_slug] = $yaml;
}
$out_file_content = sprintf('/* START: handled by php generate-nav-value LAST UPDATE %s */', date('Y-m-d H:i:s'));
$out_file_content .= PHP_EOL;
$out_file_content .= 'return $slugs_and_titles = '. var_export($out_var, true) . ';';
$out_file_content .= PHP_EOL;
$target_file = 'classes/Nav.php';
$existing_content = file($target_file);
$new_content = [];
$found_start = false;
$found_end = false;
$new_content_inserted = false;
foreach ($existing_content as $line) {
if (strpos($line, '/* START: handled by php generate-nav-value LAST UPDATE') === false) {
echo '';
} else {
$found_start = true;
}
if (strpos($line, '/* END: handled by php generate-nav-value */') === false) {
echo '';
} else {
$found_end = true;
}
// if we have not found the start, write
if (!$found_start) {
$new_content[] = $line;
}
// if we found the start and the end, write
if ($found_start && $found_end) {
$new_content[] = $line;
}
// if we found the start, but not the end, skip, and if we have not inserted the new content, insert it
if ($found_start && !$found_end && !$new_content_inserted) {
$new_content[] = $out_file_content;
$new_content_inserted = true;
}
}
file_put_contents($target_file, implode('', $new_content));