enhancement for download command

This commit is contained in:
crazywhalecc 2024-03-10 10:58:58 +08:00 committed by Jerry Ma
parent 71017361b5
commit e23daaa355
5 changed files with 65 additions and 38 deletions

View File

@ -38,26 +38,58 @@ class DownloadCommand extends BaseCommand
$this->addOption('for-extensions', 'e', InputOption::VALUE_REQUIRED, 'Fetch by extensions, e.g "openssl,mbstring"'); $this->addOption('for-extensions', 'e', InputOption::VALUE_REQUIRED, 'Fetch by extensions, e.g "openssl,mbstring"');
$this->addOption('for-libs', 'l', InputOption::VALUE_REQUIRED, 'Fetch by libraries, e.g "libcares,openssl,onig"'); $this->addOption('for-libs', 'l', InputOption::VALUE_REQUIRED, 'Fetch by libraries, e.g "libcares,openssl,onig"');
$this->addOption('without-suggestions', null, null, 'Do not fetch suggested sources when using --for-extensions'); $this->addOption('without-suggestions', null, null, 'Do not fetch suggested sources when using --for-extensions');
$this->addOption('ignore-cache-sources', null, InputOption::VALUE_REQUIRED, 'Ignore some source caches, comma separated, e.g "php-src,curl,openssl"', '');
} }
/**
* @throws FileSystemException
* @throws WrongUsageException
*/
public function initialize(InputInterface $input, OutputInterface $output): void public function initialize(InputInterface $input, OutputInterface $output): void
{ {
if ( // mode: --all
$input->getOption('all') if ($input->getOption('all')) {
|| $input->getOption('clean') $input->setArgument('sources', implode(',', array_keys(Config::getSources())));
|| $input->getOption('from-zip') parent::initialize($input, $output);
|| $input->getOption('for-extensions') return;
|| $input->getOption('for-libs') }
) { // mode: --clean and --from-zip
if ($input->getOption('clean') || $input->getOption('from-zip')) {
$input->setArgument('sources', ''); $input->setArgument('sources', '');
parent::initialize($input, $output);
return;
}
// mode: normal
if (!empty($input->getArgument('sources'))) {
$final_sources = array_map('trim', array_filter(explode(',', $input->getArgument('sources'))));
} else {
$final_sources = [];
}
// mode: --for-extensions
if ($for_ext = $input->getOption('for-extensions')) {
$ext = array_map('trim', array_filter(explode(',', $for_ext)));
$sources = $this->calculateSourcesByExt($ext, !$input->getOption('without-suggestions'));
if (PHP_OS_FAMILY !== 'Windows') {
array_unshift($sources, 'pkg-config');
}
array_unshift($sources, 'php-src', 'micro');
$final_sources = array_merge($final_sources, array_diff($sources, $final_sources));
}
// mode: --for-libs
if ($for_lib = $input->getOption('for-libs')) {
$lib = array_map('trim', array_filter(explode(',', $for_lib)));
$sources = $this->calculateSourcesByLib($lib, !$input->getOption('without-suggestions'));
$final_sources = array_merge($final_sources, array_diff($sources, $final_sources));
}
if (!empty($final_sources)) {
$input->setArgument('sources', implode(',', $final_sources));
} }
parent::initialize($input, $output); parent::initialize($input, $output);
} }
/** /**
* @throws DownloaderException
* @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException
*/ */
public function handle(): int public function handle(): int
{ {
@ -109,23 +141,12 @@ class DownloadCommand extends BaseCommand
Config::$source['openssl']['regex'] = '/href="(?<file>openssl-(?<version>1.[^"]+)\.tar\.gz)\"/'; Config::$source['openssl']['regex'] = '/href="(?<file>openssl-(?<version>1.[^"]+)\.tar\.gz)\"/';
} }
// --for-extensions $chosen_sources = array_map('trim', array_filter(explode(',', $this->getArgument('sources'))));
if ($for_ext = $this->getOption('for-extensions')) { $force_list = array_map('trim', array_filter(explode(',', $this->getOption('ignore-cache-sources'))));
$ext = array_map('trim', array_filter(explode(',', $for_ext)));
$sources = $this->calculateSourcesByExt($ext, !$this->getOption('without-suggestions')); if ($this->getOption('all')) {
array_unshift($sources, 'php-src', 'micro', 'pkg-config');
} elseif ($for_lib = $this->getOption('for-libs')) {
$lib = array_map('trim', array_filter(explode(',', $for_lib)));
$sources = $this->calculateSourcesByLib($lib, !$this->getOption('without-suggestions'));
} else {
// get source list that will be downloaded
$sources = array_map('trim', array_filter(explode(',', $this->getArgument('sources'))));
if (empty($sources)) {
logger()->notice('Downloading with --all option will take more times to download, we recommend you to download with --for-extensions option !'); logger()->notice('Downloading with --all option will take more times to download, we recommend you to download with --for-extensions option !');
$sources = array_keys(Config::getSources());
} }
}
$chosen_sources = $sources;
// Process -U options // Process -U options
$custom_urls = []; $custom_urls = [];
@ -156,7 +177,7 @@ class DownloadCommand extends BaseCommand
Downloader::downloadSource($source, $new_config, true); Downloader::downloadSource($source, $new_config, true);
} else { } else {
logger()->info("Fetching source {$source} [{$ni}/{$cnt}]"); logger()->info("Fetching source {$source} [{$ni}/{$cnt}]");
Downloader::downloadSource($source, Config::getSource($source)); Downloader::downloadSource($source, Config::getSource($source), in_array($source, $force_list));
} }
} }
$time = round(microtime(true) - START_TIME, 3); $time = round(microtime(true) - START_TIME, 3);
@ -171,6 +192,10 @@ class DownloadCommand extends BaseCommand
} }
} }
/**
* @throws RuntimeException
* @throws WrongUsageException
*/
private function downloadFromZip(string $path): int private function downloadFromZip(string $path): int
{ {
if (!file_exists($path)) { if (!file_exists($path)) {
@ -196,8 +221,10 @@ class DownloadCommand extends BaseCommand
if (PHP_OS_FAMILY !== 'Windows') { if (PHP_OS_FAMILY !== 'Windows') {
$abs_path = realpath($path); $abs_path = realpath($path);
f_passthru('mkdir ' . DOWNLOAD_PATH . ' && cd ' . DOWNLOAD_PATH . ' && unzip ' . escapeshellarg($abs_path)); f_passthru('mkdir ' . DOWNLOAD_PATH . ' && cd ' . DOWNLOAD_PATH . ' && unzip ' . escapeshellarg($abs_path));
} } else {
// Windows TODO // Windows TODO
throw new WrongUsageException('Windows currently does not support --from-zip !');
}
if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) { if (!file_exists(DOWNLOAD_PATH . '/.lock.json')) {
throw new RuntimeException('.lock.json not exist in "downloads/"'); throw new RuntimeException('.lock.json not exist in "downloads/"');

View File

@ -246,6 +246,10 @@ class Downloader
}*/ }*/
} }
/**
* @throws DownloaderException
* @throws FileSystemException
*/
public static function downloadPackage(string $name, ?array $pkg = null, bool $force = false): void public static function downloadPackage(string $name, ?array $pkg = null, bool $force = false): void
{ {
if ($pkg === null) { if ($pkg === null) {
@ -313,7 +317,7 @@ class Downloader
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/source', 'SPC\\store\\source'); $classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/source', 'SPC\\store\\source');
foreach ($classes as $class) { foreach ($classes as $class) {
if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) { if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) {
(new $class())->fetch(); (new $class())->fetch($force);
break; break;
} }
} }
@ -407,7 +411,7 @@ class Downloader
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/source', 'SPC\\store\\source'); $classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/source', 'SPC\\store\\source');
foreach ($classes as $class) { foreach ($classes as $class) {
if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) { if (is_a($class, CustomSourceBase::class, true) && $class::NAME === $name) {
(new $class())->fetch(); (new $class())->fetch($force);
break; break;
} }
} }

