Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell committed Jul 26, 2024
1 parent 26d3119 commit 38e3163
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected function configure(): void
* {@inheritdoc}
*
* @throws \Exception
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand All @@ -99,7 +100,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);
}

$selfUpdateManager = $this->getSelfUpdateManager($input);
$isPreviewOptionSet = $input->getOption('preview');
$isStable = $input->getOption('stable') || !$isPreviewOptionSet;
if ($isPreviewOptionSet && $isStable) {
throw new \RuntimeException(self::SELF_UPDATE_COMMAND_NAME . ' support either stable or preview, not both.');
}

$isCompatibleOptionSet = $input->getOption('compatible');
$versionConstraintArg = $input->getArgument('version_constraint');

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

if ($selfUpdateManager->isUpToDate()) {
$output->writeln('No update available');
Expand Down Expand Up @@ -142,13 +156,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

public function getSelfUpdateManager(InputInterface $input): SelfUpdateManager {
$isPreviewOptionSet = $input->getOption('preview');
$isStable = $input->getOption('stable') || !$isPreviewOptionSet;
if ($isPreviewOptionSet && $isStable) {
throw new \RuntimeException(self::SELF_UPDATE_COMMAND_NAME . ' support either stable or preview, not both.');
}
return new SelfUpdateManager($this->gitHubRepository, $this->currentVersion, $this->applicationName, $isPreviewOptionSet, $input->getOption('compatible'), $input->getArgument('version_constraint'));
public function getSelfUpdateManager(array $options): SelfUpdateManager {
return new SelfUpdateManager($this->gitHubRepository, $this->currentVersion, $this->applicationName, $options);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/SelfUpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
*/
class SelfUpdateManager
{
protected array $options;
private ?array $latestRelease = null;

public function __construct(protected string $gitHubRepository, protected string $currentVersion, protected string $applicationName, protected bool $isPreviewOptionSet, protected bool $isCompatibleOptionSet, protected ?string $versionConstraintArg){}
public function __construct(protected string $gitHubRepository, protected string $currentVersion, protected string $applicationName, array $options){
// Options need to be passed in the constructor since we cache the latestRelease which may vary based on options.
$this->options = array_merge([
'preview' => false,
'compatible' => false,
'version_constraint' => null,
], $options);
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
Expand Down Expand Up @@ -48,17 +56,17 @@ public function getLatestReleaseFromGithub(): ?array
continue;
}

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

if (!$this->isPreviewOptionSet && ((VersionParser::parseStability($releaseVersion) !== 'stable') || $release['prerelease'])) {
if (!$this->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->versionConstraintArg && !Semver::satisfies($releaseVersion, $this->versionConstraintArg)) {
if (null !== $this->options['version_constraint'] && !Semver::satisfies($releaseVersion, $this->options['version_constraint'])) {
// Release version does not match version constraint option.
continue;
}
Expand Down

0 comments on commit 38e3163

Please sign in to comment.