diff --git a/src/SPC/command/DownloadCommand.php b/src/SPC/command/DownloadCommand.php index f77c80f9..dfa4c9ee 100644 --- a/src/SPC/command/DownloadCommand.php +++ b/src/SPC/command/DownloadCommand.php @@ -248,10 +248,9 @@ class DownloadCommand extends BaseCommand $alt_sources = Config::getSource($source)['alt'] ?? null; if ($alt_sources === null) { logger()->warning("No alternative sources found for {$source}, using default alternative source"); - $alt_config = array_merge($config, $this->getDefaultAlternativeSource($source)); + $alt_config = array_merge($config, Downloader::getDefaultAlternativeSource($source)); } elseif ($alt_sources === false) { - logger()->warning("No alternative sources found for {$source}, skipping alternative download"); - throw $e; + throw new DownloaderException("No alternative sources found for {$source}, skipping alternative download"); } else { logger()->notice("Trying to download alternative sources for {$source}"); $alt_config = array_merge($config, $alt_sources); @@ -399,27 +398,4 @@ class DownloadCommand extends BaseCommand } return static::FAILURE; } - - private function getDefaultAlternativeSource(string $source_name): array - { - return [ - 'type' => 'custom', - 'func' => function (bool $force, array $source, int $download_as) use ($source_name) { - logger()->debug("Fetching alternative source for {$source_name}"); - // get from dl.static-php.dev - $url = "https://dl.static-php.dev/static-php-cli/deps/spc-download-mirror/{$source_name}/?format=json"; - $json = json_decode(Downloader::curlExec(url: $url, retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0)), true); - if (!is_array($json)) { - throw new RuntimeException('failed http fetch'); - } - $item = $json[0] ?? null; - if ($item === null) { - throw new RuntimeException('failed to parse json'); - } - $full_url = 'https://dl.static-php.dev' . $item['full_path']; - $filename = basename($item['full_path']); - Downloader::downloadFile($source_name, $full_url, $filename, $source['path'] ?? null, $download_as); - }, - ]; - } } diff --git a/src/SPC/command/InstallPkgCommand.php b/src/SPC/command/InstallPkgCommand.php index b1ea3cd5..19d1532b 100644 --- a/src/SPC/command/InstallPkgCommand.php +++ b/src/SPC/command/InstallPkgCommand.php @@ -24,6 +24,8 @@ class InstallPkgCommand extends BaseCommand $this->addArgument('packages', InputArgument::REQUIRED, 'The packages will be installed, comma separated'); $this->addOption('shallow-clone', null, null, 'Clone shallow'); $this->addOption('custom-url', 'U', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Specify custom source download url, e.g "php-src:https://downloads.php.net/~eric/php-8.3.0beta1.tar.gz"'); + $this->addOption('no-alt', null, null, 'Do not download alternative packages'); + $this->addOption('skip-extract', null, null, 'Skip package extraction, just download the package archive'); } /** @@ -66,10 +68,20 @@ class InstallPkgCommand extends BaseCommand $new_config['filename'] = $config['filename']; } logger()->info("Installing source {$pkg} from custom url [{$ni}/{$cnt}]"); - PackageManager::installPackage($pkg, $new_config); + PackageManager::installPackage( + $pkg, + $new_config, + allow_alt: false, + extract: !$this->getOption('skip-extract') + ); } else { logger()->info("Fetching package {$pkg} [{$ni}/{$cnt}]"); - PackageManager::installPackage($pkg, Config::getPkg($pkg)); + PackageManager::installPackage( + $pkg, + Config::getPkg($pkg), + allow_alt: !$this->getOption('no-alt'), + extract: !$this->getOption('skip-extract') + ); } } $time = round(microtime(true) - START_TIME, 3); diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index 12881ae7..ed462489 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -608,6 +608,29 @@ class Downloader return "{$source}-{$os_family}-{$gnu_arch}-{$libc}-{$libc_version}"; } + public static function getDefaultAlternativeSource(string $source_name): array + { + return [ + 'type' => 'custom', + 'func' => function (bool $force, array $source, int $download_as) use ($source_name) { + logger()->debug("Fetching alternative source for {$source_name}"); + // get from dl.static-php.dev + $url = "https://dl.static-php.dev/static-php-cli/deps/spc-download-mirror/{$source_name}/?format=json"; + $json = json_decode(Downloader::curlExec(url: $url, retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0)), true); + if (!is_array($json)) { + throw new RuntimeException('failed http fetch'); + } + $item = $json[0] ?? null; + if ($item === null) { + throw new RuntimeException('failed to parse json'); + } + $full_url = 'https://dl.static-php.dev' . $item['full_path']; + $filename = basename($item['full_path']); + Downloader::downloadFile($source_name, $full_url, $filename, $source['path'] ?? null, $download_as); + }, + ]; + } + /** * Register CTRL+C event for different OS. * diff --git a/src/SPC/store/PackageManager.php b/src/SPC/store/PackageManager.php index d5c1043d..dc662a5b 100644 --- a/src/SPC/store/PackageManager.php +++ b/src/SPC/store/PackageManager.php @@ -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,31 @@ class PackageManager } // Download package - Downloader::downloadPackage($pkg_name, $config, $force); + try { + Downloader::downloadPackage($pkg_name, $config, $force); + } catch (\Throwable $e) { + if (!$allow_alt) { + 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');