From d8bfb8bf70f09196f6bb7a68bb82ba25e5ff9a68 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 19 Sep 2018 11:36:29 +0930 Subject: [PATCH 1/3] Fix "Package seems not been installed properly" The installationSource and distType on original packages are reconfigured in the uninstall/install step. This prevents the "Package seems not been installed properly" error, and also "The package has modified files" warning after running `composer update`. Update StudioPlugin.php --- src/Composer/StudioPlugin.php | 104 +++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 33 deletions(-) diff --git a/src/Composer/StudioPlugin.php b/src/Composer/StudioPlugin.php index d07b368..c7b5a7a 100644 --- a/src/Composer/StudioPlugin.php +++ b/src/Composer/StudioPlugin.php @@ -28,6 +28,16 @@ class StudioPlugin implements PluginInterface, EventSubscriberInterface */ protected $io; + /** + * @var bool + */ + protected $studioPackagesLoaded = false; + + /** + * @var array + */ + protected $studioPackages = []; + public function activate(Composer $composer, IOInterface $io) { $this->composer = $composer; @@ -36,10 +46,11 @@ public function activate(Composer $composer, IOInterface $io) public static function getSubscribedEvents() { - // TODO: Before update, append Studio path repositories return [ - ScriptEvents::POST_UPDATE_CMD => 'symlinkStudioPackages', - ScriptEvents::PRE_AUTOLOAD_DUMP => 'loadStudioPackagesForDump', + ScriptEvents::POST_UPDATE_CMD => 'symlinkStudioPackages', + ScriptEvents::POST_INSTALL_CMD => 'symlinkStudioPackages', + ScriptEvents::PRE_AUTOLOAD_DUMP => 'loadStudioPackages', + ScriptEvents::POST_AUTOLOAD_DUMP => 'revertStudioPackages', ]; } @@ -51,8 +62,6 @@ public static function getSubscribedEvents() */ public function symlinkStudioPackages() { - $intersection = $this->getManagedPackages(); - // Create symlinks for all left-over packages in vendor/composer/studio $destination = $this->composer->getConfig()->get('vendor-dir') . '/composer/studio'; (new Filesystem())->emptyDirectory($destination); @@ -65,9 +74,13 @@ public function symlinkStudioPackages() // Get local repository which contains all installed packages $installed = $this->composer->getRepositoryManager()->getLocalRepository(); - foreach ($intersection as $package) { + foreach ($this->getManagedPackages() as $package) { $original = $installed->findPackage($package->getName(), '*'); + // Change the source type to path, to prevent 'The package has modified files' + $original->setInstallationSource('dist'); + $original->setDistType('path'); + $installationManager->getInstaller($original->getType()) ->uninstall($installed, $original); @@ -76,34 +89,65 @@ public function symlinkStudioPackages() } $studioRepo->write(); + } - // TODO: Run dump-autoload again + /** + * Swap installed packages with symlinked versions for autoload dump. + */ + public function loadStudioPackages() + { + $this->registerStudioPackages(); + + $this->swapPackages(); } - public function loadStudioPackagesForDump() + /** + * Revert swapped package versions when autoload dump is complete. + */ + public function revertStudioPackages() { - $localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); - $intersection = $this->getManagedPackages(); + $this->swapPackages(true); + } - $packagesToReplace = []; - foreach ($intersection as $package) { - $packagesToReplace[] = $package->getName(); - } + /** + * If Studio packages have not already been loaded, we need to determine + * which ones specified in studio.json are installed for this repo. + */ + public function registerStudioPackages() + { + if(!$this->studioPackagesLoaded) { + $this->studioPackagesLoaded = true; + + $localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); + + foreach ($this->getManagedPackages() as $package) { + $this->write('Loading package ' . $package->getName()); + $this->studioPackages[$package->getName()] = [ + 'studio' => $package + ]; + } - // Remove all packages with same names as one of symlinked packages - $packagesToRemove = []; - foreach ($localRepo->getCanonicalPackages() as $package) { - if (in_array($package->getName(), $packagesToReplace)) { - $packagesToRemove[] = $package; + foreach ($localRepo->getCanonicalPackages() as $package) { + if (isset($this->studioPackages[$package->getName()])) { + $this->studioPackages[$package->getName()]['original'] = $package; + } } } - foreach ($packagesToRemove as $package) { - $localRepo->removePackage($package); - } + } + + /** + * Remove original packages from local repository manager and replace with + * studio/symlinked packages. + * + * @param bool $revert Revert flag will undo this change. + */ + protected function swapPackages($revert = false) + { + $localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); - // Add symlinked packages to local repository - foreach ($intersection as $package) { - $localRepo->addPackage(clone $package); + foreach ($this->studioPackages as $package) { + $localRepo->removePackage($package[$revert ? 'studio' : 'original']); + $localRepo->addPackage(clone $package[$revert ? 'original' : 'studio']); } } @@ -141,16 +185,10 @@ private function getManagedPackages() } // Intersect PathRepository packages with local repository - $intersection = $this->getIntersection( + return $this->getIntersection( $this->composer->getRepositoryManager()->getLocalRepository(), $managed ); - - foreach ($intersection as $package) { - $this->write('Loading package ' . $package->getUniqueName()); - } - - return $intersection; } /** @@ -168,6 +206,6 @@ private function getManagedPaths() private function write($msg) { - $this->io->writeError("[Studio] $msg"); + $this->io->write("[Studio] $msg"); } } From 844876c3a2e229ce78f0d1096ef60b431ee74607 Mon Sep 17 00:00:00 2001 From: Ryan Brereton-Canham <9389421+ryzr@users.noreply.github.com> Date: Mon, 11 Feb 2019 12:20:53 +1030 Subject: [PATCH 2/3] Add support for AliasedPackage --- src/Composer/StudioPlugin.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Composer/StudioPlugin.php b/src/Composer/StudioPlugin.php index c7b5a7a..dd95c95 100644 --- a/src/Composer/StudioPlugin.php +++ b/src/Composer/StudioPlugin.php @@ -76,16 +76,16 @@ public function symlinkStudioPackages() foreach ($this->getManagedPackages() as $package) { $original = $installed->findPackage($package->getName(), '*'); + $originalPackage = $original instanceof AliasPackage ? $original->getAliasOf() : $original; // Change the source type to path, to prevent 'The package has modified files' - $original->setInstallationSource('dist'); - $original->setDistType('path'); - - $installationManager->getInstaller($original->getType()) - ->uninstall($installed, $original); + if ($originalPackage instanceof CompletePackage) { + $originalPackage->setInstallationSource('dist'); + $originalPackage->setDistType('path'); + } - $installationManager->getInstaller($package->getType()) - ->install($studioRepo, $package); + $installationManager->getInstaller($original->getType())->uninstall($installed, $original); + $installationManager->getInstaller($package->getType())->install($LocalPackagesRepo, $package); } $studioRepo->write(); From 184f3175f7e8b12891f4b5a5b64e23ae71d4472e Mon Sep 17 00:00:00 2001 From: Warren Amphlett Date: Fri, 17 May 2019 13:42:56 +0100 Subject: [PATCH 3/3] Fix for undefined LocalPackagesRepo variable --- src/Composer/StudioPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/StudioPlugin.php b/src/Composer/StudioPlugin.php index dd95c95..e17e2ad 100644 --- a/src/Composer/StudioPlugin.php +++ b/src/Composer/StudioPlugin.php @@ -85,7 +85,7 @@ public function symlinkStudioPackages() } $installationManager->getInstaller($original->getType())->uninstall($installed, $original); - $installationManager->getInstaller($package->getType())->install($LocalPackagesRepo, $package); + $installationManager->getInstaller($package->getType())->install($studioRepo, $package); } $studioRepo->write();