2023-03-18 17:32:21 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
namespace SPC\command;
|
|
|
|
|
|
|
|
|
|
|
|
use SPC\builder\BuilderProvider;
|
2023-03-21 00:28:05 +08:00
|
|
|
|
use SPC\exception\ExceptionHandler;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
use SPC\exception\RuntimeException;
|
2023-04-22 17:45:43 +08:00
|
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
2023-03-18 17:32:21 +08:00
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
2023-04-22 17:45:43 +08:00
|
|
|
|
#[AsCommand('build:libs', 'Build dependencies')]
|
2023-03-18 17:32:21 +08:00
|
|
|
|
class BuildLibsCommand extends BuildCommand
|
|
|
|
|
|
{
|
2023-08-20 19:51:45 +08:00
|
|
|
|
public function configure(): void
|
2023-03-18 17:32:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
$this->addArgument('libraries', InputArgument::REQUIRED, 'The libraries will be compiled, comma separated');
|
|
|
|
|
|
$this->addOption('clean', null, null, 'Clean old download cache and source before fetch');
|
|
|
|
|
|
$this->addOption('all', 'A', null, 'Build all libs that static-php-cli needed');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
|
public function initialize(InputInterface $input, OutputInterface $output): void
|
2023-03-18 17:32:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
// --all 等于 ""
|
|
|
|
|
|
if ($input->getOption('all')) {
|
|
|
|
|
|
$input->setArgument('libraries', '');
|
|
|
|
|
|
}
|
2023-03-21 00:28:05 +08:00
|
|
|
|
parent::initialize($input, $output);
|
2023-03-18 17:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
|
*/
|
2023-04-22 17:45:43 +08:00
|
|
|
|
public function handle(): int
|
2023-03-18 17:32:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 从参数中获取要编译的 libraries,并转换为数组
|
2023-04-22 17:45:43 +08:00
|
|
|
|
$libraries = array_map('trim', array_filter(explode(',', $this->getArgument('libraries'))));
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除旧资源
|
2023-04-22 17:45:43 +08:00
|
|
|
|
if ($this->getOption('clean')) {
|
2023-03-18 17:32:21 +08:00
|
|
|
|
logger()->warning('You are doing some operations that not recoverable: removing directories below');
|
|
|
|
|
|
logger()->warning(BUILD_ROOT_PATH);
|
|
|
|
|
|
logger()->warning('I will remove these dir after you press [Enter] !');
|
|
|
|
|
|
echo 'Confirm operation? [Yes] ';
|
|
|
|
|
|
fgets(STDIN);
|
|
|
|
|
|
if (PHP_OS_FAMILY === 'Windows') {
|
|
|
|
|
|
f_passthru('rmdir /s /q ' . BUILD_ROOT_PATH);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
f_passthru('rm -rf ' . BUILD_ROOT_PATH);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-21 00:28:05 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 构建对象
|
2023-04-22 17:45:43 +08:00
|
|
|
|
$builder = BuilderProvider::makeBuilderByInput($this->input);
|
2023-03-21 00:28:05 +08:00
|
|
|
|
// 只编译 library 的情况下,标记
|
|
|
|
|
|
$builder->setLibsOnly();
|
|
|
|
|
|
// 编译和检查库完整
|
|
|
|
|
|
$builder->buildLibs($libraries);
|
2023-03-18 17:32:21 +08:00
|
|
|
|
|
2023-03-21 00:28:05 +08:00
|
|
|
|
$time = round(microtime(true) - START_TIME, 3);
|
|
|
|
|
|
logger()->info('Build libs complete, used ' . $time . ' s !');
|
2023-08-06 10:43:20 +08:00
|
|
|
|
return static::SUCCESS;
|
2023-03-21 00:28:05 +08:00
|
|
|
|
} catch (\Throwable $e) {
|
2023-04-22 17:45:43 +08:00
|
|
|
|
if ($this->getOption('debug')) {
|
2023-03-21 00:28:05 +08:00
|
|
|
|
ExceptionHandler::getInstance()->handle($e);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logger()->critical('Build failed with ' . get_class($e) . ': ' . $e->getMessage());
|
|
|
|
|
|
logger()->critical('Please check with --debug option to see more details.');
|
|
|
|
|
|
}
|
2023-08-06 10:43:20 +08:00
|
|
|
|
return static::FAILURE;
|
2023-03-21 00:28:05 +08:00
|
|
|
|
}
|
2023-03-18 17:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|