-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathInstallExtensionsForProjectCommand.php
More file actions
308 lines (257 loc) · 11.9 KB
/
InstallExtensionsForProjectCommand.php
File metadata and controls
308 lines (257 loc) · 11.9 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
declare(strict_types=1);
namespace Php\Pie\Command;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use Composer\Package\Link;
use Composer\Package\Version\VersionParser;
use OutOfRangeException;
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\ComposerFactoryForProject;
use Php\Pie\Installing\InstallForPhpProject\DetermineExtensionsRequired;
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
use Php\Pie\Installing\InstallForPhpProject\InstallPiePackageFromPath;
use Php\Pie\Installing\InstallForPhpProject\InstallSelectedPackage;
use Php\Pie\Platform;
use Php\Pie\Platform\InstalledPiePackages;
use Php\Pie\Util\Emoji;
use Psr\Container\ContainerInterface;
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_column;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
use function array_walk;
use function assert;
use function chdir;
use function count;
use function getcwd;
use function implode;
use function in_array;
use function is_dir;
use function is_string;
use function realpath;
use function sprintf;
use function strtolower;
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 FindMatchingPackages $findMatchingPackages,
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);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$workingDirOption = (string) $input->getOption(CommandHelper::OPTION_WORKING_DIRECTORY);
$restoreWorkingDir = static function (): void {
};
if ($workingDirOption !== '' && is_dir($workingDirOption)) {
$currentWorkingDir = getcwd();
$restoreWorkingDir = function () use ($currentWorkingDir): void {
chdir($currentWorkingDir);
$this->io->write(
sprintf('Restored working directory to: %s', $currentWorkingDir),
verbosity: IOInterface::VERBOSE,
);
};
chdir($workingDirOption);
$this->io->write(
sprintf('Changed working directory to: %s', $workingDirOption),
verbosity: IOInterface::VERBOSE,
);
}
CommandHelper::applyNoCacheOptionIfSet($input, $this->io);
$rootPackage = $this->composerFactoryForProject->rootPackage($this->io);
if (ExtensionType::isValid($rootPackage->getType())) {
$cwd = realpath(getcwd());
if (! is_string($cwd) || $cwd === '') {
$this->io->writeError('<error>Failed to determine current working directory.</error>');
$restoreWorkingDir();
return Command::FAILURE;
}
$exit = ($this->installPiePackageFromPath)(
$this,
$cwd,
$rootPackage,
PieJsonEditor::fromTargetPlatform(CommandHelper::determineTargetPlatformFromInputs($input, new NullIO())),
$input,
$this->io,
);
$restoreWorkingDir();
return $exit;
}
$allowNonInteractive = $input->hasOption(CommandHelper::OPTION_ALLOW_NON_INTERACTIVE_PROJECT_INSTALL) && $input->getOption(CommandHelper::OPTION_ALLOW_NON_INTERACTIVE_PROJECT_INSTALL);
if (! Platform::isInteractive() && ! $allowNonInteractive) {
$this->io->writeError(sprintf(
'<warning>Aborting! You are not running in interactive mode, and --%s was not specified.</warning>',
CommandHelper::OPTION_ALLOW_NON_INTERACTIVE_PROJECT_INSTALL,
));
return Command::FAILURE;
}
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $this->io);
$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));
$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;
array_walk(
$extensionsRequired,
function (Link $link) use ($pieComposer, $phpEnabledExtensions, $installedPiePackages, $input, &$anyErrorsHappened): void {
$extension = ExtensionName::normaliseFromString($link->getTarget());
$linkRequiresConstraint = $link->getPrettyConstraint();
$piePackageVersion = null;
if (in_array($extension->name(), array_keys($installedPiePackages))) {
$piePackageVersion = $installedPiePackages[$extension->name()]->version();
}
$piePackageVersionMatchesLinkConstraint = null;
if ($piePackageVersion !== null) {
$piePackageVersionMatchesLinkConstraint = $link
->getConstraint()
->matches(
(new VersionParser())->parseConstraints($piePackageVersion),
);
}
if (in_array(strtolower($extension->name()), $phpEnabledExtensions)) {
if ($piePackageVersion !== null && $piePackageVersionMatchesLinkConstraint === false) {
$this->io->write(sprintf(
'%s: <comment>%s:%s</comment> %s Version %s is installed, but does not meet the version requirement %s',
$link->getDescription(),
$link->getTarget(),
$linkRequiresConstraint,
Emoji::WARNING,
$piePackageVersion,
$link->getConstraint()->getPrettyString(),
));
return;
}
$this->io->write(sprintf(
'%s: <info>%s:%s</info> %s Already installed',
$link->getDescription(),
$link->getTarget(),
$linkRequiresConstraint,
Emoji::GREEN_CHECKMARK,
));
return;
}
$this->io->write(sprintf(
'%s: <comment>%s:%s</comment> %s Missing',
$link->getDescription(),
$link->getTarget(),
$linkRequiresConstraint,
Emoji::PROHIBITED,
));
try {
$matches = $this->findMatchingPackages->for($pieComposer, $extension->name());
} catch (OutOfRangeException) {
$anyErrorsHappened = true;
$this->io->writeError(sprintf(
'<error>No packages were found for %s</error>',
$extension->nameWithExtPrefix(),
));
return;
}
if (! Platform::isInteractive() && count($matches) > 1) {
$anyErrorsHappened = true;
// @todo Figure out if there is a way to improve this, safely
$this->io->writeError(sprintf(
"<warning>Multiple packages were found for %s:</warning>\n %s\n\n<warning>This means you cannot `pie install` this project interactively for now.</warning>",
$extension->nameWithExtPrefix(),
implode("\n ", array_column($matches, 'name')),
));
return;
}
if (Platform::isInteractive()) {
$selectedPackageAnswer = (int) $this->io->select(
"\nThe following packages may be suitable, which would you like to install: ",
array_merge(
['None'],
array_map(
static function (array $match): string {
return sprintf('%s: %s', $match['name'], $match['description'] ?? 'no description available');
},
$matches,
),
),
'0',
);
if ($selectedPackageAnswer === 0) {
$this->io->write('Okay I won\'t install anything for ' . $extension->name());
$anyErrorsHappened = true;
return;
}
$matchesKey = $selectedPackageAnswer - 1;
assert(array_key_exists($matchesKey, $matches));
$selectedPackageName = $matches[$matchesKey]['name'];
} else {
$selectedPackageName = $matches[0]['name'];
}
assert($selectedPackageName !== '');
$requestedPackageAndVersion = new RequestedPackageAndVersion(
$selectedPackageName,
$linkRequiresConstraint === '*' || $linkRequiresConstraint === '' ? null : $linkRequiresConstraint,
);
try {
$this->io->write(
sprintf('Invoking pie install of %s', $requestedPackageAndVersion->prettyNameAndVersion()),
verbosity: IOInterface::VERBOSE,
);
Assert::same(
0,
$this->installSelectedPackage->withSubCommand(
ExtensionName::normaliseFromString($link->getTarget()),
$requestedPackageAndVersion,
$this,
$input,
),
'Non-zero exit code %s whilst installing ' . $requestedPackageAndVersion->package,
);
} catch (Throwable $t) {
$anyErrorsHappened = true;
$this->io->writeError('<error>' . $t->getMessage() . '</error>');
}
},
);
$this->io->write(PHP_EOL . 'Finished checking extensions.');
$restoreWorkingDir();
return $anyErrorsHappened ? self::FAILURE : self::SUCCESS;
}
}