diff --git a/src/Plugin.php b/src/Plugin.php index 53748eb..4a1f1d9 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -216,6 +216,28 @@ public function install(Event $event) { protected function processPackage(array $processed_drupal_libraries, array $drupal_libraries, PackageInterface $package) { $extra = $package->getExtra(); + if ($package->getType() === 'project') { + foreach (['drupal-libraries', 'drupal-libraries-include'] as $key) { + $extra[$key] = is_array($extra[$key] ?? NULL) ? $extra[$key] : []; + } + foreach ($extra['drupal-libraries-include'] as $include) { + if (!preg_match('/composer.libraries.json$/', $include) || !file_exists($include)) { + continue; + } + + $json = JsonFile::parseJson(@file_get_contents($include), $include); + foreach ($json['repositories'] ?? [] as $repository) { + $package_type = $repository['package']['type'] ?? ''; + $package_name = $repository['package']['extra']['installer-name'] ?? $repository['package']['name'] ?? ''; + $dist = $repository['package']['dist'] ?? NULL; + $exists = array_key_exists($package_name, $extra['drupal-libraries']); + if (!$exists && $dist && $package_type === 'drupal-library' && substr($package_name, 0, 1) !== '/') { + $extra['drupal-libraries'][$package_name] = $dist; + } + } + } + } + if (empty($extra['drupal-libraries']) || !is_array($extra['drupal-libraries'])) { return $processed_drupal_libraries; } @@ -293,11 +315,20 @@ protected function processPackage(array $processed_drupal_libraries, array $drup protected function removeUnusedLibraries(array $old_libraries) { foreach ($old_libraries as $library_name => $library_definition) { $library_package = $this->getLibraryPackage($library_name, $library_definition); + $install_path = $this->installationManager->getInstallPath($library_package); + $vendor = explode('/', $library_name)[0] ?: NULL; + $this->composer->getLoop()->wait([ + $this->downloadManager->remove($library_package, $install_path)->done(function () use ($install_path, $vendor) { + if ($vendor === 'libraries' || basename(dirname($install_path)) !== $vendor) { + return; + } - $this->downloadManager->remove( - $library_package, - $this->installationManager->getInstallPath($library_package) - ); + $vendor_dir = dirname($install_path); + if (is_dir($vendor_dir) && $this->fileSystem->isDirEmpty($vendor_dir)) { + $this->fileSystem->removeDirectory($vendor_dir); + } + }) + ]); } } @@ -552,7 +583,11 @@ protected function getLibraryPackage($library_name, array $library_definition) { $library_package->setDistSha1Checksum($library_definition['shasum']); } $library_package->setType('drupal-library'); - + if (strpos($library_name, '/') && substr($library_name, 0, 1) !== '/') { + $extra = $library_package->getExtra(); + $extra['installer-name'] = $extra['installer-name'] ?? $library_name; + $library_package->setExtra($extra); + } return $library_package; }