Skip to content

Commit

Permalink
bug #907 Re-run composer when flex is installed after deps are resolv…
Browse files Browse the repository at this point in the history
…ed (nicolas-grekas)

This PR was merged into the 1.x branch.

Discussion
----------

Re-run composer when flex is installed after deps are resolved

Fix #880 and others

Commits
-------

59ec0f3 Re-run composer when flex is installed after deps are resolved
  • Loading branch information
nicolas-grekas committed May 2, 2022
2 parents 63fdd79 + 59ec0f3 commit e74319d
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Flex implements PluginInterface, EventSubscriberInterface
private $rfs;
private $progress = true;
private $dryRun = false;
private $reinstall;
private static $activated = true;
private static $repoReadingCommands = [
'create-project' => true,
Expand Down Expand Up @@ -346,6 +347,13 @@ public function configureProject(Event $event)
$this->updateComposerLock();
}

public function recordFlexInstall(PackageEvent $event)
{
if (null === $this->reinstall && 'symfony/flex' === $event->getOperation()->getPackage()->getName()) {
$this->reinstall = true;
}
}

public function record(PackageEvent $event)
{
if ($this->shouldRecordOperation($event->getOperation(), $event->isDevMode(), $event->getComposer())) {
Expand Down Expand Up @@ -388,7 +396,7 @@ public function update(Event $event, $operations = [])
$contents = file_get_contents($file);
$json = JsonFile::parseJson($contents);

if (!isset($json['flex-require']) && !isset($json['flex-require-dev'])) {
if (!$this->reinstall && !isset($json['flex-require']) && !isset($json['flex-require-dev'])) {
$this->unpack($event);

return;
Expand All @@ -400,17 +408,18 @@ public function update(Event $event, $operations = [])
$symfonyVersion = $json['extra']['symfony']['require'] ?? null;
$versions = $symfonyVersion ? $this->downloader->getVersions() : null;
foreach (['require', 'require-dev'] as $type) {
if (isset($json['flex-'.$type])) {
foreach ($json['flex-'.$type] as $package => $constraint) {
if ($symfonyVersion && '*' === $constraint && isset($versions['splits'][$package])) {
// replace unbounded constraints for symfony/* packages by extra.symfony.require
$constraint = $symfonyVersion;
}
$manipulator->addLink($type, $package, $constraint, $sortPackages);
if (!isset($json['flex-'.$type])) {
continue;
}
foreach ($json['flex-'.$type] as $package => $constraint) {
if ($symfonyVersion && '*' === $constraint && isset($versions['splits'][$package])) {
// replace unbounded constraints for symfony/* packages by extra.symfony.require
$constraint = $symfonyVersion;
}

$manipulator->removeMainKey('flex-'.$type);
$manipulator->addLink($type, $package, $constraint, $sortPackages);
}

$manipulator->removeMainKey('flex-'.$type);
}

file_put_contents($file, $manipulator->getContents());
Expand Down Expand Up @@ -891,7 +900,7 @@ private function formatOrigin(Recipe $recipe): string

private function shouldRecordOperation(OperationInterface $operation, bool $isDevMode, Composer $composer = null): bool
{
if ($this->dryRun) {
if ($this->dryRun || $this->reinstall) {
return false;
}

Expand Down Expand Up @@ -994,6 +1003,7 @@ private function unpack(Event $event)

private function reinstall(Event $event, bool $update)
{
$this->reinstall = false;
$event->stopPropagation();

$ed = $this->composer->getEventDispatcher();
Expand Down Expand Up @@ -1045,7 +1055,7 @@ public static function getSubscribedEvents(): array

if (version_compare('2.0.0', PluginInterface::PLUGIN_API_VERSION, '>')) {
$events += [
PackageEvents::POST_PACKAGE_INSTALL => 'record',
PackageEvents::POST_PACKAGE_INSTALL => [['recordFlexInstall'], ['record']],
PackageEvents::POST_PACKAGE_UPDATE => [['record'], ['enableThanksReminder']],
PackageEvents::POST_PACKAGE_UNINSTALL => 'record',
InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['populateProvidersCacheDir', \PHP_INT_MAX]],
Expand All @@ -1057,6 +1067,7 @@ public static function getSubscribedEvents(): array
} else {
$events += [
PackageEvents::POST_PACKAGE_UPDATE => 'enableThanksReminder',
PackageEvents::POST_PACKAGE_INSTALL => 'recordFlexInstall',
InstallerEvents::PRE_OPERATIONS_EXEC => 'recordOperations',
PluginEvents::PRE_POOL_CREATE => 'truncatePackages',
];
Expand Down

0 comments on commit e74319d

Please sign in to comment.