Skip to content

Commit

Permalink
Merge pull request #782 from symfony/unpack-dev
Browse files Browse the repository at this point in the history
Unpack entries from require-dev
  • Loading branch information
nicolas-grekas authored Aug 20, 2021
2 parents 3682a58 + 2a3eae1 commit b298435
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 54 deletions.
21 changes: 15 additions & 6 deletions src/Command/UnpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Flex\Command;

use Composer\Command\BaseCommand;
use Composer\Factory;
use Composer\Installer;
use Composer\Package\Version\VersionParser;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -95,19 +96,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

$install = Installer::create($io, $composer);
$install
$composer = Factory::create($io, null, true);
$installer = Installer::create($io, $composer);
$installer
->setDryRun($dryRun)
->setDevMode(true)
->setDumpAutoloader(false)
->setRunScripts(false)
->setIgnorePlatformRequirements(true)
->setUpdate(true)
->setUpdateAllowList(['php'])
;

if (method_exists($install, 'setSkipSuggest')) {
$install->setSkipSuggest(true);
if (method_exists($composer->getEventDispatcher(), 'setRunScripts')) {
$composer->getEventDispatcher()->setRunScripts(false);
} else {
$installer->setRunScripts(false);
}

return $install->run();
if (method_exists($installer, 'setSkipSuggest')) {
$installer->setSkipSuggest(true);
}

return $installer->run();
}
}
3 changes: 1 addition & 2 deletions src/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Flex\Configurator;

use LogicException;
use Symfony\Flex\Lock;
use Symfony\Flex\Recipe;

Expand Down Expand Up @@ -101,7 +100,7 @@ public function copyFile(string $source, string $target, array $options)
}

if (!file_exists($source)) {
throw new LogicException(sprintf('File "%s" does not exist!', $source));
throw new \LogicException(sprintf('File "%s" does not exist!', $source));
}

file_put_contents($target, $this->options->expandTargetDir(file_get_contents($source)));
Expand Down
33 changes: 19 additions & 14 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\Installer\SuggestedPackagesReporter;
use Composer\IO\ConsoleIO;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use Composer\Json\JsonFile;
Expand All @@ -47,7 +46,6 @@
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Flex\Event\UpdateEvent;
use Symfony\Flex\Unpack\Operation;
Expand Down Expand Up @@ -443,21 +441,28 @@ public function update(Event $event = null, $operations = [])
$result = $unpacker->unpack($unpackOp);
$unpacker->updateLock($result, $this->io);

if ($this->io instanceof ConsoleIO) {
\Closure::bind(function () {
$this->output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}, $this->io, $this->io)();
$io = new NullIO();
$composer = Factory::create($io, null, true);
$installer = Installer::create($io, $composer);
$installer
->setDevMode($this->dryRun)
->setDumpAutoloader(false)
->setIgnorePlatformRequirements(true)
->setUpdate(true)
->setUpdateAllowList(['php'])
;

if (method_exists($composer->getEventDispatcher(), 'setRunScripts')) {
$composer->getEventDispatcher()->setRunScripts(false);
} else {
$installer->setRunScripts(false);
}

\Closure::bind(function ($locker) {
$this->locker = $locker;
$this->dumpAutoloader = false;
$this->runScripts = false;
$this->ignorePlatformReqs = true;
$this->update = false;
}, $this->installer, $this->installer)($this->composer->getLocker());
if (method_exists($installer, 'setSkipSuggest')) {
$installer->setSkipSuggest(true);
}

$this->installer->run();
$installer->run();
}

public function install(Event $event = null)
Expand Down
81 changes: 49 additions & 32 deletions src/Unpacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(Composer $composer, PackageResolver $resolver, bool
$this->versionParser = new VersionParser();
}

public function unpack(Operation $op, Result $result = null, &$links = []): Result
public function unpack(Operation $op, Result $result = null, &$links = [], bool $devRequire = false): Result
{
if (null === $result) {
$result = new Result();
Expand All @@ -69,50 +69,67 @@ public function unpack(Operation $op, Result $result = null, &$links = []): Resu
continue;
}

$versionSelector = null;
$requires = [];
foreach ($pkg->getRequires() as $link) {
if ('php' === $link->getTarget()) {
continue;
$requires[$link->getTarget()] = $link;
}
$devRequires = $pkg->getDevRequires();

foreach ($devRequires as $i => $link) {
if (!isset($requires[$link->getTarget()])) {
throw new \RuntimeException(sprintf('Symfony pack "%s" must duplicate all entries from "require-dev" into "require" but entry "%s" was not found.', $package['name'], $link->getTarget()));
}
$devRequires[$i] = $requires[$link->getTarget()];
unset($requires[$link->getTarget()]);
}

$constraint = $link->getPrettyConstraint();
$constraint = substr($this->resolver->parseVersion($link->getTarget(), $constraint, !$package['dev']), 1) ?: $constraint;
$versionSelector = null;
foreach ([$requires, $devRequires] as $dev => $requires) {
$dev = $dev ?: $devRequire ?: $package['dev'];

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, $links);
foreach ($requires as $link) {
if ('php' === $linkName = $link->getTarget()) {
continue;
}

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 = $link->getPrettyConstraint();
$constraint = substr($this->resolver->parseVersion($linkName, $constraint, true), 1) ?: $constraint;

if ($subPkg = $localRepo->findPackage($linkName, '*')) {
if ('symfony-pack' === $subPkg->getType()) {
$subOp = new Operation(true, $op->shouldSort());
$subOp->addPackage($subPkg->getName(), $constraint, $dev);
$result = $this->unpack($subOp, $result, $links, $dev);
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);
}
}
}

$linkName = $link->getTarget();
$linkType = $package['dev'] ? 'require-dev' : 'require';
$constraint = $this->versionParser->parseConstraints($constraint);
$linkType = $dev ? 'require-dev' : 'require';
$constraint = $this->versionParser->parseConstraints($constraint);

if (isset($links[$linkName])) {
$links[$linkName]['constraints'][] = $constraint;
if ('require' === $linkType) {
$links[$linkName]['type'] = 'require';
if (isset($links[$linkName])) {
$links[$linkName]['constraints'][] = $constraint;
if ('require' === $linkType) {
$links[$linkName]['type'] = 'require';
}
} else {
$links[$linkName] = [
'type' => $linkType,
'name' => $linkName,
'constraints' => [$constraint],
];
}
} else {
$links[$linkName] = [
'type' => $linkType,
'name' => $linkName,
'constraints' => [$constraint],
];
}
}
}
Expand Down

0 comments on commit b298435

Please sign in to comment.