-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIbexaSetupCommand.php
More file actions
235 lines (195 loc) · 7.88 KB
/
IbexaSetupCommand.php
File metadata and controls
235 lines (195 loc) · 7.88 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\PostInstall\Command;
use Composer\Command\BaseCommand;
use Composer\InstalledVersions;
use Composer\IO\IOInterface;
use Composer\Semver\Semver;
use Composer\Semver\VersionParser;
use Exception;
use Ibexa\PostInstall\IbexaProductVersion;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
/**
* @deprecated This command is deprecated and will be removed in 6.0.0 release. Rely on ibexa/cloud instead.
*/
#[AsCommand(name: 'ibexa:setup', description: 'Runs post install configuration tool.')]
class IbexaSetupCommand extends BaseCommand
{
private VersionParser $versionParser;
private const string PSH_RESOURCES_PATH = __DIR__ . '/../../../resources/platformsh';
protected function configure(): void
{
$this->addOption(
'platformsh',
null,
InputOption::VALUE_NONE,
'Install Platform.sh config files'
);
}
/**
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('platformsh')) {
$this->getIO()->write('Installing Platform.sh config files...', true, IOInterface::NORMAL);
$fileSystem = new Filesystem();
$product = IbexaProductVersion::getInstalledProduct();
$version = IbexaProductVersion::getInstalledProductVersion();
$commonFiles = $this->getCommonFiles($product);
$productSpecificFiles = $this->getProductSpecificFiles($product, $version);
// helper array for detecting common file overrides
$commonFilePathNames = array_fill_keys(
array_map(static function (SplFileInfo $file): string {
return $file->getRelativePathname();
}, iterator_to_array($commonFiles)),
true
);
$output->writeln('Copying common files');
$progressBar = new ProgressBar($output);
$progressBar->start($commonFiles->count());
$this->printNewLine($output);
foreach ($commonFiles as $file) {
if ($fileSystem->exists($file->getRelativePathname())) {
$output->writeln(
sprintf("File '%s' exists and has been overwritten", $file->getRelativePathname()),
OutputInterface::VERBOSITY_VERBOSE
);
}
$fileSystem->copy($file->getPathname(), $file->getRelativePathname(), true);
$progressBar->advance();
$this->printNewLine($output);
}
$progressBar->finish();
$output->writeln("\nCopying product specific files");
$progressBar->start($productSpecificFiles->count());
$this->printNewLine($output);
foreach ($productSpecificFiles as $file) {
if (
!array_key_exists($file->getRelativePathname(), $commonFilePathNames)
&& $fileSystem->exists($file->getRelativePathname())
) {
$output->writeln(
sprintf("File '%s' exists and has been overwritten", $file->getRelativePathname()),
OutputInterface::VERBOSITY_VERBOSE
);
}
$fileSystem->copy($file->getPathname(), $file->getRelativePathname(), true);
$progressBar->advance();
$this->printNewLine($output);
}
$progressBar->finish();
$output->writeln("\nPlatform.sh config files installed successfully");
}
return Command::SUCCESS;
}
/**
* @throws \Exception
*/
protected function getCommonFiles(string $product): Finder
{
$versionDir = $this->getVersionDirectory($product, self::PSH_RESOURCES_PATH . '/common');
$finder = new Finder();
$finder
->in(self::PSH_RESOURCES_PATH . '/common/' . $versionDir)
->ignoreDotFiles(false)
->followLinks()
->files();
return $finder;
}
/**
* @throws \Exception
*/
protected function getProductSpecificFiles(string $product, string $version): Finder
{
$productDir = str_replace('/', '-', $product);
$versionDir = $this->getVersionDirectory($product, self::PSH_RESOURCES_PATH . '/' . $productDir);
$finder = new Finder();
$finder
->in(self::PSH_RESOURCES_PATH . '/' . $productDir . '/' . $versionDir . '/')
->ignoreDotFiles(false)
->followLinks()
->files();
return $finder;
}
protected function printNewLine(OutputInterface $output): void
{
$output->writeln('');
}
private function getVersionDirectory(string $product, string $path): string
{
$finder = new Finder();
$finder
->in($path)
->ignoreDotFiles(false)
->directories()
->depth(0);
$versionDirs = array_values(
array_map(
static function (SplFileInfo $dir): string {
return $dir->getRelativePathname();
},
iterator_to_array($finder)
)
);
$allRawDataSets = InstalledVersions::getAllRawData();
$productPackage = null;
foreach ($allRawDataSets as $rawData) {
if (isset($rawData['versions'][$product])) {
$productPackage = $rawData['versions'][$product];
break;
}
}
if ($productPackage === null) {
throw new RuntimeException(sprintf('Could not find product "%s" in installed versions.', $product));
}
$aliases = $productPackage['aliases'] ?? [];
$productVersion = $productPackage['version'] ?? '';
$normalizedAliases = array_map(function (string $alias): string {
$normalizedAlias = $this->getVersionParser()->parseNumericAliasPrefix($alias);
if ($normalizedAlias === false) {
throw new RuntimeException(sprintf('Unable to parse version. "%s" is invalid.', $alias));
}
return trim($normalizedAlias, '.');
}, $aliases);
foreach (Semver::rsort($versionDirs) as $versionDir) {
// directory name:
// matches version (i.e. dev-master)
// OR is one of the normalized aliases (3.3.x-dev => 3.3)
// OR is one of the aliases (3.3.x-dev)
// OR matches semver constraint (3.3, 3.3.1)
if (
$versionDir === $productVersion
|| in_array($versionDir, $normalizedAliases, true)
|| in_array($versionDir, $aliases, true)
|| (
false === strpos($versionDir, 'dev-')
&& Semver::satisfies($productVersion, '~' . $versionDir)
)
) {
return $versionDir;
}
}
throw new Exception('Can\'t find directory matching your product version');
}
private function getVersionParser(): VersionParser
{
if (!isset($this->versionParser)) {
$this->versionParser = new VersionParser();
}
return $this->versionParser;
}
}