Skip to content

Commit

Permalink
Read from symfony.lock when checking for recipe updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jun 6, 2022
1 parent 2744418 commit 9c61279
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
17 changes: 12 additions & 5 deletions src/Command/RecipesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Composer\Command\BaseCommand;
use Composer\Downloader\TransportException;
use Composer\Package\Package;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -61,19 +62,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Inspect one or all packages
$package = $input->getArgument('package');
if (null !== $package) {
$packages = [0 => ['name' => strtolower($package)]];
$packages = [strtolower($package)];
} else {
$locker = $this->getComposer()->getLocker();
$lockData = $locker->getLockData();

// Merge all packages installed
$packages = array_merge($lockData['packages'], $lockData['packages-dev']);
$packages = array_column(array_merge($lockData['packages'], $lockData['packages-dev']), 'name');
$packages = array_unique(array_merge($packages, array_keys($this->symfonyLock->all())));
}

$operations = [];
foreach ($packages as $value) {
if (null === $pkg = $installedRepo->findPackage($value['name'], '*')) {
$this->getIO()->writeError(sprintf('<error>Package %s is not installed</error>', $value['name']));
foreach ($packages as $name) {
$pkg = $installedRepo->findPackage($name, '*');

if (!$pkg && $this->symfonyLock->has($name)) {
$pkgVersion = $this->symfonyLock->get($name)['version'];
$pkg = new Package($name, $pkgVersion, $pkgVersion);
} elseif (!$pkg) {
$this->getIO()->writeError(sprintf('<error>Package %s is not installed</error>', $name));

continue;
}
Expand Down
34 changes: 13 additions & 21 deletions src/Command/UpdateRecipesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Composer\Command\BaseCommand;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Util\ProcessExecutor;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
Expand Down Expand Up @@ -122,7 +123,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$originalRecipe = $this->getRecipe($packageName, $recipeRef, $recipeVersion);
$installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
$package = $installedRepo->findPackage($packageName, '*') ?? new Package($packageName, $packageLockData['version'], $packageLockData['version']);
$originalRecipe = $this->getRecipe($package, $recipeRef, $recipeVersion);

if (null === $originalRecipe) {
$io->writeError([
Expand All @@ -134,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$newRecipe = $this->getRecipe($packageName);
$newRecipe = $this->getRecipe($package);

if ($newRecipe->getRef() === $originalRecipe->getRef()) {
$io->write(sprintf('This recipe for <info>%s</info> is already at the latest version.', $packageName));
Expand Down Expand Up @@ -259,13 +262,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function getRecipe(string $packageName, string $recipeRef = null, string $recipeVersion = null): ?Recipe
private function getRecipe(Package $package, string $recipeRef = null, string $recipeVersion = null): ?Recipe
{
$installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
$package = $installedRepo->findPackage($packageName, '*');
if (null === $package) {
throw new RuntimeException(sprintf('Could not find package "%s". Try running "composer install".', $packageName));
}
$operation = new InformationOperation($package);
if (null !== $recipeRef) {
$operation->setSpecificRecipeVersion($recipeRef, $recipeVersion);
Expand All @@ -278,10 +276,10 @@ private function getRecipe(string $packageName, string $recipeRef = null, string

return new Recipe(
$package,
$packageName,
$package->getName(),
$operation->getOperationType(),
$recipes['manifests'][$packageName],
$recipes['locks'][$packageName] ?? []
$recipes['manifests'][$package->getName()],
$recipes['locks'][$package->getName()] ?? []
);
}

Expand Down Expand Up @@ -358,19 +356,13 @@ private function generateChangelog(Recipe $originalRecipe): ?array
private function askForPackage(IOInterface $io, Lock $symfonyLock): ?string
{
$installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
$locker = $this->getComposer()->getLocker();
$lockData = $locker->getLockData();

// Merge all packages installed
$packages = array_merge($lockData['packages'], $lockData['packages-dev']);

$operations = [];
foreach ($packages as $value) {
if (null === $pkg = $installedRepo->findPackage($value['name'], '*')) {
continue;
foreach ($symfonyLock->all() as $name => $lock) {
if (isset($lock['recipe']['ref'])) {
$package = $installedRepo->findPackage($name, '*') ?? new Package($name, $lock['version'], $lock['version']);
$operations[] = new InformationOperation($package);
}

$operations[] = new InformationOperation($pkg);
}

$recipes = $this->flex->fetchRecipes($operations, false);
Expand Down
1 change: 0 additions & 1 deletion src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ public function getRecipes(array $operations): array
}

if (null !== $this->endpoints) {
$data['locks'][$package->getName()]['version'] = $version;
continue;
}

Expand Down
12 changes: 7 additions & 5 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public function recordOperations(InstallerEvent $event)
}, null, Transaction::class)();

foreach ($transation->getOperations() as $operation) {
if ($this->shouldRecordOperation($operation, $event->isDevMode(), $event->getComposer())) {
if (!$operation instanceof UninstallOperation && $this->shouldRecordOperation($operation, $event->isDevMode(), $event->getComposer())) {
$this->operations[] = $operation;
}
}
Expand Down Expand Up @@ -822,9 +822,7 @@ public function fetchRecipes(array $operations, bool $reset): array
} else {
$recipes[$name] = $recipe;
}
}

if (!isset($manifests[$name])) {
} else {
$bundles = [];

if (null === $devPackages) {
Expand All @@ -842,6 +840,10 @@ public function fetchRecipes(array $operations, bool $reset): array
'manifest' => ['bundles' => $bundles],
];
$recipes[$name] = new Recipe($package, $name, $job, $manifest);

if ($operation instanceof InstallOperation) {
$this->lock->set($name, ['version' => $package->getPrettyVersion()]);
}
}
}
}
Expand Down Expand Up @@ -1058,6 +1060,7 @@ public static function getSubscribedEvents(): array
}

$events = [
PackageEvents::POST_PACKAGE_UNINSTALL => 'record',
ScriptEvents::POST_CREATE_PROJECT_CMD => 'configureProject',
ScriptEvents::POST_INSTALL_CMD => 'install',
ScriptEvents::PRE_UPDATE_CMD => 'configureInstaller',
Expand All @@ -1069,7 +1072,6 @@ public static function getSubscribedEvents(): array
$events += [
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]],
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', \PHP_INT_MAX]],
PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir', ~\PHP_INT_MAX]],
Expand Down

0 comments on commit 9c61279

Please sign in to comment.