Skip to content

Commit f500945

Browse files
authored
Merge pull request #833 from crazywhalecc/feat/install-pkg-options
Add --no-alt and --skip-extract option to install-pkg command
2 parents f543b55 + 52a623f commit f500945

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

src/SPC/command/DownloadCommand.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,9 @@ public function handle(): int
248248
$alt_sources = Config::getSource($source)['alt'] ?? null;
249249
if ($alt_sources === null) {
250250
logger()->warning("No alternative sources found for {$source}, using default alternative source");
251-
$alt_config = array_merge($config, $this->getDefaultAlternativeSource($source));
251+
$alt_config = array_merge($config, Downloader::getDefaultAlternativeSource($source));
252252
} elseif ($alt_sources === false) {
253-
logger()->warning("No alternative sources found for {$source}, skipping alternative download");
254-
throw $e;
253+
throw new DownloaderException("No alternative sources found for {$source}, skipping alternative download");
255254
} else {
256255
logger()->notice("Trying to download alternative sources for {$source}");
257256
$alt_config = array_merge($config, $alt_sources);
@@ -399,27 +398,4 @@ private function _clean(): int
399398
}
400399
return static::FAILURE;
401400
}
402-
403-
private function getDefaultAlternativeSource(string $source_name): array
404-
{
405-
return [
406-
'type' => 'custom',
407-
'func' => function (bool $force, array $source, int $download_as) use ($source_name) {
408-
logger()->debug("Fetching alternative source for {$source_name}");
409-
// get from dl.static-php.dev
410-
$url = "https://dl.static-php.dev/static-php-cli/deps/spc-download-mirror/{$source_name}/?format=json";
411-
$json = json_decode(Downloader::curlExec(url: $url, retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0)), true);
412-
if (!is_array($json)) {
413-
throw new RuntimeException('failed http fetch');
414-
}
415-
$item = $json[0] ?? null;
416-
if ($item === null) {
417-
throw new RuntimeException('failed to parse json');
418-
}
419-
$full_url = 'https://dl.static-php.dev' . $item['full_path'];
420-
$filename = basename($item['full_path']);
421-
Downloader::downloadFile($source_name, $full_url, $filename, $source['path'] ?? null, $download_as);
422-
},
423-
];
424-
}
425401
}

src/SPC/command/InstallPkgCommand.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function configure(): void
2424
$this->addArgument('packages', InputArgument::REQUIRED, 'The packages will be installed, comma separated');
2525
$this->addOption('shallow-clone', null, null, 'Clone shallow');
2626
$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"');
27+
$this->addOption('no-alt', null, null, 'Do not download alternative packages');
28+
$this->addOption('skip-extract', null, null, 'Skip package extraction, just download the package archive');
2729
}
2830

2931
/**
@@ -66,10 +68,20 @@ public function handle(): int
6668
$new_config['filename'] = $config['filename'];
6769
}
6870
logger()->info("Installing source {$pkg} from custom url [{$ni}/{$cnt}]");
69-
PackageManager::installPackage($pkg, $new_config);
71+
PackageManager::installPackage(
72+
$pkg,
73+
$new_config,
74+
allow_alt: false,
75+
extract: !$this->getOption('skip-extract')
76+
);
7077
} else {
7178
logger()->info("Fetching package {$pkg} [{$ni}/{$cnt}]");
72-
PackageManager::installPackage($pkg, Config::getPkg($pkg));
79+
PackageManager::installPackage(
80+
$pkg,
81+
Config::getPkg($pkg),
82+
allow_alt: !$this->getOption('no-alt'),
83+
extract: !$this->getOption('skip-extract')
84+
);
7385
}
7486
}
7587
$time = round(microtime(true) - START_TIME, 3);

src/SPC/store/Downloader.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,29 @@ public static function getPreBuiltLockName(string $source): string
608608
return "{$source}-{$os_family}-{$gnu_arch}-{$libc}-{$libc_version}";
609609
}
610610

611+
public static function getDefaultAlternativeSource(string $source_name): array
612+
{
613+
return [
614+
'type' => 'custom',
615+
'func' => function (bool $force, array $source, int $download_as) use ($source_name) {
616+
logger()->debug("Fetching alternative source for {$source_name}");
617+
// get from dl.static-php.dev
618+
$url = "https://dl.static-php.dev/static-php-cli/deps/spc-download-mirror/{$source_name}/?format=json";
619+
$json = json_decode(Downloader::curlExec(url: $url, retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0)), true);
620+
if (!is_array($json)) {
621+
throw new RuntimeException('failed http fetch');
622+
}
623+
$item = $json[0] ?? null;
624+
if ($item === null) {
625+
throw new RuntimeException('failed to parse json');
626+
}
627+
$full_url = 'https://dl.static-php.dev' . $item['full_path'];
628+
$filename = basename($item['full_path']);
629+
Downloader::downloadFile($source_name, $full_url, $filename, $source['path'] ?? null, $download_as);
630+
},
631+
];
632+
}
633+
611634
/**
612635
* Register CTRL+C event for different OS.
613636
*

src/SPC/store/PackageManager.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace SPC\store;
66

7+
use SPC\exception\DownloaderException;
78
use SPC\exception\FileSystemException;
89
use SPC\exception\WrongUsageException;
910
use SPC\store\pkg\CustomPackage;
1011

1112
class PackageManager
1213
{
13-
public static function installPackage(string $pkg_name, ?array $config = null, bool $force = false): void
14+
public static function installPackage(string $pkg_name, ?array $config = null, bool $force = false, bool $allow_alt = true, bool $extract = true): void
1415
{
1516
if ($config === null) {
1617
$config = Config::getPkg($pkg_name);
@@ -32,7 +33,31 @@ public static function installPackage(string $pkg_name, ?array $config = null, b
3233
}
3334

3435
// Download package
35-
Downloader::downloadPackage($pkg_name, $config, $force);
36+
try {
37+
Downloader::downloadPackage($pkg_name, $config, $force);
38+
} catch (\Throwable $e) {
39+
if (!$allow_alt) {
40+
throw new DownloaderException("Download package {$pkg_name} failed: " . $e->getMessage());
41+
}
42+
// if download failed, we will try to download alternative packages
43+
logger()->warning("Download package {$pkg_name} failed: " . $e->getMessage());
44+
$alt = $config['alt'] ?? null;
45+
if ($alt === null) {
46+
logger()->warning("No alternative package found for {$pkg_name}, using default mirror.");
47+
$alt_config = array_merge($config, Downloader::getDefaultAlternativeSource($pkg_name));
48+
} elseif ($alt === false) {
49+
logger()->error("No alternative package found for {$pkg_name}.");
50+
throw $e;
51+
} else {
52+
logger()->notice("Trying alternative package for {$pkg_name}.");
53+
$alt_config = array_merge($config, $alt);
54+
}
55+
Downloader::downloadPackage($pkg_name, $alt_config, $force);
56+
}
57+
if (!$extract) {
58+
logger()->info("Package [{$pkg_name}] downloaded, but extraction is skipped.");
59+
return;
60+
}
3661
if (Config::getPkg($pkg_name)['type'] === 'custom') {
3762
// Custom extract function
3863
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/pkg', 'SPC\store\pkg');

0 commit comments

Comments
 (0)