Skip to content

Unpack recursively #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Command/UnpackCommand.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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