View File

@ -8,5 +8,5 @@ abstract class CustomSourceBase
{ {
public const NAME = 'unknown'; public const NAME = 'unknown';
abstract public function fetch(); abstract public function fetch(bool $force = false);
} }

View File

@ -7,7 +7,6 @@ namespace SPC\store\source;
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
use SPC\exception\DownloaderException; use SPC\exception\DownloaderException;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\Downloader; use SPC\store\Downloader;
class PhpSource extends CustomSourceBase class PhpSource extends CustomSourceBase
@ -16,13 +15,12 @@ class PhpSource extends CustomSourceBase
/** /**
* @throws DownloaderException * @throws DownloaderException
* @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function fetch(): void public function fetch(bool $force = false): void
{ {
$major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.1'; $major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.1';
Downloader::downloadSource('php-src', self::getLatestPHPInfo($major)); Downloader::downloadSource('php-src', self::getLatestPHPInfo($major), $force);
} }
/** /**

View File

@ -6,7 +6,6 @@ namespace SPC\store\source;
use SPC\exception\DownloaderException; use SPC\exception\DownloaderException;
use SPC\exception\FileSystemException; use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\store\Downloader; use SPC\store\Downloader;
class PostgreSQLSource extends CustomSourceBase class PostgreSQLSource extends CustomSourceBase
@ -15,12 +14,11 @@ class PostgreSQLSource extends CustomSourceBase
/** /**
* @throws DownloaderException * @throws DownloaderException
* @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
public function fetch(): void public function fetch(bool $force = false): void
{ {
Downloader::downloadSource('postgresql', self::getLatestInfo()); Downloader::downloadSource('postgresql', self::getLatestInfo(), $force);
} }
/** /**