From 4f5d979beebe5646b878b203c6efda8889ed2129 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 23 Jul 2014 19:01:22 +0200 Subject: [PATCH] ArchiveCommand improvements --- src/Composer/Command/ArchiveCommand.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Composer/Command/ArchiveCommand.php b/src/Composer/Command/ArchiveCommand.php index d7e627b34..34f6fe8a6 100644 --- a/src/Composer/Command/ArchiveCommand.php +++ b/src/Composer/Command/ArchiveCommand.php @@ -18,6 +18,9 @@ use Composer\DependencyResolver\Pool; use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Repository\CompositeRepository; use Composer\Script\ScriptEvents; +use Composer\Plugin\CommandEvent; +use Composer\Plugin\PluginEvents; +use Composer\Package\Version\VersionParser; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -38,7 +41,7 @@ class ArchiveCommand extends Command ->setDescription('Create an archive of this composer package') ->setDefinition(array( new InputArgument('package', InputArgument::OPTIONAL, 'The package to archive instead of the current project'), - new InputArgument('version', InputArgument::OPTIONAL, 'The package version to archive'), + new InputArgument('version', InputArgument::OPTIONAL, 'A version constraint to find the package to archive'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the resulting archive: tar or zip', 'tar'), new InputOption('dir', false, InputOption::VALUE_REQUIRED, 'Write the archive to this directory', '.'), )) @@ -56,7 +59,12 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { - $this->getComposer()->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_ARCHIVE_CMD); + $composer = $this->getComposer(false); + if ($composer) { + $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'archive', $input, $output); + $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); + $composer->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_ARCHIVE_CMD); + } $returnCode = $this->archive( $this->getIO(), @@ -66,8 +74,8 @@ EOT $input->getOption('dir') ); - if (0 === $returnCode) { - $this->getComposer()->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ARCHIVE_CMD); + if (0 === $returnCode && $composer) { + $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ARCHIVE_CMD); } return $returnCode; @@ -112,16 +120,17 @@ EOT $pool = new Pool(); $pool->addRepository($repos); - $constraint = ($version) ? new VersionConstraint('>=', $version) : null; - $packages = $pool->whatProvides($packageName, $constraint); + $parser = new VersionParser(); + $constraint = ($version) ? $parser->parseConstraints($version) : null; + $packages = $pool->whatProvides($packageName, $constraint, true); if (count($packages) > 1) { - $package = $packages[0]; + $package = reset($packages); $io->write('Found multiple matches, selected '.$package->getPrettyString().'.'); $io->write('Alternatives were '.implode(', ', array_map(function ($p) { return $p->getPrettyString(); }, $packages)).'.'); $io->write('Please use a more specific constraint to pick a different package.'); } elseif ($packages) { - $package = $packages[0]; + $package = reset($packages); $io->write('Found an exact match '.$package->getPrettyString().'.'); } else { $io->write('Could not find a package matching '.$packageName.'.');