Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve Phar updater strategies #481

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a way to set the package name without Composer, as there is no real guarantee that the compiled binary is run in a directory with a composer.json file, nor do I think there should need to be?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the composer.json within the Phar file. It should always exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that's weird, because for me, it tries to load it from the working directory.
hydephp/cli@925f00b It could be due to how my paths are setup though, in which case this is my fault. Gonna check it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is my bad. My application sets the working base path to be the working directory. For my usages, I would need this to be an absolute path, but that is probably not something Zero should worry about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: Changing the line to the following works better for me.

$composer = json_decode(file_get_contents(__DIR__.'/../../../../../../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,

];