|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.1 |
|
4
|
|
|
* |
|
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PhUml\Console\Commands; |
|
9
|
|
|
|
|
10
|
|
|
use Exception; |
|
11
|
|
|
use Humbug\SelfUpdate\Strategy\ShaStrategy; |
|
12
|
|
|
use Humbug\SelfUpdate\Updater; |
|
13
|
|
|
use PhUml\Console\UpdaterDisplay; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
|
|
19
|
|
|
class SelfUpdateCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
const VERSION_URL = 'https://montealegreluis.com/phuml/phuml.phar.version'; |
|
22
|
|
|
const PHAR_URL = 'https://montealegreluis.com/phuml/phuml.phar'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Updater */ |
|
25
|
|
|
private $updater; |
|
26
|
|
|
|
|
27
|
|
|
/** @var UpdaterDisplay */ |
|
28
|
|
|
private $display; |
|
29
|
|
|
|
|
30
|
|
|
/** @throws \Symfony\Component\Console\Exception\LogicException */ |
|
31
|
24 |
|
public function __construct(Updater $updater = null, UpdaterDisplay $display = null) |
|
32
|
|
|
{ |
|
33
|
24 |
|
parent::__construct(); |
|
34
|
24 |
|
$this->updater = $updater ?? new Updater(); |
|
35
|
24 |
|
$this->display = $display ?? new UpdaterDisplay(); |
|
36
|
24 |
|
} |
|
37
|
|
|
|
|
38
|
24 |
|
protected function configure() |
|
39
|
|
|
{ |
|
40
|
|
|
$this |
|
41
|
24 |
|
->setName('self-update') |
|
42
|
24 |
|
->setDescription('Update phuml.phar to most recent stable version.') |
|
43
|
24 |
|
->addOption( |
|
44
|
24 |
|
'rollback', |
|
45
|
24 |
|
'r', |
|
46
|
24 |
|
InputOption::VALUE_NONE, |
|
47
|
24 |
|
'Rollback to previous version of phUML if available on filesystem.' |
|
48
|
|
|
) |
|
49
|
24 |
|
->addOption( |
|
50
|
24 |
|
'check', |
|
51
|
24 |
|
'c', |
|
52
|
24 |
|
InputOption::VALUE_NONE, |
|
53
|
24 |
|
'Checks if there is an updated version available.' |
|
54
|
|
|
); |
|
55
|
24 |
|
} |
|
56
|
|
|
|
|
57
|
24 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
58
|
|
|
{ |
|
59
|
24 |
|
$this->configureUpdaterStrategy(); |
|
60
|
|
|
|
|
61
|
24 |
|
if ($input->getOption('rollback')) { |
|
62
|
9 |
|
$this->tryToRollback(); |
|
63
|
9 |
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
15 |
|
if ($input->getOption('check')) { |
|
67
|
9 |
|
$this->tryToCheckForUpdates(); |
|
68
|
9 |
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
6 |
|
$this->tryToUpdate($output); |
|
72
|
6 |
|
} |
|
73
|
|
|
|
|
74
|
9 |
|
private function tryToRollback(): void |
|
75
|
|
|
{ |
|
76
|
9 |
|
$this->tryAction([$this, 'rollback']); |
|
77
|
9 |
|
} |
|
78
|
|
|
|
|
79
|
9 |
|
private function rollback(): void |
|
80
|
|
|
{ |
|
81
|
9 |
|
$result = $this->updater->rollback(); |
|
82
|
6 |
|
$this->display->rollbackMessage($result); |
|
83
|
6 |
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
private function tryToCheckForUpdates(): void |
|
86
|
|
|
{ |
|
87
|
9 |
|
$this->display->currentLocalVersion($this->getApplication()->getVersion()); |
|
88
|
9 |
|
$this->tryAction([$this, 'showAvailableUpdates']); |
|
89
|
9 |
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
private function showAvailableUpdates(): void |
|
92
|
|
|
{ |
|
93
|
9 |
|
if ($this->updater->hasUpdate()) { |
|
94
|
3 |
|
$this->display->newVersion($this->updater->getNewVersion()); |
|
95
|
6 |
|
} elseif (false === $this->updater->getNewVersion()) { |
|
96
|
3 |
|
$this->display->noUpdatesAvailable(); |
|
97
|
|
|
} else { |
|
98
|
3 |
|
$this->display->alreadyUpToDate(); |
|
99
|
|
|
} |
|
100
|
9 |
|
} |
|
101
|
|
|
|
|
102
|
6 |
|
private function tryToUpdate(OutputInterface $output): void |
|
103
|
|
|
{ |
|
104
|
6 |
|
$output->writeln('Updating...' . PHP_EOL); |
|
105
|
6 |
|
$this->tryAction([$this, 'update']); |
|
106
|
6 |
|
$output->write(PHP_EOL); |
|
107
|
6 |
|
} |
|
108
|
|
|
|
|
109
|
6 |
|
private function update(): void |
|
110
|
|
|
{ |
|
111
|
6 |
|
$result = $this->updater->update(); |
|
112
|
6 |
|
if ($result) { |
|
113
|
3 |
|
$this->display->updateApplied($this->updater->getOldVersion(), $this->updater->getNewVersion()); |
|
114
|
|
|
} else { |
|
115
|
3 |
|
$this->display->noUpdateApplied($this->updater->getOldVersion()); |
|
116
|
|
|
} |
|
117
|
6 |
|
} |
|
118
|
|
|
|
|
119
|
24 |
|
private function configureUpdaterStrategy(): void |
|
120
|
|
|
{ |
|
121
|
24 |
|
$strategy = new ShaStrategy(); |
|
122
|
24 |
|
$strategy->setPharUrl(self::PHAR_URL); |
|
123
|
24 |
|
$strategy->setVersionUrl(self::VERSION_URL); |
|
124
|
24 |
|
$this->updater->setStrategyObject($strategy); |
|
125
|
24 |
|
} |
|
126
|
|
|
|
|
127
|
24 |
|
private function tryAction(callable $action): void |
|
128
|
|
|
{ |
|
129
|
|
|
try { |
|
130
|
24 |
|
$action(); |
|
131
|
3 |
|
} catch (Exception $exception) { |
|
132
|
3 |
|
$this->display->error($exception); |
|
133
|
|
|
} |
|
134
|
24 |
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|