Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Aug 12, 2024
1 parent ed37ade commit 2d76f4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 52 deletions.
49 changes: 10 additions & 39 deletions src/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SelfUpdate;

use Composer\Semver\VersionParser;
use JetBrains\PhpStorm\NoReturn;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -20,39 +20,17 @@ class SelfUpdateCommand extends Command
{
public const SELF_UPDATE_COMMAND_NAME = 'self:update';

protected string $gitHubRepository;

protected string $currentVersion;

protected string $applicationName;

protected bool $ignorePharRunningCheck;

public function __construct(string $applicationName = null, string $currentVersion = null, string $gitHubRepository = null)
public function __construct(private readonly SelfUpdateManager $selfUpdateManager, private readonly bool $ignorePharRunningCheck = false)
{
$this->applicationName = $applicationName;
$version_parser = new VersionParser();
$this->currentVersion = $version_parser->normalize($currentVersion);
$this->gitHubRepository = $gitHubRepository;
$this->ignorePharRunningCheck = false;

parent::__construct(self::SELF_UPDATE_COMMAND_NAME);
}

/**
* Set ignorePharRunningCheck to true.
*/
public function ignorePharRunningCheck($ignore = true): void
{
$this->ignorePharRunningCheck = $ignore;
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$app = $this->applicationName;
$app = $this->selfUpdateManager->applicationName;

// Follow Composer's pattern of command and channel names.
$this
Expand All @@ -79,7 +57,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->ignorePharRunningCheck && empty(\Phar::running())) {
throw new \RuntimeException(self::SELF_UPDATE_COMMAND_NAME . ' only works when running the phar version of ' . $this->applicationName . '.');
throw new \RuntimeException(self::SELF_UPDATE_COMMAND_NAME . ' only works when running the phar version of ' . $this->selfUpdateManager->applicationName . '.');
}

$localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
Expand Down Expand Up @@ -109,22 +87,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$isCompatibleOptionSet = $input->getOption('compatible');
$versionConstraintArg = $input->getArgument('version_constraint');

$selfUpdateManager = $this->getSelfUpdateManager([
$options = [
'preview' => $isPreviewOptionSet,
'compatible' => $isCompatibleOptionSet,
'version_constraint' => $versionConstraintArg,
]);
];

if ($selfUpdateManager->isUpToDate()) {
if ($this->selfUpdateManager->isUpToDate($options)) {
$output->writeln('No update available');
return Command::SUCCESS;
}

$latestRelease = $selfUpdateManager->getLatestReleaseFromGithub();
$latestRelease = $this->selfUpdateManager->getLatestReleaseFromGithub($options);

$fs = new sfFilesystem();

$output->writeln('Downloading ' . $this->applicationName . ' (' . $this->gitHubRepository . ') ' . $latestRelease['tag_name']);
$output->writeln('Downloading ' . $this->selfUpdateManager->applicationName . ' (' . $this->selfUpdateManager->gitHubRepository . ') ' . $latestRelease['tag_name']);

$fs->copy($latestRelease['download_url'], $tempFilename);

Expand Down Expand Up @@ -152,12 +130,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return Command::FAILURE;
}
// This will never be reached, but it keeps static analysis tools happy :)
return Command::SUCCESS;
}

public function getSelfUpdateManager(array $options = []): SelfUpdateManager {
return new SelfUpdateManager($this->gitHubRepository, $this->currentVersion, $this->applicationName, $options);
}

/**
Expand All @@ -168,8 +140,7 @@ public function getSelfUpdateManager(array $options = []): SelfUpdateManager {
*
* @return void
*/
protected function _exit()
{
#[NoReturn] protected function _exit(): void {
exit;
}
}
28 changes: 15 additions & 13 deletions src/SelfUpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@
*/
class SelfUpdateManager
{
protected array $options;

public function __construct(protected string $gitHubRepository, protected string $currentVersion, protected string $applicationName, array $options){
$this->options = array_merge([
'preview' => false,
'compatible' => false,
'version_constraint' => null,
], $options);
public function __construct(public string $applicationName, public string $currentVersion, public string $gitHubRepository){
$version_parser = new VersionParser();
$this->currentVersion = $version_parser->normalize($currentVersion);
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function isUpToDate(): bool {
$latestRelease = $this->getLatestReleaseFromGithub();
public function isUpToDate(array $options = []): bool {
$latestRelease = $this->getLatestReleaseFromGithub($options);
return NULL === $latestRelease || Comparator::greaterThanOrEqualTo($this->currentVersion, $latestRelease['version']);
}

Expand All @@ -46,25 +42,31 @@ public function isUpToDate(): bool {
* available, otherwise - NULL.
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getLatestReleaseFromGithub(): ?array
public function getLatestReleaseFromGithub(array $options = []): ?array
{
$options = array_merge([
'preview' => false,
'compatible' => false,
'version_constraint' => null,
], $options);

foreach ($this->getReleasesFromGithub() as $releaseVersion => $release) {
// We do not care about this release if it does not contain assets.
if (!isset($release['assets'][0]) || !is_object($release['assets'][0])) {
continue;
}

if ($this->options['compatible'] && !$this->satisfiesMajorVersionConstraint($releaseVersion)) {
if ($options['compatible'] && !$this->satisfiesMajorVersionConstraint($releaseVersion)) {
// If it does not satisfy, look for the next one.
continue;
}

if (!$this->options['preview'] && ((VersionParser::parseStability($releaseVersion) !== 'stable') || $release['prerelease'])) {
if (!$options['preview'] && ((VersionParser::parseStability($releaseVersion) !== 'stable') || $release['prerelease'])) {
// If preview not requested and current version is not stable, look for the next one.
continue;
}

if (null !== $this->options['version_constraint'] && !Semver::satisfies($releaseVersion, $this->options['version_constraint'])) {
if (null !== $options['version_constraint'] && !Semver::satisfies($releaseVersion, $options['version_constraint'])) {
// Release version does not match version constraint option.
continue;
}
Expand Down

0 comments on commit 2d76f4b

Please sign in to comment.