-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.php
More file actions
174 lines (156 loc) · 7.3 KB
/
build.php
File metadata and controls
174 lines (156 loc) · 7.3 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
// Script from https://github.com/ColinHDev/pmmp-plugin-build-script
declare(strict_types=1);
if (ini_get("phar.readonly") === "1") {
echo "Set phar.readonly to 0 with -dphar.readonly=0" . PHP_EOL;
exit(1);
}
$start = microtime(true);
/**
* @link https://github.com/pmmp/PocketMine-MP/blob/stable/build/server-phar.php
*/
$dir = rtrim(str_replace("/", DIRECTORY_SEPARATOR, __DIR__ . DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$regex = sprintf('/^%s(%s).*/i',
//String must start with this path...
preg_quote($dir, '/'),
//... and must be followed by one of these relative paths, if any were specified. If none, this will produce a null capturing group which will allow anything.
implode('|', array_map(static function(string $string) : string { return preg_quote($string, '/'); }, ["src", "resources", "plugin.yml", ".poggit.yml"]))
);
$files = [];
$iterator = new RegexIterator(
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS |
FilesystemIterator::CURRENT_AS_PATHNAME)),
$regex
);
foreach($iterator as $file) {
$files[str_replace($dir, "", $file)] = file_get_contents($file);
}
$composerFile = __DIR__ . DIRECTORY_SEPARATOR . "composer.json";
if (is_file($composerFile)) {
$vendorPath = __DIR__ . DIRECTORY_SEPARATOR . "vendor";
if (!is_dir($vendorPath)) {
exec("composer install --no-progress --no-dev --prefer-dist --optimize-autoloader --ignore-platform-reqs");
}
$composerData = json_decode(file_get_contents($composerFile), true);
$injectableDependencies = [];
foreach ($composerData["require"] ?? [] as $dependency => $version) {
if (isPlatformPackage($dependency)) {
continue;
}
searchInjectableDependencies($dependency, $vendorPath, $injectableDependencies);
}
$dependencyPrefixes = [];
foreach ($injectableDependencies as $dependency => $directory) {
$prefix = "_" . bin2hex(random_bytes(10)) . "_";
$dependencyPrefixes[$dependency] = $prefix;
$src = "src" . DIRECTORY_SEPARATOR . str_replace(["/", "\\"], DIRECTORY_SEPARATOR, $dependency);
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::CURRENT_AS_PATHNAME)
);
foreach ($iterator as $file) {
$srcPos = strpos($file, $src);
if ($srcPos === false) {
continue;
}
$shadedFilePath = "src" . DIRECTORY_SEPARATOR . $prefix . substr($file, $srcPos + 4);
$files[$shadedFilePath] = file_get_contents($file);
}
}
foreach ($dependencyPrefixes as $dependency => $prefix) {
foreach ($files as $file => $contents) {
$files[$file] = shadeFile($contents, $dependency, $prefix);
}
}
}
$pharPath = __DIR__ . DIRECTORY_SEPARATOR . basename(__DIR__) . ".phar";
if (file_exists($pharPath)) {
Phar::unlinkArchive($pharPath);
}
$phar = new Phar($pharPath);
$phar->startBuffering();
foreach($files as $file => $contents) {
$phar->addFromString($file, $contents);
}
$phar->compressFiles(Phar::GZ);
$phar->stopBuffering();
echo "Done in " . round(microtime(true) - $start, 3) . "s" . PHP_EOL;
exit();
function searchInjectableDependencies(string $dependency, string $vendorPath, array &$injectableDependencies) : void {
$dependencyPath = $vendorPath . DIRECTORY_SEPARATOR . str_replace("/", DIRECTORY_SEPARATOR, $dependency);
$composerFile = $dependencyPath . DIRECTORY_SEPARATOR . "composer.json";
if (is_dir($dependencyPath) && is_file($composerFile)) {
$composerData = json_decode(file_get_contents($composerFile), true);
if (!isset($composerData["extra"]["virion"])) {
return;
}
$virionData = $composerData["extra"]["virion"];
if (!is_array($virionData) || !isset($virionData["spec"])) {
return;
}
$specVersion = $virionData["spec"];
if (version_compare($specVersion, "3.0", ">") || version_compare($specVersion, "3.0", "<") || !isset($virionData["namespace-root"])) {
return;
}
$injectableDependencies[$virionData["namespace-root"]] = $dependencyPath;
foreach($composerData["require"] ?? [] as $subdependency => $version) {
if (isPlatformPackage($subdependency)) {
continue;
}
searchInjectableDependencies($subdependency, $vendorPath, $injectableDependencies);
}
}
}
/**
* @link https://github.com/poggit/poggit/blob/000a86ff75481540b561a9bf6f363418012bdbdf/assets/php/virion.php#L147-L213
*/
function shadeFile(string $fileContents, string $dependency, string $prefix) : string {
$tokens = token_get_all($fileContents);
$tokens[] = ""; // should not be valid though
foreach($tokens as $offset => $token) {
if(!is_array($token) or $token[0] !== T_WHITESPACE) {
/** @noinspection IssetArgumentExistenceInspection */
list($id, $str, $line) = is_array($token) ? $token : [-1, $token, $line ?? 1];
//namespace test; is a T_STRING whereas namespace test\test; is not.
if(isset($init, $prefixToken) and $id === T_STRING){
if($str === $dependency) { // case-sensitive!
$tokens[$offset][1] = $prefix . $dependency . substr($str, strlen($dependency));
} elseif(stripos($str, $dependency) === 0) {
echo "\x1b[38;5;227m\n[WARNING] Not replacing FQN $str case-insensitively.\n\x1b[m";
}
unset($init, $prefixToken);
} else {
if($id === T_NAMESPACE) {
$init = $offset;
$prefixToken = $id;
} elseif($id === T_NAME_QUALIFIED) {
if(($str[strlen($dependency)]??"\\") === "\\") {
if(strpos($str, $dependency) === 0) { // case-sensitive!
$tokens[$offset][1] = $prefix . $dependency . substr($str, strlen($dependency));
} elseif(stripos($str, $dependency) === 0) {
echo "\x1b[38;5;227m\n[WARNING] Not replacing FQN $str case-insensitively.\n\x1b[m";
}
}
unset($init, $prefixToken);
} elseif($id === T_NAME_FULLY_QUALIFIED){
if(strpos($str, "\\" . $dependency . "\\") === 0) { // case-sensitive!
$tokens[$offset][1] = "\\" . $prefix . $dependency . substr($str, strlen($dependency)+1);
} elseif(stripos($str, "\\" . $dependency . "\\") === 0) {
echo "\x1b[38;5;227m\n[WARNING] Not replacing FQN $str case-insensitively.\n\x1b[m";
}
unset($init, $prefixToken);
}
}
}
}
$ret = "";
foreach($tokens as $token) {
$ret .= is_array($token) ? $token[1] : $token;
}
return $ret;
}
/**
* @link https://github.com/SOF3/pharynx/blob/a42afb5e998769ea95ae5808ec25e459b2e3a6e6/src/Args.php#L322-L324
*/
function isPlatformPackage(string $depName) : bool {
return preg_match('#^(php|(ext|php|lib|composer)-[^/]+)$#', $depName, $_) === 1;
}