-
Notifications
You must be signed in to change notification settings - Fork 29
/
fetch_source.php
executable file
·138 lines (118 loc) · 4.27 KB
/
fetch_source.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
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
134
135
136
137
138
#!php
<?php
/**
* Copyright (c) 2022 Yun Dou <dixyes@gmail.com>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
require __DIR__ . '/common/Log.php';
require __DIR__ . '/common/LogType.php';
require __DIR__ . '/common/FetcherUtilTrait.php';
require __DIR__ . '/common/CommonUtilTrait.php';
require __DIR__ . '/common/Fetcher/SourceCode.php';
require __DIR__ . '/common/Fetcher/SourceCodeSource.php';
class Util
{
use CommonUtilTrait;
use FetcherUtilTrait;
}
function mian($argv): int
{
Util::setErrorHandler();
$cmdArgs = Util::parseArgs(
argv: $argv,
positionalNames: [
'libraries' => ['LIBRARIES', false, '', 'libraries used, comma spearated, empty for all'],
'extensions' => ['EXTENSIONS', false, '', 'extensions used, comma spearated, empty for all'],
],
namedKeys: [
'hash' => ['BOOL', false, false, 'hash only'],
'versionFile' => ['PATH', false, null, 'output versions to a file'],
'shallowClone' => ['BOOL', false, false, 'use shallow clone'],
'srcFile' => ['SRCFILE', false, __DIR__ . DIRECTORY_SEPARATOR . 'src.json', 'src.json path'],
'phpVer' => ['VERSION', false, '8.3', 'php version in major.minor format like 8.3'],
],
);
$phpVer = $cmdArgs['named']['phpVer'];
preg_match('/^\d+\.\d+$/', $phpVer, $matches);
if (!$matches) {
Log::e("bad version arg: {$phpVer}\n");
return 1;
}
$phpRef = Util::latestPHP($phpVer);
if ($cmdArgs['named']['hash']) {
Log::$outFd = STDERR;
}
$data = json_decode(file_get_contents($cmdArgs['named']['srcFile']), true);
$chosen = [
'php',
'micro',
];
$libraries = array_map('trim', array_filter(explode(',', $cmdArgs['positional']['libraries'])));
if ($libraries) {
foreach ($libraries as $lib) {
$srcName = $data['libs'][$lib]['source'];
$chosen[] = $srcName;
}
} else {
$chosen = [...$chosen, ...array_map(fn ($x) => $x['source'], array_values($data['libs']))];
}
$extensions = array_map('trim', array_filter(explode(',', $cmdArgs['positional']['extensions'])));
if ($extensions) {
foreach ($extensions as $lib) {
$srcName = $data['exts'][$lib]['source'];
$chosen[] = $srcName;
}
} else {
$chosen = [...$chosen, ...array_map(fn ($x) => $x['source'], array_values($data['exts']))];
}
$chosen = array_unique($chosen);
$filter = fn ($_, $name) => in_array($name, $chosen, true);
$versionFile = fopen($cmdArgs['named']['versionFile'] ?? "php://memory", 'w+');
$shallowClone = (bool)$cmdArgs['named']['shallowClone'];
@mkdir('downloads');
/** @var GitSourceCodeSource $phpSCS */
$phpSCS = SourceCodeSource::fromConfig('php', [
"type" => "git",
"path" => "php-src",
"url" => "https://github.com/php/php-src",
"ref" => $phpRef,
]);
$phpSC = $phpSCS->download();
fwrite($versionFile, "{$phpSCS->versionLine()}\n");
if (!$cmdArgs['named']['hash']) {
$phpSCS->clone('src/php-src', shallowClone: $shallowClone);
}
ksort($data["src"]);
foreach (array_filter($data["src"], $filter, ARRAY_FILTER_USE_BOTH) as $name => $config) {
// var_dump($name, $config);
$scs = SourceCodeSource::fromConfig($name, $config);
fwrite($versionFile, "{$scs->versionLine()}\n");
if ($cmdArgs['named']['hash']) {
continue;
}
$sc = $scs->download();
$sc->prepare(shallowClone: $shallowClone);
}
if (!$cmdArgs['named']['hash']) {
$phpSC->prepare();
} else {
$versionLines = stream_get_contents($versionFile, offset: 0);
printf("%s\n", hash('sha256', $versionLines));
}
fclose($versionFile);
Log::i('done');
return 0;
}
exit(mian($argv));