-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallExtensionsForProjectCommand.php
More file actions
277 lines (234 loc) · 10.3 KB
/
Copy pathInstallExtensionsForProjectCommand.php
File metadata and controls
277 lines (234 loc) · 10.3 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
namespace Php\Pie\Command;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use Composer\Package\Link;
use Composer\Package\RootPackageInterface;
use Php\Pie\ComposerIntegration\PieComposerFactory;
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieJsonEditor;
use Php\Pie\DependencyResolver\RequestedPackageAndVersion;
use Php\Pie\ExtensionName;
use Php\Pie\ExtensionType;
use Php\Pie\Installing\InstallForPhpProject\CheckExtensionStatus;
use Php\Pie\Installing\InstallForPhpProject\ComposerFactoryForProject;
use Php\Pie\Installing\InstallForPhpProject\DetermineExtensionsRequired;
use Php\Pie\Installing\InstallForPhpProject\InstallPiePackageFromPath;
use Php\Pie\Installing\InstallForPhpProject\InstallSelectedPackage;
use Php\Pie\Installing\InstallForPhpProject\NoMatchingPackagesFound;
use Php\Pie\Installing\InstallForPhpProject\PackageSelectionRequired;
use Php\Pie\Installing\InstallForPhpProject\SelectPackageForExtension;
use Php\Pie\Platform;
use Php\Pie\Platform\InstalledPiePackages;
use Php\Pie\Platform\PiePackageList;
use Php\Pie\Platform\TargetPlatform;
use Psr\Container\ContainerInterface;
use Safe\Exceptions\DirException;
use Safe\Exceptions\FilesystemException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use Webmozart\Assert\Assert;
use function array_filter;
use function array_keys;
use function array_map;
use function array_values;
use function count;
use function implode;
use function Safe\getcwd;
use function Safe\realpath;
use function sprintf;
use const PHP_EOL;
#[AsCommand(
name: 'install-extensions-for-project',
description: 'Check a project for its extension dependencies, and offers to install them',
)]
final class InstallExtensionsForProjectCommand extends Command
{
public function __construct(
private readonly ComposerFactoryForProject $composerFactoryForProject,
private readonly DetermineExtensionsRequired $determineExtensionsRequired,
private readonly InstalledPiePackages $installedPiePackages,
private readonly CheckExtensionStatus $checkExtensionStatus,
private readonly SelectPackageForExtension $selectPackageForExtension,
private readonly InstallSelectedPackage $installSelectedPackage,
private readonly InstallPiePackageFromPath $installPiePackageFromPath,
private readonly ContainerInterface $container,
private readonly IOInterface $io,
) {
parent::__construct();
}
public function configure(): void
{
parent::configure();
CommandHelper::configureDownloadBuildInstallOptions($this, false);
}
private function handlePieProject(InputInterface $input, RootPackageInterface $rootPackage, callable $restoreWorkingDir): int
{
try {
$cwd = realpath(getcwd());
} catch (FilesystemException | DirException $e) {
$this->io->writeError(sprintf(
'<error>Failed to determine current working directory: %s</error>',
$e->getMessage(),
));
$restoreWorkingDir();
return Command::FAILURE;
}
$exit = ($this->installPiePackageFromPath)(
$this,
$cwd,
$rootPackage,
PieJsonEditor::fromTargetPlatform(CommandHelper::determineTargetPlatformFromInputs($input, new NullIO())),
$input,
$this->io,
);
$restoreWorkingDir();
return $exit;
}
private function handlePhpProject(InputInterface $input, RootPackageInterface $rootPackage, callable $restoreWorkingDir): int
{
$extensionToPackageSelections = CommandHelper::determineExtensionToPackageSelections($input);
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $this->io);
$allowNonInteractive = $input->hasOption(CommandHelper::OPTION_ALLOW_NON_INTERACTIVE_PROJECT_INSTALL) && $input->getOption(CommandHelper::OPTION_ALLOW_NON_INTERACTIVE_PROJECT_INSTALL);
if ($allowNonInteractive) {
$this->io->writeError(sprintf(
'<warning>The --%s is now deprecated and has no effect.</warning>',
CommandHelper::OPTION_ALLOW_NON_INTERACTIVE_PROJECT_INSTALL,
));
}
$this->io->write(sprintf(
'Checking extensions for your project <info>%s</info> (path: %s)',
$rootPackage->getPrettyName(),
getcwd(),
));
$extensionsRequired = $this->determineExtensionsRequired->forProject(
$this->composerFactoryForProject->composer($this->io),
CommandHelper::noDev($input),
);
$pieComposer = PieComposerFactory::createPieComposer(
$this->container,
PieComposerRequest::noOperation(
new NullIO(),
$targetPlatform,
),
);
$phpEnabledExtensions = array_map('strtolower', array_keys($targetPlatform->phpBinaryPath->extensions()));
$installedPiePackages = $this->installedPiePackages->allPiePackages($pieComposer);
$anyErrorsHappened = false;
$scheduledForInstall = array_values(array_filter(array_map(
function (Link $link) use ($pieComposer, $phpEnabledExtensions, $installedPiePackages, &$anyErrorsHappened, $targetPlatform, $extensionToPackageSelections): RequestedPackageAndVersion|null {
$result = $this->handleSingleExtensionRequiredByPhpProject(
$pieComposer,
$link,
$installedPiePackages,
$targetPlatform,
$phpEnabledExtensions,
$extensionToPackageSelections,
);
if ($result instanceof RequestedPackageAndVersion) {
return $result;
}
if ($result === false) {
$anyErrorsHappened = true;
}
return null;
},
$extensionsRequired,
)));
if (count($scheduledForInstall)) {
$nicePackageList = implode(', ', array_map(
static fn (RequestedPackageAndVersion $req) => $req->prettyNameAndVersion(),
$scheduledForInstall,
));
try {
$this->io->write(
sprintf('Invoking pie install of %s', $nicePackageList),
verbosity: IOInterface::VERBOSE,
);
Assert::same(
0,
$this->installSelectedPackage->withSubCommand(
$scheduledForInstall,
$this,
$input,
),
'Non-zero exit code %s whilst installing ' . $nicePackageList,
);
} catch (Throwable $t) {
$this->io->writeError('<error>' . $t->getMessage() . '</error>');
$anyErrorsHappened = true;
}
}
$this->io->write(PHP_EOL . 'Finished checking extensions.');
$restoreWorkingDir();
return $anyErrorsHappened ? self::FAILURE : self::SUCCESS;
}
/**
* Returns false if any error happened whilst trying to install the extension; returns true if was already
* installed, or it was successfully installed if needed.
*
* @param list<string> $phpEnabledExtensions
* @param array<non-empty-string, non-empty-string> $extensionToPackageSelections
*/
private function handleSingleExtensionRequiredByPhpProject(
Composer $pieComposer,
Link $link,
PiePackageList $installedPiePackages,
TargetPlatform $targetPlatform,
array $phpEnabledExtensions,
array $extensionToPackageSelections,
): RequestedPackageAndVersion|bool {
$extension = ExtensionName::normaliseFromString($link->getTarget());
$piePackagesForExtension = $installedPiePackages
->findByPhpFormattedExtensionName($extension->phpFormattedExtensionName())
->onlyVerifiedFor($targetPlatform);
// Check if the extension is already installed, if it is, return early.
if (($this->checkExtensionStatus)($link, $piePackagesForExtension, $phpEnabledExtensions)) {
return true;
}
try {
$requestedPackageAndVersion = ($this->selectPackageForExtension)(
$extension,
$link->getPrettyConstraint(),
$extensionToPackageSelections,
$pieComposer,
Platform::isInteractive(),
);
} catch (NoMatchingPackagesFound $e) {
$this->io->write($e->getMessage());
return false;
} catch (PackageSelectionRequired $e) {
// @todo https://github.com/php/pie/issues/592
$options = array_map(
static fn (array $match) => sprintf(' --select=%s=%s', $e->extensionName->name(), $match['name']),
$e->matches,
);
$this->io->writeError(sprintf(
'<warning>No package selections were made for %s; you MUST specify a package selection in non-interactive mode, by adding one of the following parameters to the `pie install` command:</warning>%s',
$e->extensionName->nameWithExtPrefix(),
"\n" . implode("\n", $options),
));
return false;
}
// Interactive user did not select a package to install
if ($requestedPackageAndVersion === null) {
return false;
}
return $requestedPackageAndVersion;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$restoreWorkingDir = CommandHelper::handleWorkingDirectory($input, $this->io);
CommandHelper::applyNoCacheOptionIfSet($input, $this->io);
$rootPackage = $this->composerFactoryForProject->rootPackage($this->io);
if (ExtensionType::isValid($rootPackage->getType())) {
return $this->handlePieProject($input, $rootPackage, $restoreWorkingDir);
}
return $this->handlePhpProject($input, $rootPackage, $restoreWorkingDir);
}
}