Skip to content

Commit acd3b0f

Browse files
committed
Adding the ability to run recipes:update with no arguments
1 parent 0eb4b43 commit acd3b0f

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

src/Command/UpdateRecipesCommand.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Flex\Command;
1313

1414
use Composer\Command\BaseCommand;
15+
use Composer\IO\IOInterface;
1516
use Composer\Util\HttpDownloader;
1617
use Symfony\Component\Console\Exception\RuntimeException;
1718
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
@@ -23,6 +24,7 @@
2324
use Symfony\Flex\Flex;
2425
use Symfony\Flex\GithubApi;
2526
use Symfony\Flex\InformationOperation;
27+
use Symfony\Flex\Lock;
2628
use Symfony\Flex\Recipe;
2729
use Symfony\Flex\Update\RecipePatcher;
2830
use Symfony\Flex\Update\RecipeUpdate;
@@ -52,7 +54,7 @@ protected function configure()
5254
$this->setName('symfony:recipes:update')
5355
->setAliases(['recipes:update'])
5456
->setDescription('Updates an already-installed recipe to the latest version.')
55-
->addArgument('package', InputArgument::REQUIRED, 'Recipe that should be updated.')
57+
->addArgument('package', InputArgument::OPTIONAL, 'Recipe that should be updated.')
5658
;
5759
}
5860

@@ -67,6 +69,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6769

6870
$packageName = $input->getArgument('package');
6971
$symfonyLock = $this->flex->getLock();
72+
if (!$packageName) {
73+
$packageName = $this->askForPackage($io, $symfonyLock);
74+
75+
if (null === $packageName) {
76+
$io->writeError('All packages appear to be up-to-date!');
77+
78+
return 0;
79+
}
80+
}
81+
7082
if (!$symfonyLock->has($packageName)) {
7183
$io->writeError([
7284
'Package not found inside symfony.lock. It looks like it\'s not installed?',
@@ -293,4 +305,49 @@ private function generateChangelog(Recipe $originalRecipe): ?array
293305

294306
return $lines;
295307
}
308+
309+
private function askForPackage(IOInterface $io, Lock $symfonyLock): ?string
310+
{
311+
$installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository();
312+
$locker = $this->getComposer()->getLocker();
313+
$lockData = $locker->getLockData();
314+
315+
// Merge all packages installed
316+
$packages = array_merge($lockData['packages'], $lockData['packages-dev']);
317+
318+
$operations = [];
319+
foreach ($packages as $value) {
320+
if (null === $pkg = $installedRepo->findPackage($value['name'], '*')) {
321+
continue;
322+
}
323+
324+
$operations[] = new InformationOperation($pkg);
325+
}
326+
327+
$recipes = $this->flex->fetchRecipes($operations, false);
328+
ksort($recipes);
329+
330+
$outdatedRecipes = [];
331+
foreach ($recipes as $name => $recipe) {
332+
$lockRef = $symfonyLock->get($name)['recipe']['ref'] ?? null;
333+
334+
if (null !== $lockRef && $recipe->getRef() !== $lockRef && !$recipe->isAuto()) {
335+
$outdatedRecipes[] = $name;
336+
}
337+
}
338+
339+
if (0 === \count($outdatedRecipes)) {
340+
return null;
341+
}
342+
343+
$question = 'Which outdated recipe would you like to update?';
344+
345+
$choice = $io->select(
346+
$question,
347+
$outdatedRecipes,
348+
null
349+
);
350+
351+
return $outdatedRecipes[$choice];
352+
}
296353
}

0 commit comments

Comments
 (0)