Skip to content

Commit 557dce1

Browse files
padraictheofidry
authored andcommitted
Add a self-update command (#51)
1 parent ca19958 commit 557dce1

File tree

10 files changed

+380
-124
lines changed

10 files changed

+380
-124
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/bin/php-scoper.phar
2+
/bin/php-scoper.phar.pubkey
3+
/bin/php-scoper-old.phar
4+
/bin/php-scoper-old.phar.pubkey
5+
/bin/php-scoper.phar.temp.pubkey
26
/box.json
37
/dist/
48
/build/

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ build: vendor vendor-bin/box/vendor
2424

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

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

9393
bin/scoper.phar: bin/php-scoper src vendor scoper.inc.php
94-
$(MAKE) build
94+
$(MAKE) CONFIG="-c box.json.dist" build

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": "^7.0",
2222
"nikic/php-parser": "^3.0",
2323
"ocramius/package-versions": "^1.1",
24-
"padraic/phar-updater": "^1.0.3",
24+
"padraic/phar-updater": "^1.0",
2525
"symfony/console": "^3.2",
2626
"symfony/filesystem": "^3.2",
2727
"symfony/finder": "^3.2"

composer.lock

Lines changed: 17 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ file that was distributed with this source code.
2727
<exclude>
2828
<file>src/Console/Application.php</file>
2929
<file>src/Console/Configuration.php</file>
30+
<file>src/Console/Command/SelfUpdateCommand.php</file>
3031
<directory>src/Logger</directory>
3132
<directory>src/NodeVisitor</directory>
3233
<file>src/functions.php</file>
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\Console\Command;
16+
17+
use Humbug\PhpScoper\Logger\UpdateConsoleLogger;
18+
use Humbug\SelfUpdate\Updater;
19+
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Input\InputOption;
22+
use Symfony\Component\Console\Output\OutputInterface;
23+
use Symfony\Component\Console\Style\SymfonyStyle;
24+
25+
final class SelfUpdateCommand extends Command
26+
{
27+
/** @internal */
28+
const REMOTE_FILENAME = 'php-scoper.phar';
29+
/** @internal */
30+
const STABILITY_STABLE = 'stable';
31+
/** @internal */
32+
const PACKAGIST_PACKAGE_NAME = 'humbug/php-scoper';
33+
/** @internal */
34+
const ROLLBACK_OPT = 'rollback';
35+
/** @internal */
36+
const CHECK_OPT = 'check';
37+
38+
/**
39+
* @var Updater
40+
*/
41+
private $updater;
42+
43+
/**
44+
* @var string
45+
*/
46+
private $version;
47+
48+
/**
49+
* @var UpdateConsoleLogger
50+
*/
51+
private $logger;
52+
53+
/**
54+
* @param Updater $updater
55+
*/
56+
public function __construct(Updater $updater)
57+
{
58+
parent::__construct();
59+
60+
$this->updater = $updater;
61+
}
62+
63+
/**
64+
* @inheritdoc
65+
*/
66+
protected function configure()
67+
{
68+
$this
69+
->setName('self-update')
70+
->setDescription(sprintf(
71+
'Update %s to most recent stable build.',
72+
$this->getLocalPharName()
73+
))
74+
->addOption(
75+
self::ROLLBACK_OPT,
76+
'r',
77+
InputOption::VALUE_NONE,
78+
'Rollback to previous version of PHP-Scoper if available on filesystem.'
79+
)
80+
->addOption(
81+
self::CHECK_OPT,
82+
'c',
83+
InputOption::VALUE_NONE,
84+
'Checks whether an update is available.'
85+
)
86+
;
87+
}
88+
89+
/**
90+
* @inheritdoc
91+
*/
92+
protected function execute(InputInterface $input, OutputInterface $output)
93+
{
94+
$this->logger = new UpdateConsoleLogger(
95+
new SymfonyStyle($input, $output)
96+
);
97+
98+
$this->version = $this->getApplication()->getVersion();
99+
100+
$this->configureUpdater();
101+
102+
if ($input->getOption('rollback')) {
103+
$this->rollback();
104+
105+
return 0;
106+
}
107+
108+
if ($input->getOption('check')) {
109+
$this->printAvailableUpdates();
110+
111+
return 0;
112+
}
113+
114+
$this->update();
115+
}
116+
117+
private function configureUpdater()
118+
{
119+
$this->updater->setStrategy(Updater::STRATEGY_GITHUB);
120+
$this->updater->getStrategy()->setPackageName(self::PACKAGIST_PACKAGE_NAME);
121+
$this->updater->getStrategy()->setPharName(self::REMOTE_FILENAME);
122+
$this->updater->getStrategy()->setCurrentLocalVersion($this->version);
123+
}
124+
125+
private function update()
126+
{
127+
$this->logger->startUpdating();
128+
try {
129+
$result = $this->updater->update();
130+
131+
$newVersion = $this->updater->getNewVersion();
132+
$oldVersion = $this->updater->getOldVersion();
133+
134+
if ($result) {
135+
$this->logger->updateSuccess($newVersion, $oldVersion);
136+
} else {
137+
$this->logger->updateNotNeeded($oldVersion);
138+
}
139+
} catch (\Throwable $e) {
140+
$this->logger->error($e);
141+
throw $e;
142+
}
143+
}
144+
145+
private function rollback()
146+
{
147+
try {
148+
$result = $this->updater->rollback();
149+
if ($result) {
150+
$this->logger->rollbackSuccess();
151+
} else {
152+
$this->logger->rollbackFail();
153+
}
154+
} catch (\Throwable $e) {
155+
$this->logger->error($e);
156+
throw $e;
157+
}
158+
}
159+
160+
private function printAvailableUpdates()
161+
{
162+
$this->logger->printLocalVersion($this->version);
163+
$this->printCurrentStableVersion();
164+
}
165+
166+
private function printCurrentStableVersion()
167+
{
168+
$stability = self::STABILITY_STABLE;
169+
170+
try {
171+
if ($this->updater->hasUpdate()) {
172+
$this->logger->printRemoteVersion(
173+
$stability,
174+
$updater->getNewVersion()
175+
);
176+
} elseif (false == $this->updater->getNewVersion()) {
177+
$this->logger->noNewRemoteVersions($stability);
178+
} else {
179+
$this->logger->currentVersionInstalled($stability);
180+
}
181+
} catch (\Throwable $e) {
182+
$this->logger->error($e);
183+
throw $e;
184+
}
185+
}
186+
187+
private function getLocalPharName(): string
188+
{
189+
return basename(\PHAR::running());
190+
}
191+
}

0 commit comments

Comments
 (0)