static-php-cli/src/SPC/command/DumpLicenseCommand.php

74 lines
3.4 KiB
PHP
Raw Normal View History

2023-03-18 17:32:21 +08:00
<?php
declare(strict_types=1);
namespace SPC\command;
2023-04-15 18:46:46 +08:00
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\util\DependencyUtil;
use SPC\util\LicenseDumper;
2023-04-22 17:45:43 +08:00
use Symfony\Component\Console\Attribute\AsCommand;
2023-04-15 18:46:46 +08:00
use Symfony\Component\Console\Input\InputOption;
2023-03-18 17:32:21 +08:00
/**
* 修改 config 后对其 kv 进行排序的操作
*/
2023-04-22 17:45:43 +08:00
#[AsCommand('dump-license', 'Dump licenses for required libraries')]
2023-03-18 17:32:21 +08:00
class DumpLicenseCommand extends BaseCommand
{
public function configure()
{
2023-04-15 18:46:46 +08:00
$this->addOption('by-extensions', null, InputOption::VALUE_REQUIRED, 'Dump by extensions and related libraries', null);
$this->addOption('without-php', null, InputOption::VALUE_NONE, 'Dump without php-src');
$this->addOption('by-libs', null, InputOption::VALUE_REQUIRED, 'Dump by libraries', null);
$this->addOption('by-sources', null, InputOption::VALUE_REQUIRED, 'Dump by original sources (source.json)', null);
$this->addOption('dump-dir', null, InputOption::VALUE_REQUIRED, 'Change dump directory', BUILD_ROOT_PATH . '/license');
2023-03-18 17:32:21 +08:00
}
2023-04-15 18:46:46 +08:00
/**
* @throws WrongUsageException
* @throws FileSystemException
* @throws RuntimeException
*/
2023-04-22 17:45:43 +08:00
public function handle(): int
2023-03-18 17:32:21 +08:00
{
2023-04-15 18:46:46 +08:00
$dumper = new LicenseDumper();
2023-04-22 17:45:43 +08:00
if ($this->getOption('by-extensions') !== null) {
2023-04-15 18:46:46 +08:00
// 从参数中获取要编译的 extensions并转换为数组
2023-04-22 17:45:43 +08:00
$extensions = array_map('trim', array_filter(explode(',', $this->getOption('by-extensions'))));
2023-04-15 18:46:46 +08:00
// 根据提供的扩展列表获取依赖库列表并编译
[$extensions, $libraries, $not_included] = DependencyUtil::getExtLibsByDeps($extensions);
$dumper->addExts($extensions);
$dumper->addLibs($libraries);
2023-04-22 17:45:43 +08:00
if (!$this->getOption('without-php')) {
2023-04-15 18:46:46 +08:00
$dumper->addSources(['php-src']);
}
2023-04-22 17:45:43 +08:00
$dumper->dump($this->getOption('dump-dir'));
$this->output->writeln('Dump license with extensions: ' . implode(', ', $extensions));
$this->output->writeln('Dump license with libraries: ' . implode(', ', $libraries));
$this->output->writeln('Dump license with' . ($this->getOption('without-php') ? 'out' : '') . ' php-src');
$this->output->writeln('Dump target dir: ' . $this->getOption('dump-dir'));
2023-08-06 10:43:20 +08:00
return static::SUCCESS;
2023-04-15 18:46:46 +08:00
}
2023-04-22 17:45:43 +08:00
if ($this->getOption('by-libs') !== null) {
$libraries = array_map('trim', array_filter(explode(',', $this->getOption('by-libs'))));
2023-04-15 18:46:46 +08:00
$libraries = DependencyUtil::getLibsByDeps($libraries);
$dumper->addLibs($libraries);
2023-04-22 17:45:43 +08:00
$dumper->dump($this->getOption('dump-dir'));
$this->output->writeln('Dump target dir: ' . $this->getOption('dump-dir'));
2023-08-06 10:43:20 +08:00
return static::SUCCESS;
2023-04-15 18:46:46 +08:00
}
2023-04-22 17:45:43 +08:00
if ($this->getOption('by-sources') !== null) {
$sources = array_map('trim', array_filter(explode(',', $this->getOption('by-sources'))));
2023-04-15 18:46:46 +08:00
$dumper->addSources($sources);
2023-04-22 17:45:43 +08:00
$dumper->dump($this->getOption('dump-dir'));
$this->output->writeln('Dump target dir: ' . $this->getOption('dump-dir'));
2023-08-06 10:43:20 +08:00
return static::SUCCESS;
2023-04-15 18:46:46 +08:00
}
2023-04-22 17:45:43 +08:00
$this->output->writeln('You must use one of "--by-extensions=", "--by-libs=", "--by-sources=" to dump');
2023-08-06 10:43:20 +08:00
return static::FAILURE;
2023-03-18 17:32:21 +08:00
}
}