|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CL\SlackCli\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Herrera\Phar\Update\Manager; |
|
6
|
|
|
use Herrera\Phar\Update\Manifest; |
|
7
|
|
|
use Herrera\Version\Parser; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class SelfUpdateCommand extends AbstractCommand |
|
12
|
|
|
{ |
|
13
|
|
|
const MANIFEST_FILE = 'http://cleentfaar.github.io/slack-cli/manifest.json'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::configure(); |
|
21
|
|
|
|
|
22
|
|
|
$this->setName('self:update'); |
|
23
|
|
|
$this->setDescription('Updates slack.phar to the latest version'); |
|
24
|
|
|
$this->setAliases(['self.update']); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
31
|
|
|
{ |
|
32
|
|
|
$currentVersion = $this->getApplication()->getVersion(); |
|
33
|
|
|
$manager = new Manager(Manifest::loadFile(self::MANIFEST_FILE)); |
|
34
|
|
|
$lockMajor = false; |
|
35
|
|
|
$allowPreRelease = false; |
|
36
|
|
|
|
|
37
|
|
|
if (substr($currentVersion, 0, 1) == 0) { |
|
38
|
|
|
$allowPreRelease = true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($manager->update($currentVersion, $lockMajor, $allowPreRelease)) { |
|
42
|
|
|
$newVersion = $this->getNewVersion($currentVersion, $manager, $lockMajor, $allowPreRelease); |
|
43
|
|
|
|
|
44
|
|
|
$this->output->writeln(sprintf( |
|
45
|
|
|
'<info>Updated Slack CLI from <fg=yellow>%s</fg=yellow> to <fg=yellow>%s</fg=yellow></info>', |
|
46
|
|
|
$currentVersion, |
|
47
|
|
|
$newVersion |
|
48
|
|
|
)); |
|
49
|
|
|
} else { |
|
50
|
|
|
$this->output->writeln(sprintf( |
|
51
|
|
|
'<comment>You are already using the latest version (%s)</comment>', |
|
52
|
|
|
$currentVersion |
|
53
|
|
|
)); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $currentVersion |
|
59
|
|
|
* @param Manager $manager |
|
60
|
|
|
* @param bool $lockMajor |
|
61
|
|
|
* @param bool $allowPreRelease |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getNewVersion($currentVersion, Manager $manager, $lockMajor = false, $allowPreRelease = false) |
|
66
|
|
|
{ |
|
67
|
|
|
$newVersionObject = $manager->getManifest()->findRecent( |
|
68
|
|
|
Parser::toVersion($currentVersion), |
|
69
|
|
|
$lockMajor, |
|
70
|
|
|
$allowPreRelease |
|
71
|
|
|
)->getVersion(); |
|
72
|
|
|
|
|
73
|
|
|
$newVersion = sprintf( |
|
74
|
|
|
'%s.%s', |
|
75
|
|
|
$newVersionObject->getMajor(), |
|
76
|
|
|
implode('.', array_filter([ |
|
77
|
|
|
$newVersionObject->getMinor(), |
|
78
|
|
|
$newVersionObject->getPatch(), |
|
79
|
|
|
])) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
return $newVersion; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|