1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Deployer\Component\PharUpdate\Console; |
6
|
|
|
|
7
|
|
|
use Deployer\Component\PharUpdate\Manager; |
8
|
|
|
use LogicException; |
9
|
|
|
use Symfony\Component\Console\Command\Command as Base; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manages updating or upgrading the Phar. |
16
|
|
|
* |
17
|
|
|
* @author Kevin Herrera <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Command extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Disable the ability to upgrade? |
23
|
|
|
* |
24
|
|
|
* @var boolean |
25
|
|
|
*/ |
26
|
|
|
private $disableUpgrade = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The manifest file URI. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $manifestUri; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The running file (the Phar that will be updated). |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $runningFile; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $name The command name. |
44
|
|
|
* @param boolean $disable Disable upgrading? |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $name, bool $disable = false) |
47
|
|
|
{ |
48
|
|
|
$this->disableUpgrade = $disable; |
49
|
|
|
|
50
|
|
|
parent::__construct($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets the manifest URI. |
55
|
|
|
* |
56
|
|
|
* @param string $uri The URI. |
57
|
|
|
*/ |
58
|
|
|
public function setManifestUri(string $uri) |
59
|
|
|
{ |
60
|
|
|
$this->manifestUri = $uri; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets the running file (the Phar that will be updated). |
65
|
|
|
* |
66
|
|
|
* @param string $file The file name or path. |
67
|
|
|
*/ |
68
|
|
|
public function setRunningFile(string $file): void |
69
|
|
|
{ |
70
|
|
|
$this->runningFile = $file; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
protected function configure() |
77
|
|
|
{ |
78
|
|
|
$this->setDescription('Updates the application.'); |
79
|
|
|
$this->addOption( |
80
|
|
|
'pre', |
81
|
|
|
'p', |
82
|
|
|
InputOption::VALUE_NONE, |
83
|
|
|
'Allow pre-release updates.', |
84
|
|
|
); |
85
|
|
|
$this->addOption( |
86
|
|
|
'redo', |
87
|
|
|
'r', |
88
|
|
|
InputOption::VALUE_NONE, |
89
|
|
|
'Redownload update if already using current version.', |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
if (false === $this->disableUpgrade) { |
93
|
|
|
$this->addOption( |
94
|
|
|
'upgrade', |
95
|
|
|
'u', |
96
|
|
|
InputOption::VALUE_NONE, |
97
|
|
|
'Upgrade to next major release, if available.', |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
106
|
|
|
{ |
107
|
|
|
if (null === $this->manifestUri) { |
108
|
|
|
throw new LogicException( |
109
|
|
|
'No manifest URI has been configured.', |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$output->writeln('Looking for updates...'); |
114
|
|
|
|
115
|
|
|
/** @var Helper */ |
116
|
|
|
$pharUpdate = $this->getHelper('phar-update'); |
117
|
|
|
/** @var Manager $manager */ |
118
|
|
|
$manager = $pharUpdate->getManager($this->manifestUri); |
|
|
|
|
119
|
|
|
$manager->setRunningFile($this->runningFile); |
120
|
|
|
|
121
|
|
|
if ($manager->update( |
122
|
|
|
$this->getApplication()->getVersion(), |
123
|
|
|
$this->disableUpgrade ?: (false === $input->getOption('upgrade')), |
124
|
|
|
$input->getOption('pre'), |
125
|
|
|
)) { |
126
|
|
|
$output->writeln('<info>Update successful!</info>'); |
127
|
|
|
} else { |
128
|
|
|
$output->writeln('<comment>Already up-to-date.</comment>'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Force exit to prevent warnings |
132
|
|
|
die(0); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|