Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Package seems not been installed properly" #87

Open
wants to merge 4 commits into
base: 0.14
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 71 additions & 33 deletions src/Composer/StudioPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
];
}

Expand All @@ -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);
Expand All @@ -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');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line raises an error when $original is an instance of AliasPackage:

PHP Fatal error:  Uncaught Error: Call to undefined method Composer\Package\AliasPackage::setDistType()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for highlighting this. I've just run into this issue myself today. I'm now testing out a fix with my latest commit.

This does highlight a valid concern about the many configurations possible with composer. My workflow is simple, with packages installed through Packagist or VCS by tag or by branch. I'm sure theres plenty of other possibilities that my proposed change would not work for. That said - I'm not exactly sure how many of those workflows were possible before my commit?


$installationManager->getInstaller($original->getType())
->uninstall($installed, $original);

Expand All @@ -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']);
}
}

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -168,6 +206,6 @@ private function getManagedPaths()

private function write($msg)
{
$this->io->writeError("[Studio] $msg");
$this->io->write("[Studio] $msg");
}
}