1
0
Fork 0

Allow all prepare/cleanup/install/update/uninstall operations to return promises to be executed in parallel as well

pull/8444/head
Jordi Boggiano 2019-11-23 11:25:28 +01:00
parent 438c45e991
commit 5725a2db57
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 69 additions and 55 deletions

View File

@ -173,14 +173,14 @@ class InstallationManager
$promises = array(); $promises = array();
foreach ($operations as $operation) { foreach ($operations as $operation) {
$method = $operation->getJobType(); $jobType = $operation->getJobType();
$promise = null; $promise = null;
if ($method === 'install') { if ($jobType === 'install') {
$package = $operation->getPackage(); $package = $operation->getPackage();
$installer = $this->getInstaller($package->getType()); $installer = $this->getInstaller($package->getType());
$promise = $installer->download($package); $promise = $installer->download($package);
} elseif ($method === 'update') { } elseif ($jobType === 'update') {
$target = $operation->getTargetPackage(); $target = $operation->getTargetPackage();
$targetType = $target->getType(); $targetType = $target->getType();
$installer = $this->getInstaller($targetType); $installer = $this->getInstaller($targetType);
@ -197,66 +197,80 @@ class InstallationManager
} }
foreach ($operations as $operation) { foreach ($operations as $operation) {
$method = $operation->getJobType(); $jobType = $operation->getJobType();
$event = 'Composer\Installer\PackageEvents::PRE_PACKAGE_'.strtoupper($method);
if (defined($event) && $runScripts && $this->eventDispatcher) {
$this->eventDispatcher->dispatchPackageEvent(constant($event), $devMode, $repo, $operations, $operation);
}
// ignoring alias ops as they don't need to execute anything
if (!in_array($jobType, array('update', 'install', 'uninstall'))) {
// output alias ops in debug verbosity as they have no output otherwise // output alias ops in debug verbosity as they have no output otherwise
if ($this->io->isDebug()) { if ($this->io->isDebug()) {
$this->io->writeError(' - ' . $operation->show(false)); $this->io->writeError(' - ' . $operation->show(false));
} }
continue;
}
$e = null; if ($jobType === 'install' || $jobType === 'uninstall') {
try {
if ($method === 'install' || $method === 'uninstall') {
$package = $operation->getPackage(); $package = $operation->getPackage();
$initialPackage = null;
} elseif ($jobType === 'update') {
$package = $operation->getTargetPackage();
$initialPackage = $operation->getInitialPackage();
}
$installer = $this->getInstaller($package->getType()); $installer = $this->getInstaller($package->getType());
$promise = $installer->prepare($method, $package);
} elseif ($method === 'update') {
$target = $operation->getTargetPackage();
$targetType = $target->getType();
$installer = $this->getInstaller($targetType);
$promise = $installer->prepare('update', $target, $operation->getInitialPackage());
}
if (!empty($promise)) { $event = 'Composer\Installer\PackageEvents::PRE_PACKAGE_'.strtoupper($jobType);
$this->loop->wait(array($promise));
}
$promise = $this->$method($repo, $operation);
if (!empty($promise)) {
$this->loop->wait(array($promise));
}
} catch (\Exception $e) {
}
if ($method === 'install' || $method === 'uninstall') {
$package = $operation->getPackage();
$installer = $this->getInstaller($package->getType());
$promise = $installer->cleanup($method, $package);
} elseif ($method === 'update') {
$target = $operation->getTargetPackage();
$targetType = $target->getType();
$installer = $this->getInstaller($targetType);
$promise = $installer->cleanup('update', $target, $operation->getInitialPackage());
}
if (!empty($promise)) {
$this->loop->wait(array($promise));
}
if ($e) {
throw $e;
}
$repo->write($devMode, $this);
$event = 'Composer\Installer\PackageEvents::POST_PACKAGE_'.strtoupper($method);
if (defined($event) && $runScripts && $this->eventDispatcher) { if (defined($event) && $runScripts && $this->eventDispatcher) {
$this->eventDispatcher->dispatchPackageEvent(constant($event), $devMode, $repo, $operations, $operation); $this->eventDispatcher->dispatchPackageEvent(constant($event), $devMode, $repo, $operations, $operation);
} }
$dispatcher = $this->eventDispatcher;
$installManager = $this;
$loop = $this->loop;
$io = $this->io;
$promise = new \React\Promise\Promise(function ($resolve, $reject) use ($installer, $jobType, $package, $initialPackage) {
$promise = $installer->prepare($jobType, $package, $initialPackage);
if (null === $promise) {
$promise = new \React\Promise\Promise(function ($resolve, $reject) { $resolve(); });
}
return $promise->then($resolve, $reject);
});
$promise = $promise->then(function () use ($jobType, $installManager, $repo, $operation) {
$promise = $installManager->$jobType($repo, $operation);
if (null === $promise) {
$promise = new \React\Promise\Promise(function ($resolve, $reject) { $resolve(); });
}
return $promise;
})->then(function () use ($jobType, $installer, $package, $initialPackage) {
$promise = $installer->cleanup($jobType, $package, $initialPackage);
if (null === $promise) {
$promise = new \React\Promise\Promise(function ($resolve, $reject) { $resolve(); });
}
return $promise;
})->then(function () use ($jobType, $installer, $package, $initialPackage, $runScripts, $dispatcher, $installManager, $devMode, $repo, $operations, $operation) {
$repo->write($devMode, $installManager);
$event = 'Composer\Installer\PackageEvents::POST_PACKAGE_'.strtoupper($jobType);
if (defined($event) && $runScripts && $dispatcher) {
$dispatcher->dispatchPackageEvent(constant($event), $devMode, $repo, $operations, $operation);
}
}, function ($e) use ($jobType, $installer, $package, $initialPackage, $loop, $io) {
$this->io->writeError(' <error>' . ucfirst($jobType) .' of '.$package->getPrettyName().' failed</error>');
$promise = $installer->cleanup($jobType, $package, $initialPackage);
$loop->wait(array($promise));
throw $e;
});
$promises[] = $promise;
}
if (!empty($promises)) {
$this->loop->wait($promises);
} }
} }