add phar compression

This commit is contained in:
sunxyw
2022-12-31 23:57:56 +08:00
parent 413d783d53
commit 50d6e75e71
2 changed files with 23 additions and 8 deletions

View File

@@ -18,8 +18,9 @@ class BuildCommand extends Command
*/ */
protected function configure() protected function configure()
{ {
$this->setHelp('此功能将会把整个项目打包为 Phar'); $this->setHelp('此功能将会把整个项目打包为 Phar' . PHP_EOL . '默认会启用压缩功能,通过去除文件中的注释和空格,以减小文件大小,但可能增加构建耗时');
$this->addOption('target', 'D', InputOption::VALUE_REQUIRED, '指定输出文件位置', 'zm.phar'); $this->addOption('target', 'D', InputOption::VALUE_REQUIRED, '指定输出文件位置', 'zm.phar');
$this->addOption('no-compress', null, InputOption::VALUE_NONE, '是否不压缩文件,以减小构建耗时');
} }
protected function handle(): int protected function handle(): int
@@ -33,9 +34,17 @@ class BuildCommand extends Command
$this->ensureTargetWritable($target); $this->ensureTargetWritable($target);
$this->comment("目标文件:{$target}"); $this->comment("目标文件:{$target}");
if (file_exists($target)) {
$this->comment('目标文件已存在,正在删除...');
unlink($target);
}
$this->info('正在构建 Phar 包'); $this->info('正在构建 Phar 包');
$this->build($target, LOAD_MODE === LOAD_MODE_VENDOR ? 'src/entry.php' : 'vendor/zhamao/framework/src/entry.php'); $this->build(
$target,
LOAD_MODE === LOAD_MODE_VENDOR ? 'src/entry.php' : 'vendor/zhamao/framework/src/entry.php',
);
$this->info('Phar 包构建完成'); $this->info('Phar 包构建完成');
@@ -80,14 +89,20 @@ class BuildCommand extends Command
$phar->startBuffering(); $phar->startBuffering();
$files = FileSystem::scanDirFiles(SOURCE_ROOT_DIR, true, true); $files = FileSystem::scanDirFiles(SOURCE_ROOT_DIR, true, true);
$files = array_filter($files, function ($x) { // 只打包 bin / config / resources / src / vendor
$dirs = preg_match('/(^(bin|config|resources|src|vendor)\\/|^(composer\\.json|README\\.md)$)/', $x); $files = array_filter($files, function ($file) {
return !($dirs !== 1); return preg_match('#^(bin|config|resources|src|vendor)/#', $file);
}); });
sort($files); sort($files);
foreach ($this->progress()->iterate($files) as $file) { if ($this->input->getOption('no-compress')) {
$phar->addFile($file, $file); foreach ($this->progress()->iterate($files) as $file) {
$phar->addFile($file, $file);
}
} else {
foreach ($this->progress()->iterate($files) as $file) {
$phar->addFromString($file, php_strip_whitespace($file));
}
} }
$phar->setStub( $phar->setStub(

View File

@@ -165,7 +165,7 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
$progress->setEmptyBarCharacter('<fg=red>⚬</>'); $progress->setEmptyBarCharacter('<fg=red>⚬</>');
$progress->setProgressCharacter('<fg=green>➤</>'); $progress->setProgressCharacter('<fg=green>➤</>');
$progress->setFormat( $progress->setFormat(
"%current%/%max% [%bar%] %percent:3s%%\n🪅 %estimated:-20s% %memory:20s%" "%current%/%max% [%bar%] %percent:3s%%\n🪅 %estimated:-20s% %memory:20s%" . PHP_EOL
); );
return $progress; return $progress;
} }