This repository has been archived by the owner on Dec 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.php
178 lines (149 loc) · 5.44 KB
/
build.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
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
175
176
177
178
<?php
declare(strict_types=1);
const PLUGIN_STUB = '
<?php
echo "%s PocketMine-MP plugin v%s, developed by %s. Built on %s.
----------------
";
if(extension_loaded("phar")){
$phar = new \Phar(__FILE__);
foreach($phar->getMetadata() as $key => $value){
echo ucfirst($key) . ": " . (is_array($value) ? implode(", ", $value) : $value) . "\n";
}
}
__HALT_COMPILER();
';
function main() : void{
echo "== Plugin Build Script ==" . PHP_EOL;
if(ini_get("phar.readonly") == 1){
echo "Set phar.readonly to 0 with -dphar.readonly=0" . PHP_EOL;
exit(1);
}
$includedPaths = ["src", "resources", "plugin.yml", "LICENSE"];
array_walk($includedPaths, function(&$path, $key){
$realPath = realpath($path);
if($realPath === false){
echo "[ERROR] required directory `$path` does not exist or permission denied" . PHP_EOL;
echo "[ERROR] have you installed composer dependencies?" . PHP_EOL;
exit(1);
}
//Convert to absolute path for base path detection
if(is_dir($realPath)){
$path = rtrim($realPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
});
$basePath = rtrim(realpath(__DIR__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
//Convert included paths back to relative after we decide what the base path is
$includedPaths = array_filter(array_map(function(string $path) use ($basePath) : string{
return str_replace($basePath, '', $path);
}, $includedPaths), function(string $v) : bool{
return $v !== '';
});
$pharName = "Charm.phar";
if(!is_dir($basePath)){
echo $basePath . " is not a folder" . PHP_EOL;
return;
}
echo PHP_EOL;
$metadata = generatePluginMetadataFromYml($basePath . "plugin.yml");
if($metadata === null) {
echo "Missing entry point or plugin.yml" . PHP_EOL;
exit(1);
}
$stub = sprintf(PLUGIN_STUB, $metadata["name"], $metadata["version"], $metadata["author"], date("r"));
foreach(buildPhar(__DIR__ . DIRECTORY_SEPARATOR . $pharName, $basePath, $includedPaths, $metadata, $stub) as $line){
echo $line . PHP_EOL;
}
}
/**
* @param string[] $strings
* @param string|null $delim
*
* @return string[]
*/
function preg_quote_array(array $strings, string $delim = null) : array{
return array_map(function(string $str) use ($delim) : string{ return preg_quote($str, $delim); }, $strings);
}
/**
* @param string $pharPath
* @param string $basePath
* @param string[] $includedPaths
* @param array $metadata
* @param string $stub
* @param int $signatureAlgo
* @param int|null $compression
*
* @return Generator|string[]
*/
function buildPhar(string $pharPath, string $basePath, array $includedPaths, array $metadata, string $stub, int $signatureAlgo = \Phar::SHA1, ?int $compression = null){
if(file_exists($pharPath)){
yield "Phar file already exists, overwriting...";
try{
\Phar::unlinkArchive($pharPath);
}catch(\PharException $e){
//unlinkArchive() doesn't like dodgy phars
unlink($pharPath);
}
}
yield "Adding files...";
$start = microtime(true);
$phar = new \Phar($pharPath);
$phar->setMetadata($metadata);
$phar->setStub($stub);
$phar->setSignatureAlgorithm($signatureAlgo);
$phar->startBuffering();
//If paths contain any of these, they will be excluded
$excludedSubstrings = preg_quote_array([
realpath($pharPath), //don't add the phar to itself
], '/');
$folderPatterns = preg_quote_array([
DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR . '.' //"Hidden" files, git dirs etc
], '/');
//Only exclude these within the basedir, otherwise the project won't get built if it itself is in a directory that matches these patterns
$basePattern = preg_quote(rtrim($basePath, DIRECTORY_SEPARATOR), '/');
foreach($folderPatterns as $p){
$excludedSubstrings[] = $basePattern . '.*' . $p;
}
$regex = sprintf('/^(?!.*(%s))^%s(%s).*/i',
implode('|', $excludedSubstrings), //String may not contain any of these substrings
preg_quote($basePath, '/'), //String must start with this path...
implode('|', preg_quote_array($includedPaths, '/')) //... 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.
);
$directory = new \RecursiveDirectoryIterator($basePath, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::CURRENT_AS_PATHNAME); //can't use fileinfo because of symlinks
$iterator = new \RecursiveIteratorIterator($directory);
$regexIterator = new \RegexIterator($iterator, $regex);
$count = count($phar->buildFromIterator($regexIterator, $basePath));
yield "Added $count files";
if($compression !== null){
yield "Checking for compressible files...";
foreach($phar as $file => $finfo){
/** @var \PharFileInfo $finfo */
if($finfo->getSize() > (1024 * 512)){
yield "Compressing " . $finfo->getFilename();
$finfo->compress($compression);
}
}
}
$phar->stopBuffering();
yield "Done in " . round(microtime(true) - $start, 3) . "s";
}
function generatePluginMetadataFromYml(string $pluginYmlPath) : ?array{
if(!file_exists($pluginYmlPath)){
return null;
}
$pluginYml = yaml_parse_file($pluginYmlPath);
return [
"name" => $pluginYml["name"],
"version" => $pluginYml["version"],
"main" => $pluginYml["main"],
"api" => $pluginYml["api"],
"depend" => $pluginYml["depend"] ?? "",
"description" => $pluginYml["description"] ?? "",
"author" => $pluginYml["author"] ?? "",
"authors" => $pluginYml["authors"] ?? "",
"website" => $pluginYml["website"] ?? "",
"creationDate" => time()
];
}
main();