-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.php
101 lines (92 loc) · 3.05 KB
/
update.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
set_time_limit(0);
function needDirectory($directory) {
if (!file_exists($directory)) {
return mkdir($directory, 0777, true);
}
return false;
}
$enginesDirectory = __DIR__ . '/var/engines';
needDirectory($enginesDirectory);
$cacheDirectory = __DIR__ . '/var/cache';
needDirectory($cacheDirectory);
$gitHost = 'https://github.com/';
$apiHost = 'https://api.github.com/';
$apiContext = stream_context_create([
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: PHP',
],
],
]);
$enginesRepositories = [
'tale-pug' => 'Talesoft/tale-pug',
'phug' => 'phug-php/phug',
'pug-php' => 'pug-php/pug',
];
foreach ($enginesRepositories as $repository => $url) {
$optionsHtml = '';
$directory = $enginesDirectory . DIRECTORY_SEPARATOR . $repository;
needDirectory($directory);
$versionCache = $cacheDirectory . DIRECTORY_SEPARATOR . $repository . '-tags.json';
$versionFile = $versionCache;
if (!file_exists($versionCache) || time() - filemtime($versionCache) > 3600) {
$list = [];
for ($i = 1; true; $i++) {
$items = @json_decode(file_get_contents(
$apiHost . 'repos/' . $url . '/tags?page=' . $i,
false,
$apiContext
));
if (!is_array($items)) {
$items = @json_decode(file_get_contents(
__DIR__ . '/fallback/' . $repository . '-tags.json'
));
}
$list = array_merge($list, $items);
if (count($items) < 30) {
break;
}
}
file_put_contents($versionCache, json_encode($list));
}
$touched = false;
$tags = json_decode(file_get_contents($versionCache));
usort($tags, function ($a, $b) {
$a = strtolower($a->name);
$b = strtolower($b->name);
if ($a === $b) {
return 0;
}
if (strpos($a, $b) === 0) {
return 1;
}
if (strpos($b, $a) === 0) {
return -1;
}
$tab = [$a, $b];
sort($tab, SORT_STRING);
return $a === $tab[0] ? 1 : -1;
});
foreach ($tags as $tag) {
echo "Load $url {$tag->name}\n";
$optionsHtml .= '<option value="' . $tag->name . '">' . $tag->name . '</option>';
$versionDirectory = $directory . DIRECTORY_SEPARATOR . $tag->name;
if (needDirectory($versionDirectory)) {
$touched = true;
chdir($versionDirectory);
echo shell_exec('git clone ' . $gitHost . $url . ' .');
echo shell_exec('git checkout tags/' . $tag->name);
foreach (['tests', 'examples'] as $ignore) {
if (file_exists($ignore)) {
shell_exec('rm -rf ' . $ignore);
}
}
echo shell_exec('composer update --no-dev &');
}
}
if ($touched) {
file_put_contents($cacheDirectory . DIRECTORY_SEPARATOR . $repository . '-versions-options.html', $optionsHtml);
}
}