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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a basic SelfUpdateCommand using phar-updater v1 #51

Merged
merged 19 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/bin/php-scoper.phar
/bin/php-scoper.phar.pubkey
/bin/php-scoper-old.phar
/bin/php-scoper-old.phar.pubkey
/bin/php-scoper.phar.temp.pubkey
/box.json
/dist/
/build/
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build: vendor vendor-bin/box/vendor

php -d zend.enable_gc=0 bin/php-scoper add-prefix --force
composer dump-autoload -d build --classmap-authoritative
php -d zend.enable_gc=0 -d phar.readonly=0 $(BOX) build
php -d zend.enable_gc=0 -d phar.readonly=0 $(BOX) build $(CONFIG)

# Install back all the dependencies
composer install
Expand Down Expand Up @@ -91,4 +91,4 @@ vendor-bin/box/composer.lock: vendor-bin/box/composer.json
@echo compose.lock is not up to date.

bin/scoper.phar: bin/php-scoper src vendor scoper.inc.php
$(MAKE) build
$(MAKE) CONFIG="-c box.json.dist" build
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"php": "^7.0",
"nikic/php-parser": "^3.0",
"ocramius/package-versions": "^1.1",
"padraic/phar-updater": "^1.0.3",
"padraic/phar-updater": "^1.0",
"symfony/console": "^3.2",
"symfony/filesystem": "^3.2",
"symfony/finder": "^3.2"
Expand Down
100 changes: 17 additions & 83 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions src/Console/Command/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\Console\Command;

use Humbug\PhpScoper\Logger\UpdateConsoleLogger;
use Humbug\SelfUpdate\Updater;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class SelfUpdateCommand extends Command
{
/** @internal */
const REMOTE_FILENAME = 'php-scoper.phar';
/** @internal */
const STABILITY_STABLE = 'stable';
/** @internal */
const PACKAGIST_PACKAGE_NAME = 'humbug/php-scoper';
/** @internal */
const ROLLBACK_OPT = 'rollback';
/** @internal */
const CHECK_OPT = 'check';

/**
* @var Updater
*/
private $updater;

/**
* @var OutputInterface
*/
private $output;

/**
* @var string
*/
private $version;

/**
* @var UpdateConsoleLogger
*/
private $logger;

/**
* @param Updater $updater
*/
public function __construct(Updater $updater)
{
parent::__construct();

$this->updater = $updater;
}

/**
* @inheritdoc
*/
protected function configure()
{
$this
->setName('self-update')
->setDescription(sprintf(
'Update %s to most recent stable build.',
$this->getLocalPharName()
))
->addOption(
self::ROLLBACK_OPT,
'r',
InputOption::VALUE_NONE,
'Rollback to previous version of PHP-Scoper if available on filesystem.'
)
->addOption(
self::CHECK_OPT,
'c',
InputOption::VALUE_NONE,
'Checks whether an update is available.'
)
;
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->logger = new UpdateConsoleLogger(
new SymfonyStyle($input, $output)
);

$this->output = $output;

$this->version = $this->getApplication()->getVersion();
Copy link
Member

Choose a reason for hiding this comment

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

can this be done in the constructor?


if ($input->getOption('rollback')) {
$this->rollback();

return 0;
}

if ($input->getOption('check')) {
$this->printAvailableUpdates();

return 0;
}

$this->update($this->createUpdater());
}

private function createUpdater(): Updater
{
$this->updater->setStrategy(Updater::STRATEGY_GITHUB);
$this->updater->getStrategy()->setPackageName(self::PACKAGIST_PACKAGE_NAME);
$this->updater->getStrategy()->setPharName(self::REMOTE_FILENAME);
$this->updater->getStrategy()->setCurrentLocalVersion($this->version);

return $this->updater;
}

private function update(Updater $updater)
{
$this->logger->startUpdating();
try {
$result = $this->updater->update();

$newVersion = $this->updater->getNewVersion();
$oldVersion = $this->updater->getOldVersion();

if ($result) {
$this->logger->updateSuccess($newVersion, $oldVersion);
} else {
$this->logger->updateNotNeeded($oldVersion);
}
} catch (\Throwable $e) {
$this->logger->error($e);
Copy link
Member

Choose a reason for hiding this comment

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

we're not failing here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mostly, it's just to add some preceding text to assure users the phar was not replaced. We could let the Throwable just fall through, but I always find that a bit untidy.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure about this though as we rule out any way for the user to know why it failed. I think throwing the throwable after logging the message would be fine

throw $e;
}
}

private function rollback()
{
try {
$result = $this->updater->rollback();
if ($result) {
$this->logger->rollbackSuccess();
} else {
$this->logger->rollbackFail();
}
} catch (\Throwable $e) {
$this->logger->error($e);
throw $e;
}
}

private function printAvailableUpdates()
{
$this->logger->printLocalVersion($this->version);
$this->printCurrentStableVersion();
}

private function printCurrentStableVersion()
{
$updater = $this->createUpdater();
$stability = self::STABILITY_STABLE;

try {
if ($updater->hasUpdate()) {
$this->logger->printRemoteVersion(
$stability,
$updater->getNewVersion()
);
} elseif (false == $updater->getNewVersion()) {
$this->logger->noNewRemoteVersions($stability);
} else {
$this->logger->currentVersionInstalled($stability);
}
} catch (\Throwable $e) {
$this->logger->error($e);
throw $e;
}
}

private function getLocalPharName(): string
{
return basename(\PHAR::running());
Copy link
Member

Choose a reason for hiding this comment

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

This command doesn't work without a PHAR, but can we ensure it fails gracefully?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you clarify? The SelfUpdateCommand only ever exists in a PHAR, so the above running() call should always succeed.

}
}
Loading