1
0
Fork 0

Improve version selection in archive command, fixes #4794 (#11230)

pull/11233/head
Jordi Boggiano 2022-12-18 00:02:43 +01:00 committed by GitHub
parent 685ec29573
commit 5e6ccae116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View File

@ -10,6 +10,11 @@ parameters:
count: 1
path: ../src/Composer/Autoload/ClassLoader.php
-
message: "#^Casting to string something that's already string\\.$#"
count: 1
path: ../src/Composer/Command/ArchiveCommand.php
-
message: "#^Only booleans are allowed in an if condition, string\\|false given\\.$#"
count: 1

View File

@ -180,11 +180,6 @@ parameters:
count: 1
path: ../src/Composer/Command/ArchiveCommand.php
-
message: "#^Only booleans are allowed in an elseif condition, array\\<Composer\\\\Package\\\\BasePackage\\> given\\.$#"
count: 1
path: ../src/Composer/Command/ArchiveCommand.php
-
message: "#^Only booleans are allowed in an if condition, Composer\\\\Composer\\|null given\\.$#"
count: 3

View File

@ -18,8 +18,12 @@ use Composer\Config;
use Composer\Composer;
use Composer\Package\BasePackage;
use Composer\Package\CompletePackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Package\Version\VersionSelector;
use Composer\Pcre\Preg;
use Composer\Repository\CompositeRepository;
use Composer\Repository\RepositoryFactory;
use Composer\Repository\RepositorySet;
use Composer\Script\ScriptEvents;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
@ -156,22 +160,38 @@ EOT
if ($composer = $this->tryComposer()) {
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$repo = new CompositeRepository(array_merge([$localRepo], $composer->getRepositoryManager()->getRepositories()));
$minStability = $composer->getPackage()->getMinimumStability();
} else {
$defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io);
$io->writeError('No composer.json found in the current directory, searching packages from ' . implode(', ', array_keys($defaultRepos)));
$repo = new CompositeRepository($defaultRepos);
$minStability = 'stable';
}
$packages = $repo->findPackages($packageName, $version);
if ($version !== null && Preg::isMatchStrictGroups('{@(stable|RC|beta|alpha|dev)$}i', $version, $match)) {
$minStability = $match[1];
$version = (string) substr($version, 0, -strlen($match[0]));
}
$repoSet = new RepositorySet($minStability);
$repoSet->addRepository($repo);
$parser = new VersionParser();
$constraint = $version !== null ? $parser->parseConstraints($version) : null;
$packages = $repoSet->findPackages(strtolower($packageName), $constraint);
if (count($packages) > 1) {
$versionSelector = new VersionSelector($repoSet);
$package = $versionSelector->findBestCandidate(strtolower($packageName), $version, $minStability);
if ($package === false) {
$package = reset($packages);
}
$io->writeError('<info>Found multiple matches, selected '.$package->getPrettyString().'.</info>');
$io->writeError('Alternatives were '.implode(', ', array_map(static function ($p): string {
return $p->getPrettyString();
}, $packages)).'.');
$io->writeError('<comment>Please use a more specific constraint to pick a different package.</comment>');
} elseif ($packages) {
} elseif (count($packages) === 1) {
$package = reset($packages);
$io->writeError('<info>Found an exact match '.$package->getPrettyString().'.</info>');
} else {
@ -183,6 +203,9 @@ EOT
if (!$package instanceof CompletePackageInterface) {
throw new \LogicException('Expected a CompletePackageInterface instance but found '.get_class($package));
}
if (!$package instanceof BasePackage) {
throw new \LogicException('Expected a BasePackage instance but found '.get_class($package));
}
return $package;
}