mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 01:45:36 +08:00
Add parallel update checking and improve artifact update handling
This commit is contained in:
@@ -4,7 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace StaticPHP\Command;
|
||||
|
||||
use StaticPHP\Artifact\ArtifactCache;
|
||||
use StaticPHP\Artifact\ArtifactDownloader;
|
||||
use StaticPHP\Artifact\Downloader\Type\CheckUpdateResult;
|
||||
use StaticPHP\DI\ApplicationContext;
|
||||
use StaticPHP\Exception\SPCException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
@@ -17,9 +20,10 @@ class CheckUpdateCommand extends BaseCommand
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->addArgument('artifact', InputArgument::REQUIRED, 'The name of the artifact(s) to check for updates, comma-separated');
|
||||
$this->addArgument('artifact', InputArgument::OPTIONAL, 'The name of the artifact(s) to check for updates, comma-separated (default: all downloaded artifacts)');
|
||||
$this->addOption('json', null, null, 'Output result in JSON format');
|
||||
$this->addOption('bare', null, null, 'Check update without requiring the artifact to be downloaded first (old version will be null)');
|
||||
$this->addOption('parallel', 'p', InputOption::VALUE_REQUIRED, 'Number of parallel update checks (default: 10)', 10);
|
||||
|
||||
// --with-php option for checking updates with a specific PHP version context
|
||||
$this->addOption('with-php', null, InputOption::VALUE_REQUIRED, 'PHP version in major.minor format (default 8.4)', '8.4');
|
||||
@@ -27,17 +31,27 @@ class CheckUpdateCommand extends BaseCommand
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$artifacts = parse_comma_list($this->input->getArgument('artifact'));
|
||||
$artifact_arg = $this->input->getArgument('artifact');
|
||||
if ($artifact_arg === null) {
|
||||
$artifacts = ApplicationContext::get(ArtifactCache::class)->getCachedArtifactNames();
|
||||
if (empty($artifacts)) {
|
||||
$this->output->writeln('<comment>No downloaded artifacts found.</comment>');
|
||||
return static::OK;
|
||||
}
|
||||
} else {
|
||||
$artifacts = parse_comma_list($artifact_arg);
|
||||
}
|
||||
|
||||
try {
|
||||
$downloader = new ArtifactDownloader($this->input->getOptions());
|
||||
$bare = (bool) $this->getOption('bare');
|
||||
if ($this->getOption('json')) {
|
||||
$results = $downloader->checkUpdates($artifacts, bare: $bare);
|
||||
$outputs = [];
|
||||
foreach ($artifacts as $artifact) {
|
||||
$result = $downloader->checkUpdate($artifact, bare: $bare);
|
||||
foreach ($results as $artifact => $result) {
|
||||
$outputs[$artifact] = [
|
||||
'need-update' => $result->needUpdate,
|
||||
'unsupported' => $result->unsupported,
|
||||
'old' => $result->old,
|
||||
'new' => $result->new,
|
||||
];
|
||||
@@ -45,15 +59,17 @@ class CheckUpdateCommand extends BaseCommand
|
||||
$this->output->writeln(json_encode($outputs, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
return static::OK;
|
||||
}
|
||||
foreach ($artifacts as $artifact) {
|
||||
$result = $downloader->checkUpdate($artifact, bare: $bare);
|
||||
if (!$result->needUpdate) {
|
||||
$this->output->writeln("Artifact <info>{$artifact}</info> is already up to date (<comment>{$result->new}</comment>)");
|
||||
$downloader->checkUpdates($artifacts, bare: $bare, onResult: function (string $artifact, CheckUpdateResult $result) {
|
||||
if ($result->unsupported) {
|
||||
$this->output->writeln("Artifact <info>{$artifact}</info> does not support update checking, <comment>skipped</comment>");
|
||||
} elseif (!$result->needUpdate) {
|
||||
$ver = $result->new ? "(<comment>{$result->new}</comment>)" : '';
|
||||
$this->output->writeln("Artifact <info>{$artifact}</info> is already up to date {$ver}");
|
||||
} else {
|
||||
[$old, $new] = [$result->old ?? 'unavailable', $result->new ?? 'unknown'];
|
||||
$this->output->writeln("Update available for <info>{$artifact}</info>: <comment>{$old}</comment> -> <comment>{$new}</comment>");
|
||||
}
|
||||
}
|
||||
});
|
||||
return static::OK;
|
||||
} catch (SPCException $e) {
|
||||
$e->setSimpleOutput();
|
||||
|
||||
Reference in New Issue
Block a user