Add --no-alt and --skip-extract option to install-pkg command

This commit is contained in:
crazywhalecc
2025-07-24 00:42:30 +08:00
parent f543b55f19
commit abd7d3cfb4
4 changed files with 67 additions and 29 deletions

View File

@@ -4,13 +4,14 @@ declare(strict_types=1);
namespace SPC\store;
use SPC\exception\DownloaderException;
use SPC\exception\FileSystemException;
use SPC\exception\WrongUsageException;
use SPC\store\pkg\CustomPackage;
class PackageManager
{
public static function installPackage(string $pkg_name, ?array $config = null, bool $force = false): void
public static function installPackage(string $pkg_name, ?array $config = null, bool $force = false, bool $allow_alt = true, bool $extract = true): void
{
if ($config === null) {
$config = Config::getPkg($pkg_name);
@@ -32,7 +33,32 @@ class PackageManager
}
// Download package
Downloader::downloadPackage($pkg_name, $config, $force);
try {
Downloader::downloadPackage($pkg_name, $config, $force);
} catch (\Throwable $e) {
if (!$allow_alt) {
logger()->error("Download package {$pkg_name} failed: " . $e->getMessage());
throw new DownloaderException("Download package {$pkg_name} failed: " . $e->getMessage());
}
// if download failed, we will try to download alternative packages
logger()->warning("Download package {$pkg_name} failed: " . $e->getMessage());
$alt = $config['alt'] ?? null;
if ($alt === null) {
logger()->warning("No alternative package found for {$pkg_name}, using default mirror.");
$alt_config = array_merge($config, Downloader::getDefaultAlternativeSource($pkg_name));
} elseif ($alt === false) {
logger()->error("No alternative package found for {$pkg_name}.");
throw $e;
} else {
logger()->notice("Trying alternative package for {$pkg_name}.");
$alt_config = array_merge($config, $alt);
}
Downloader::downloadPackage($pkg_name, $alt_config, $force);
}
if (!$extract) {
logger()->info("Package [{$pkg_name}] downloaded, but extraction is skipped.");
return;
}
if (Config::getPkg($pkg_name)['type'] === 'custom') {
// Custom extract function
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/pkg', 'SPC\store\pkg');