Skip to content

Commit

Permalink
fix: resolve Phar updater strategies
Browse files Browse the repository at this point in the history
This makes sure that the Phar name is set for the GitHub Releases strategy.
  • Loading branch information
owenvoke committed Aug 25, 2023
1 parent 3050bf8 commit 477b05c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
28 changes: 19 additions & 9 deletions src/Components/Updater/Provider.php
Expand Up @@ -14,10 +14,12 @@
namespace LaravelZero\Framework\Components\Updater;

use Humbug\SelfUpdate\Updater as PharUpdater;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use LaravelZero\Framework\Components\AbstractComponentProvider;
use LaravelZero\Framework\Components\Updater\Strategy\GithubStrategy;
use LaravelZero\Framework\Components\Updater\Strategy\StrategyInterface;
use LaravelZero\Framework\Providers\Build\Build;
use Phar;

use function class_exists;

Expand All @@ -31,7 +33,7 @@ final class Provider extends AbstractComponentProvider
*/
public function isAvailable(): bool
{
return class_exists(\Humbug\SelfUpdate\Updater::class);
return class_exists(PharUpdater::class);
}

/**
Expand Down Expand Up @@ -67,19 +69,27 @@ public function register(): void
$this->app->singleton(Updater::class, function () use ($build) {
$updater = new PharUpdater($build->getPath(), false, PharUpdater::STRATEGY_GITHUB);

$composer = json_decode(file_get_contents(base_path('composer.json')), true);
$name = $composer['name'];
$composer = json_decode(file_get_contents($this->app->basePath('composer.json')), true);

$strategy = $this->app['config']->get('updater.strategy', GithubStrategy::class);
/** @var ConfigRepository $config */
$config = $this->app->make(ConfigRepository::class);

$updater->setStrategyObject($this->app->make($strategy));
$strategyClass = $config->get('updater.strategy', GithubStrategy::class);

if ($updater->getStrategy() instanceof StrategyInterface) {
$updater->getStrategy()->setPackageName($name);
$updater->setStrategyObject($strategy = $this->app->make($strategyClass));

if ($strategy instanceof StrategyInterface) {
assert(isset($composer['name']), 'Package name has not been set in Composer');

$strategy->setPackageName($composer['name']);
}

if (method_exists($strategy, 'setPharName')) {
$strategy->setPharName($config->get('updater.phar_name') ?? basename(Phar::running()));
}

if (method_exists($updater->getStrategy(), 'setCurrentLocalVersion')) {
$updater->getStrategy()->setCurrentLocalVersion(config('app.version'));
if (method_exists($strategy, 'setCurrentLocalVersion')) {
$strategy->setCurrentLocalVersion($config->get('app.version'));
}

return new Updater($updater);
Expand Down
20 changes: 20 additions & 0 deletions src/Components/Updater/Strategy/Concerns/UsesPharName.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaravelZero\Framework\Components\Updater\Strategy\Concerns;

trait UsesPharName
{
private string $pharName;

public function setPharName($name): void
{
$this->pharName = $name;
}

public function getPharName(): string
{
return $this->pharName;
}
}
9 changes: 4 additions & 5 deletions src/Components/Updater/Strategy/GithubStrategy.php
Expand Up @@ -2,19 +2,18 @@

namespace LaravelZero\Framework\Components\Updater\Strategy;

use Phar;
use LaravelZero\Framework\Components\Updater\Strategy\Concerns\UsesPharName;

final class GithubStrategy extends \Humbug\SelfUpdate\Strategy\GithubStrategy implements StrategyInterface
{
/**
* Returns the Download Url.
*/
use UsesPharName;

protected function getDownloadUrl(array $package): string
{
$downloadUrl = parent::getDownloadUrl($package);

$downloadUrl = str_replace('releases/download', 'raw', $downloadUrl);

return $downloadUrl.'/builds/'.basename(Phar::running());
return "{$downloadUrl}/builds/{$this->getPharName()}";
}
}
9 changes: 4 additions & 5 deletions src/Components/Updater/Strategy/GitlabStrategy.php
Expand Up @@ -2,19 +2,18 @@

namespace LaravelZero\Framework\Components\Updater\Strategy;

use Phar;
use LaravelZero\Framework\Components\Updater\Strategy\Concerns\UsesPharName;

class GitlabStrategy extends \Humbug\SelfUpdate\Strategy\GithubStrategy implements StrategyInterface
{
/**
* Returns the Download Url.
*/
use UsesPharName;

protected function getDownloadUrl(array $package): string
{
$downloadUrl = parent::getDownloadUrl($package);

$downloadUrl = str_replace('releases/download', '-/raw', $downloadUrl);

return $downloadUrl.'/builds/'.basename(Phar::running());
return "{$downloadUrl}/builds/{$this->getPharName()}";
}
}
15 changes: 15 additions & 0 deletions src/Components/Updater/config/updater.php
Expand Up @@ -17,4 +17,19 @@

'strategy' => GithubStrategy::class,

/*
|--------------------------------------------------------------------------
| Self-updater Phar Name
|--------------------------------------------------------------------------
|
| Here you may specify the name of the Phar file, as stored on GitHub or
| GitLab. This can be configured if the Phar name is different to the
| name of the Phar file running on the users' machine.
|
| Default: `null`
|
*/

'phar_name' => null,

];

0 comments on commit 477b05c

Please sign in to comment.