Files
duckbrain-toolbox/packages/commands/src/Toolbox/Commands/SelfUpdateCommand.php
T

49 lines
1.5 KiB
PHP

<?php
namespace Toolbox\Commands;
use Toolbox\Config;
use Toolbox\SelfUpdater;
use Toolbox\Semver;
use Toolbox\ToolboxException;
final class SelfUpdateCommand extends Command
{
public static function describe(): string
{
return 'Actualiza el instalador global (script/phar)';
}
public function run(array $args): int
{
$parsed = $this->parse($args);
$from = $this->option($parsed['options'], 'from', Config::updateUrl());
$target = $this->option($parsed['options'], 'target', Config::binPath());
$temp = $this->option($parsed['options'], 'temp', sys_get_temp_dir());
$force = $this->flag($parsed['options'], 'force');
if ($from === null || $target === null) {
$this->console->error('Faltan --from o --target.');
return 1;
}
$updater = new SelfUpdater($this->console);
try {
if ($this->flag($parsed['options'], 'check')) {
$tag = $updater->available($from);
$this->console->out(' actual: v' . Config::VERSION);
$this->console->out(' disponible: ' . ($tag !== null ? 'v' . ltrim($tag, 'vV') : '(desconocido)'));
return $tag === null || Semver::compare(Config::VERSION, ltrim($tag, 'vV')) >= 0 ? 0 : 1;
}
$updater->update($from, $target, $temp, $force);
} catch (ToolboxException $e) {
$this->console->error($e->getMessage());
return 1;
}
return 0;
}
}