Skip to content

Commit

Permalink
feature #652 Unpack recursively (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.8-dev branch.

Discussion
----------

Unpack recursively

Commits
-------

5902f59 Unpack recursively
  • Loading branch information
nicolas-grekas committed Jul 14, 2020
2 parents 1cea72f + 5902f59 commit 894c994
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/Command/UnpackCommand.php
Expand Up @@ -61,6 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$lockData = $locker->getLockData();
$installedRepo = $composer->getRepositoryManager()->getLocalRepository();
$versionParser = new VersionParser();
$dryRun = $input->hasOption('dry-run') && $input->getOption('dry-run');

$op = new Operation(true, $input->getOption('sort-packages') || $composer->getConfig()->get('sort-packages'));
foreach ($versionParser->parseNameVersionPairs($packages) as $package) {
Expand All @@ -82,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$op->addPackage($pkg->getName(), $pkg->getVersion(), $dev);
}

$unpacker = new Unpacker($composer, $this->resolver);
$unpacker = new Unpacker($composer, $this->resolver, $dryRun);
$result = $unpacker->unpack($op);

// remove the packages themselves
Expand Down Expand Up @@ -115,8 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$lockData['content-hash'] = $locker->getContentHash(file_get_contents($json->getPath()));
$lockFile = new JsonFile(substr($json->getPath(), 0, -4).'lock', null, $io);

$dryRun = $input->hasOption('dry-run') && !$input->getOption('dry-run');
if ($dryRun) {
if (!$dryRun) {
$lockFile->write($lockData);
}

Expand Down
12 changes: 10 additions & 2 deletions src/Unpack/Result.php
Expand Up @@ -18,9 +18,17 @@ class Result
private $unpacked = [];
private $required = [];

public function addUnpacked(PackageInterface $package)
public function addUnpacked(PackageInterface $package): bool
{
$this->unpacked[] = $package;
$name = $package->getName();

if (!isset($this->unpacked[$name])) {
$this->unpacked[$name] = $package;

return true;
}

return false;
}

/**
Expand Down
52 changes: 36 additions & 16 deletions src/Unpacker.php
Expand Up @@ -14,7 +14,6 @@
use Composer\Composer;
use Composer\DependencyResolver\Pool;
use Composer\Factory;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Package\Version\VersionSelector;
use Composer\Repository\CompositeRepository;
Expand All @@ -26,18 +25,25 @@ class Unpacker
{
private $composer;
private $resolver;
private $dryRun;
private $jsonPath;
private $manipulator;

public function __construct(Composer $composer, PackageResolver $resolver)
public function __construct(Composer $composer, PackageResolver $resolver, bool $dryRun)
{
$this->composer = $composer;
$this->resolver = $resolver;
$this->dryRun = $dryRun;
}

public function unpack(Operation $op): Result
public function unpack(Operation $op, Result $result = null): Result
{
$result = new Result();
$json = new JsonFile(Factory::getComposerFile());
$manipulator = new JsonManipulator(file_get_contents($json->getPath()));
if (null === $result) {
$result = new Result();
$this->jsonPath = Factory::getComposerFile();
$this->manipulator = new JsonManipulator(file_get_contents($this->jsonPath));
}

$localRepo = $this->composer->getRepositoryManager()->getLocalRepository();
foreach ($op->getPackages() as $package) {
$pkg = $localRepo->findPackage($package['name'], $package['version'] ?: '*');
Expand All @@ -55,8 +61,11 @@ public function unpack(Operation $op): Result
continue;
}

if (!$result->addUnpacked($pkg)) {
continue;
}

$versionSelector = null;
$result->addUnpacked($pkg);
foreach ($pkg->getRequires() as $link) {
if ('php' === $link->getTarget()) {
continue;
Expand All @@ -65,24 +74,35 @@ public function unpack(Operation $op): Result
$constraint = $link->getPrettyConstraint();
$constraint = substr($this->resolver->parseVersion($link->getTarget(), $constraint, !$package['dev']), 1) ?: $constraint;

if ('*' === $constraint && $subPkg = $localRepo->findPackage($link->getTarget(), '*')) {
if (null === $versionSelector) {
$pool = class_exists(RepositorySet::class) ? RepositorySet::class : Pool::class;
$pool = new $pool($this->composer->getPackage()->getMinimumStability(), $this->composer->getPackage()->getStabilityFlags());
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
$versionSelector = new VersionSelector($pool);
if ($subPkg = $localRepo->findPackage($link->getTarget(), '*')) {
if ('symfony-pack' === $subPkg->getType()) {
$subOp = new Operation(true, $op->shouldSort());
$subOp->addPackage($subPkg->getName(), $constraint, $package['dev']);
$result = $this->unpack($subOp, $result);
continue;
}

$constraint = $versionSelector->findRecommendedRequireVersion($subPkg);
if ('*' === $constraint) {
if (null === $versionSelector) {
$pool = class_exists(RepositorySet::class) ? RepositorySet::class : Pool::class;
$pool = new $pool($this->composer->getPackage()->getMinimumStability(), $this->composer->getPackage()->getStabilityFlags());
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
$versionSelector = new VersionSelector($pool);
}

$constraint = $versionSelector->findRecommendedRequireVersion($subPkg);
}
}

if (!$manipulator->addLink($package['dev'] ? 'require-dev' : 'require', $link->getTarget(), $constraint, $op->shouldSort())) {
if (!$this->manipulator->addLink($package['dev'] ? 'require-dev' : 'require', $link->getTarget(), $constraint, $op->shouldSort())) {
throw new \RuntimeException(sprintf('Unable to unpack package "%s".', $link->getTarget()));
}
}
}

file_put_contents($json->getPath(), $manipulator->getContents());
if (!$this->dryRun && 1 === \func_num_args()) {
file_put_contents($this->jsonPath, $this->manipulator->getContents());
}

return $result;
}
Expand Down

0 comments on commit 894c994

Please sign in to comment.