/
UpdatePackageCommand.php
133 lines (123 loc) · 5.07 KB
/
UpdatePackageCommand.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Concrete\Core\Console\Command;
use Concrete\Core\Console\Command;
use Concrete\Core\Console\ConsoleAwareInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Concrete\Core\Support\Facade\Package;
use Exception;
class UpdatePackageCommand extends Command
{
protected function configure()
{
$okExitCode = static::SUCCESS;
$errExitCode = static::FAILURE;
$this
->setName('c5:package:update')
->setAliases([
'c5:package-update',
'c5:update-package',
])
->addEnvOption()
->setCanRunAsRoot(false)
->addOption('all', 'a', InputOption::VALUE_NONE, 'Update all the installed packages')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force update even if the package is already at last version')
->addArgument('packages', InputArgument::IS_ARRAY, 'The handle of the package to be updated (multiple values allowed)')
->setDescription('Update a Concrete package')
->setHelp(<<<EOT
Returns codes:
$okExitCode operation completed successfully
$errExitCode errors occurred
More info at http://documentation.concrete5.org/developers/appendix/cli-commands#c5-package-update
EOT
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$rc = static::SUCCESS;
$updatableHandles = [];
$force = $input->getOption('force');
if ($input->getOption('all')) {
if (count($input->getArgument('packages')) > 0) {
throw new Exception('If you use the --all option you can\'t specify package handles.');
}
if ($force) {
foreach (Package::getInstalledHandles() as $pkgHandle) {
$updatableHandles[] = $pkgHandle;
}
if (empty($updatableHandles)) {
$output->writeln("No package has been found.");
}
} else {
foreach (Package::getLocalUpgradeablePackages() as $pkg) {
$updatableHandles[] = $pkg->getPackageHandle();
}
if (empty($updatableHandles)) {
$output->writeln("No package needs to be updated.");
}
}
} else {
$updatableHandles = $input->getArgument('packages');
if (empty($updatableHandles)) {
throw new Exception('No package handle specified and the --all option has not been specified.');
}
}
foreach ($updatableHandles as $updatableHandle) {
try {
$this->updatePackage($updatableHandle, $output, $input, $force);
} catch (Exception $x) {
$this->writeError($output, $x);
$rc = static::FAILURE;
}
}
return $rc;
}
protected function updatePackage($pkgHandle, OutputInterface $output, InputInterface $input, $force)
{
$output->write("Looking for package '$pkgHandle'... ");
$pkg = null;
foreach (Package::getInstalledList() as $installed) {
if ($installed->getPackageHandle() === $pkgHandle) {
$pkg = $installed;
break;
}
}
if ($pkg === null) {
throw new Exception(sprintf("No package with handle '%s' is installed", $pkgHandle));
}
// Provide the console objects to objects that are aware of the console
if ($pkg instanceof ConsoleAwareInterface) {
$pkg->setConsole($this->getApplication(), $output, $input);
}
$output->writeln(sprintf('<info>found (%s).</info>', $pkg->getPackageName()));
$output->write('Checking preconditions... ');
$upPkg = null;
foreach (Package::getLocalUpgradeablePackages() as $updatable) {
if ($updatable->getPackageHandle() === $pkgHandle) {
$upPkg = $updatable;
break;
}
}
if ($upPkg === null && $force !== true) {
$output->writeln(sprintf("<info>the package is already up-to-date (v%s)</info>", $pkg->getPackageVersion()));
} else {
$test = $pkg->testForUpgrade();
if ($test !== true) {
throw new Exception(implode("\n", $test->getList()));
}
$output->writeln('<info>good.</info>');
if ($upPkg === null) {
$output->write(sprintf('Forcing upgrade at v%s... ', $pkg->getPackageVersion()));
$upPkg = Package::getByHandle($pkgHandle);
} else {
$output->write(sprintf('Updating from v%s to v%s... ', $upPkg->getPackageEntity()->getPackageVersion(), $upPkg->getPackageVersion()));
}
$upPkg->upgradeCoreData();
$upPkg->upgrade();
$output->writeln('<info>done.</info>');
}
}
}