mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-17 20:34:51 +08:00
add laravel/prompts support, and change deploy commands and exception handlers
This commit is contained in:
parent
7298e2441b
commit
4bab7ecfab
@ -9,16 +9,15 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">= 8.0",
|
"php": ">= 8.1",
|
||||||
"ext-tokenizer": "*",
|
"ext-pcntl": "*",
|
||||||
"ext-iconv": "*",
|
"ext-mbstring": "*",
|
||||||
|
"laravel/prompts": "dev-main",
|
||||||
"symfony/console": "^6 || ^5 || ^4",
|
"symfony/console": "^6 || ^5 || ^4",
|
||||||
"zhamao/logger": "^1.0",
|
"zhamao/logger": "^1.0"
|
||||||
"crazywhalecc/cli-helper": "^0.1.0",
|
|
||||||
"nunomaduro/collision": "*",
|
|
||||||
"ext-pcntl": "*"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
"nunomaduro/collision": "*",
|
||||||
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
|
"friendsofphp/php-cs-fixer": "^3.2 != 3.7.0",
|
||||||
"phpstan/phpstan": "^1.1",
|
"phpstan/phpstan": "^1.1",
|
||||||
"captainhook/captainhook": "^5.10",
|
"captainhook/captainhook": "^5.10",
|
||||||
|
|||||||
@ -4,14 +4,15 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace SPC\command;
|
namespace SPC\command;
|
||||||
|
|
||||||
use CliHelper\Tools\ArgFixer;
|
use SPC\store\FileSystem;
|
||||||
use CliHelper\Tools\DataProvider;
|
|
||||||
use CliHelper\Tools\SeekableArrayIterator;
|
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Helper\ProgressBar;
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
use function Laravel\Prompts\confirm;
|
||||||
|
use function Laravel\Prompts\text;
|
||||||
|
|
||||||
#[AsCommand('deploy', 'Deploy static-php-cli self to an .phar application')]
|
#[AsCommand('deploy', 'Deploy static-php-cli self to an .phar application')]
|
||||||
class DeployCommand extends BaseCommand
|
class DeployCommand extends BaseCommand
|
||||||
{
|
{
|
||||||
@ -20,26 +21,51 @@ class DeployCommand extends BaseCommand
|
|||||||
$this->addArgument('target', InputArgument::OPTIONAL, 'The file or directory to pack.');
|
$this->addArgument('target', InputArgument::OPTIONAL, 'The file or directory to pack.');
|
||||||
$this->addOption('auto-phar-fix', null, InputOption::VALUE_NONE, 'Automatically fix ini option.');
|
$this->addOption('auto-phar-fix', null, InputOption::VALUE_NONE, 'Automatically fix ini option.');
|
||||||
$this->addOption('overwrite', 'W', InputOption::VALUE_NONE, 'Overwrite existing files.');
|
$this->addOption('overwrite', 'W', InputOption::VALUE_NONE, 'Overwrite existing files.');
|
||||||
|
$this->addOption('with-no-dev', 'D', InputOption::VALUE_NONE, 'Automatically use non-dev composer dependencies to reduce size');
|
||||||
|
$this->addOption('with-dev', 'd', InputOption::VALUE_NONE, 'Automatically use dev composer dependencies');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
// 第一阶段流程:如果没有写path,将会提示输入要打包的path
|
$composer = require ROOT_DIR . '/vendor/composer/installed.php';
|
||||||
$prompt = new ArgFixer($this->input, $this->output);
|
if (($composer['root']['dev'] ?? false) === true) {
|
||||||
|
if (!$this->getOption('with-no-dev')) {
|
||||||
|
$this->output->writeln('<comment>Current static-php-cli dependencies have installed dev-dependencies</comment>');
|
||||||
|
$this->output->writeln('<comment>If you want to remove, you can choose "Yes" to run command "composer update --no-dev" to remove.</comment>');
|
||||||
|
$this->output->writeln('<comment>Or choose "No", just pack, deploy.</comment>');
|
||||||
|
$ask = confirm('Do you want to remove dev-dependencies to reduce size of phar file?');
|
||||||
|
} elseif (!$this->getOption('with-dev')) {
|
||||||
|
$ask = true;
|
||||||
|
} else {
|
||||||
|
$ask = false;
|
||||||
|
}
|
||||||
|
if ($ask) {
|
||||||
|
[$code] = shell()->execWithResult('composer update --no-dev');
|
||||||
|
if ($code !== 0) {
|
||||||
|
$this->output->writeln('<error>"composer update --no-dev" failed with exit code [' . $code . ']</error>');
|
||||||
|
$this->output->writeln('<error>You may need to run this command by your own.</error>');
|
||||||
|
return static::FAILURE;
|
||||||
|
}
|
||||||
|
$this->output->writeln('<info>Update successfully, you need to re-run deploy command to pack.</info>');
|
||||||
|
return static::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
// 首先得确认是不是关闭了readonly模式
|
// 首先得确认是不是关闭了readonly模式
|
||||||
if (ini_get('phar.readonly') == 1) {
|
if (ini_get('phar.readonly') == 1) {
|
||||||
if ($this->getOption('auto-phar-fix')) {
|
if ($this->getOption('auto-phar-fix')) {
|
||||||
$ask = true;
|
$ask = true;
|
||||||
} else {
|
} else {
|
||||||
$ask = $prompt->requireBool('<comment>pack command needs "phar.readonly" = "Off" !</comment>' . PHP_EOL . 'If you want to automatically set it and continue, just Enter', true);
|
$this->output->writeln('<comment>pack command needs "phar.readonly" = "Off" !</comment>');
|
||||||
|
$ask = confirm('Do you want to automatically set it and continue ?');
|
||||||
|
// $ask = $prompt->requireBool('<comment>pack command needs "phar.readonly" = "Off" !</comment>' . PHP_EOL . 'If you want to automatically set it and continue, just Enter', true);
|
||||||
}
|
}
|
||||||
if ($ask) {
|
if ($ask) {
|
||||||
global $argv;
|
global $argv;
|
||||||
$args = array_merge(['-d', 'phar.readonly=0'], $_SERVER['argv']);
|
$args = array_merge(['-d', 'phar.readonly=0'], $_SERVER['argv'], ['--no-motd']);
|
||||||
if (function_exists('pcntl_exec')) {
|
if (function_exists('pcntl_exec')) {
|
||||||
$this->output->writeln('<info>Changing to phar.readonly=0 mode ...</info>');
|
$this->output->writeln('<info>Changing to phar.readonly=0 mode ...</info>');
|
||||||
if (pcntl_exec(PHP_BINARY, $args) === false) {
|
if (pcntl_exec(PHP_BINARY, $args) === false) {
|
||||||
throw new \PharException('切换到读写模式失败,请检查环境。');
|
throw new \PharException('Switching to read write mode failed, please check the environment.');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->output->writeln('<info>Now running command in child process.</info>');
|
$this->output->writeln('<info>Now running command in child process.</info>');
|
||||||
@ -51,13 +77,19 @@ class DeployCommand extends BaseCommand
|
|||||||
// 获取路径
|
// 获取路径
|
||||||
$path = WORKING_DIR;
|
$path = WORKING_DIR;
|
||||||
// 如果是目录,则将目录下的所有文件打包
|
// 如果是目录,则将目录下的所有文件打包
|
||||||
$phar_path = $prompt->requireArgument('target', 'Please input the phar target filename', 'static-php-cli.phar');
|
$phar_path = text('Please input the phar target filename', default: '/tmp/static-php-cli.phar');
|
||||||
|
// $phar_path = $prompt->requireArgument('target', 'Please input the phar target filename', 'static-php-cli.phar');
|
||||||
|
|
||||||
if (DataProvider::isRelativePath($phar_path)) {
|
if (FileSystem::isRelativePath($phar_path)) {
|
||||||
$phar_path = '/tmp/' . $phar_path;
|
$phar_path = WORKING_DIR . '/' . $phar_path;
|
||||||
}
|
}
|
||||||
if (file_exists($phar_path)) {
|
if (file_exists($phar_path)) {
|
||||||
$ask = $this->getOption('overwrite') ? true : $prompt->requireBool('<comment>The file "' . $phar_path . '" already exists, do you want to overwrite it?</comment>' . PHP_EOL . 'If you want to, just Enter');
|
if (!$this->getOption('overwrite')) {
|
||||||
|
$this->output->writeln('<comment>The file "' . $phar_path . '" already exists.</comment>');
|
||||||
|
$ask = confirm('Do you want to overwrite it?');
|
||||||
|
} else {
|
||||||
|
$ask = true;
|
||||||
|
}
|
||||||
if (!$ask) {
|
if (!$ask) {
|
||||||
$this->output->writeln('<comment>User canceled.</comment>');
|
$this->output->writeln('<comment>User canceled.</comment>');
|
||||||
return static::FAILURE;
|
return static::FAILURE;
|
||||||
@ -67,7 +99,7 @@ class DeployCommand extends BaseCommand
|
|||||||
$phar = new \Phar($phar_path);
|
$phar = new \Phar($phar_path);
|
||||||
$phar->startBuffering();
|
$phar->startBuffering();
|
||||||
|
|
||||||
$all = DataProvider::scanDirFiles($path, true, true);
|
$all = FileSystem::scanDirFiles($path, true, true);
|
||||||
|
|
||||||
$all = array_filter($all, function ($x) {
|
$all = array_filter($all, function ($x) {
|
||||||
$dirs = preg_match('/(^(config|src|vendor)\\/|^(composer\\.json|README\\.md|source\\.json|LICENSE|README-en\\.md)$)/', $x);
|
$dirs = preg_match('/(^(config|src|vendor)\\/|^(composer\\.json|README\\.md|source\\.json|LICENSE|README-en\\.md)$)/', $x);
|
||||||
|
|||||||
@ -47,7 +47,8 @@ class ExceptionHandler
|
|||||||
logger()->error($e->getTraceAsString());
|
logger()->error($e->getTraceAsString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->whoops->handleException($e);
|
$this->whoops->handleException($e);
|
||||||
|
|
||||||
|
logger()->critical('You can report this exception to static-php-cli GitHub repo.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -320,28 +320,7 @@ class FileSystem
|
|||||||
foreach ($files as $v) {
|
foreach ($files as $v) {
|
||||||
$pathinfo = pathinfo($v);
|
$pathinfo = pathinfo($v);
|
||||||
if (($pathinfo['extension'] ?? '') == 'php') {
|
if (($pathinfo['extension'] ?? '') == 'php') {
|
||||||
$path = rtrim($dir, '/') . '/' . rtrim($pathinfo['dirname'], './') . '/' . $pathinfo['basename'];
|
if ($rule === null) {
|
||||||
|
|
||||||
// 过滤不包含类的文件
|
|
||||||
$tokens = token_get_all(self::readFile($path));
|
|
||||||
$found = false;
|
|
||||||
foreach ($tokens as $token) {
|
|
||||||
if (!is_array($token)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ($token[0] === T_CLASS) {
|
|
||||||
$found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!$found) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($rule === null) { // 规则未设置回调时候,使用默认的识别过滤规则
|
|
||||||
/*if (substr(file_get_contents($dir . '/' . $v), 6, 6) == '#plain') {
|
|
||||||
continue;
|
|
||||||
}*/
|
|
||||||
if (file_exists($dir . '/' . $pathinfo['basename'] . '.ignore')) {
|
if (file_exists($dir . '/' . $pathinfo['basename'] . '.ignore')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user