From 08e3b8ffdc179607ea7e86d22cf72072dd630ee6 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 4 Jan 2020 14:56:53 +0000 Subject: [PATCH] build: added specialized script to create a server phar this is much easier to use than devtools, and allows us to make additional specializations for PM build that would otherwise just be a colossal pain in the ass. --- build/server-phar.php | 147 ++++++++++++++++++++++++++++++++++++++++++ phpstan.neon.dist | 2 + 2 files changed, 149 insertions(+) create mode 100644 build/server-phar.php diff --git a/build/server-phar.php b/build/server-phar.php new file mode 100644 index 000000000..de0abee97 --- /dev/null +++ b/build/server-phar.php @@ -0,0 +1,147 @@ +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 main() : void{ + if(ini_get("phar.readonly") == 1){ + echo "Set phar.readonly to 0 with -dphar.readonly=0" . PHP_EOL; + exit(1); + } + + $opts = getopt("", ["out:"]); + $gitHash = Git::getRepositoryStatePretty(dirname(__DIR__)); + echo "Git hash detected as $gitHash" . PHP_EOL; + foreach(buildPhar( + $opts["out"] ?? getcwd() . DIRECTORY_SEPARATOR . "PocketMine-MP.phar", + dirname(__DIR__) . DIRECTORY_SEPARATOR, + [ + 'src', + 'vendor' + ], + [ + 'git' => $gitHash + ], + '