-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathSelfUpdateCommand.php
81 lines (68 loc) · 2.58 KB
/
SelfUpdateCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @author hollodotme
*/
namespace CodeClimate\PhpTestReporter\ConsoleCommands;
use CodeClimate\PhpTestReporter\Constants\PharTool;
use Humbug\SelfUpdate\Exception\HttpRequestException;
use Humbug\SelfUpdate\Strategy\GithubStrategy;
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\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class SelfUpdateCommand
* @package CodeClimate\PhpTestReporter\ConsoleCommands
*/
class SelfUpdateCommand extends Command
{
protected function configure()
{
$this->setAliases(array( 'selfupdate' ));
$this->setDescription('Updates this PHAR to latest version.');
$this->addOption(
'stability',
's',
InputOption::VALUE_OPTIONAL,
sprintf(
'Specify the stability (%s, %s or %s)',
GithubStrategy::STABLE,
GithubStrategy::UNSTABLE,
GithubStrategy::ANY
),
GithubStrategy::STABLE
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$logger = new ConsoleLogger($output);
$updater = new Updater(null, false, Updater::STRATEGY_GITHUB);
/** @var GithubStrategy $strategy */
$strategy = $updater->getStrategy();
$strategy->setPackageName(PharTool::PACKAGE_NAME);
$strategy->setPharName(PharTool::PHAR_NAME);
$strategy->setCurrentLocalVersion('@package_version@');
$stability = $input->getOption('stability');
$strategy->setStability($stability);
try {
if ($updater->hasUpdate()) {
$newVersion = $updater->getNewVersion();
$logger->info(sprintf('The current stable version available is: %s', $newVersion));
$logger->info('Updating...');
if ($updater->update()) {
$logger->info(sprintf('Successful! You now have version %s installed', $newVersion));
}
} elseif (false === $updater->getNewVersion()) {
$logger->alert('There is no stable version available.');
} else {
$logger->info('@package_version@ is the latest stable version.');
}
return 0;
} catch (HttpRequestException $e) {
$logger->alert('Error fetching current version from remote repository.');
return 1;
}
}
}