Skip to content

Commit b298435

Browse files
Merge pull request #782 from symfony/unpack-dev
Unpack entries from require-dev
2 parents 3682a58 + 2a3eae1 commit b298435

File tree

4 files changed

+84
-54
lines changed

4 files changed

+84
-54
lines changed

src/Command/UnpackCommand.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Flex\Command;
1313

1414
use Composer\Command\BaseCommand;
15+
use Composer\Factory;
1516
use Composer\Installer;
1617
use Composer\Package\Version\VersionParser;
1718
use Symfony\Component\Console\Input\InputArgument;
@@ -95,19 +96,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
9596
return 0;
9697
}
9798

98-
$install = Installer::create($io, $composer);
99-
$install
99+
$composer = Factory::create($io, null, true);
100+
$installer = Installer::create($io, $composer);
101+
$installer
100102
->setDryRun($dryRun)
101103
->setDevMode(true)
102104
->setDumpAutoloader(false)
103-
->setRunScripts(false)
104105
->setIgnorePlatformRequirements(true)
106+
->setUpdate(true)
107+
->setUpdateAllowList(['php'])
105108
;
106109

107-
if (method_exists($install, 'setSkipSuggest')) {
108-
$install->setSkipSuggest(true);
110+
if (method_exists($composer->getEventDispatcher(), 'setRunScripts')) {
111+
$composer->getEventDispatcher()->setRunScripts(false);
112+
} else {
113+
$installer->setRunScripts(false);
109114
}
110115

111-
return $install->run();
116+
if (method_exists($installer, 'setSkipSuggest')) {
117+
$installer->setSkipSuggest(true);
118+
}
119+
120+
return $installer->run();
112121
}
113122
}

src/Configurator/CopyFromPackageConfigurator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Flex\Configurator;
1313

14-
use LogicException;
1514
use Symfony\Flex\Lock;
1615
use Symfony\Flex\Recipe;
1716

@@ -101,7 +100,7 @@ public function copyFile(string $source, string $target, array $options)
101100
}
102101

103102
if (!file_exists($source)) {
104-
throw new LogicException(sprintf('File "%s" does not exist!', $source));
103+
throw new \LogicException(sprintf('File "%s" does not exist!', $source));
105104
}
106105

107106
file_put_contents($target, $this->options->expandTargetDir(file_get_contents($source)));

src/Flex.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Composer\Installer\PackageEvent;
2929
use Composer\Installer\PackageEvents;
3030
use Composer\Installer\SuggestedPackagesReporter;
31-
use Composer\IO\ConsoleIO;
3231
use Composer\IO\IOInterface;
3332
use Composer\IO\NullIO;
3433
use Composer\Json\JsonFile;
@@ -47,7 +46,6 @@
4746
use Composer\Script\Event;
4847
use Composer\Script\ScriptEvents;
4948
use Symfony\Component\Console\Input\ArgvInput;
50-
use Symfony\Component\Console\Output\OutputInterface;
5149
use Symfony\Component\Filesystem\Filesystem;
5250
use Symfony\Flex\Event\UpdateEvent;
5351
use Symfony\Flex\Unpack\Operation;
@@ -443,21 +441,28 @@ public function update(Event $event = null, $operations = [])
443441
$result = $unpacker->unpack($unpackOp);
444442
$unpacker->updateLock($result, $this->io);
445443

446-
if ($this->io instanceof ConsoleIO) {
447-
\Closure::bind(function () {
448-
$this->output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
449-
}, $this->io, $this->io)();
444+
$io = new NullIO();
445+
$composer = Factory::create($io, null, true);
446+
$installer = Installer::create($io, $composer);
447+
$installer
448+
->setDevMode($this->dryRun)
449+
->setDumpAutoloader(false)
450+
->setIgnorePlatformRequirements(true)
451+
->setUpdate(true)
452+
->setUpdateAllowList(['php'])
453+
;
454+
455+
if (method_exists($composer->getEventDispatcher(), 'setRunScripts')) {
456+
$composer->getEventDispatcher()->setRunScripts(false);
457+
} else {
458+
$installer->setRunScripts(false);
450459
}
451460

452-
\Closure::bind(function ($locker) {
453-
$this->locker = $locker;
454-
$this->dumpAutoloader = false;
455-
$this->runScripts = false;
456-
$this->ignorePlatformReqs = true;
457-
$this->update = false;
458-
}, $this->installer, $this->installer)($this->composer->getLocker());
461+
if (method_exists($installer, 'setSkipSuggest')) {
462+
$installer->setSkipSuggest(true);
463+
}
459464

460-
$this->installer->run();
465+
$installer->run();
461466
}
462467

463468
public function install(Event $event = null)

src/Unpacker.php

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(Composer $composer, PackageResolver $resolver, bool
4242
$this->versionParser = new VersionParser();
4343
}
4444

45-
public function unpack(Operation $op, Result $result = null, &$links = []): Result
45+
public function unpack(Operation $op, Result $result = null, &$links = [], bool $devRequire = false): Result
4646
{
4747
if (null === $result) {
4848
$result = new Result();
@@ -69,50 +69,67 @@ public function unpack(Operation $op, Result $result = null, &$links = []): Resu
6969
continue;
7070
}
7171

72-
$versionSelector = null;
72+
$requires = [];
7373
foreach ($pkg->getRequires() as $link) {
74-
if ('php' === $link->getTarget()) {
75-
continue;
74+
$requires[$link->getTarget()] = $link;
75+
}
76+
$devRequires = $pkg->getDevRequires();
77+
78+
foreach ($devRequires as $i => $link) {
79+
if (!isset($requires[$link->getTarget()])) {
80+
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()));
7681
}
82+
$devRequires[$i] = $requires[$link->getTarget()];
83+
unset($requires[$link->getTarget()]);
84+
}
7785

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

81-
if ($subPkg = $localRepo->findPackage($link->getTarget(), '*')) {
82-
if ('symfony-pack' === $subPkg->getType()) {
83-
$subOp = new Operation(true, $op->shouldSort());
84-
$subOp->addPackage($subPkg->getName(), $constraint, $package['dev']);
85-
$result = $this->unpack($subOp, $result, $links);
90+
foreach ($requires as $link) {
91+
if ('php' === $linkName = $link->getTarget()) {
8692
continue;
8793
}
8894

89-
if ('*' === $constraint) {
90-
if (null === $versionSelector) {
91-
$pool = class_exists(RepositorySet::class) ? RepositorySet::class : Pool::class;
92-
$pool = new $pool($this->composer->getPackage()->getMinimumStability(), $this->composer->getPackage()->getStabilityFlags());
93-
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
94-
$versionSelector = new VersionSelector($pool);
95+
$constraint = $link->getPrettyConstraint();
96+
$constraint = substr($this->resolver->parseVersion($linkName, $constraint, true), 1) ?: $constraint;
97+
98+
if ($subPkg = $localRepo->findPackage($linkName, '*')) {
99+
if ('symfony-pack' === $subPkg->getType()) {
100+
$subOp = new Operation(true, $op->shouldSort());
101+
$subOp->addPackage($subPkg->getName(), $constraint, $dev);
102+
$result = $this->unpack($subOp, $result, $links, $dev);
103+
continue;
95104
}
96105

97-
$constraint = $versionSelector->findRecommendedRequireVersion($subPkg);
106+
if ('*' === $constraint) {
107+
if (null === $versionSelector) {
108+
$pool = class_exists(RepositorySet::class) ? RepositorySet::class : Pool::class;
109+
$pool = new $pool($this->composer->getPackage()->getMinimumStability(), $this->composer->getPackage()->getStabilityFlags());
110+
$pool->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));
111+
$versionSelector = new VersionSelector($pool);
112+
}
113+
114+
$constraint = $versionSelector->findRecommendedRequireVersion($subPkg);
115+
}
98116
}
99-
}
100117

101-
$linkName = $link->getTarget();
102-
$linkType = $package['dev'] ? 'require-dev' : 'require';
103-
$constraint = $this->versionParser->parseConstraints($constraint);
118+
$linkType = $dev ? 'require-dev' : 'require';
119+
$constraint = $this->versionParser->parseConstraints($constraint);
104120

105-
if (isset($links[$linkName])) {
106-
$links[$linkName]['constraints'][] = $constraint;
107-
if ('require' === $linkType) {
108-
$links[$linkName]['type'] = 'require';
121+
if (isset($links[$linkName])) {
122+
$links[$linkName]['constraints'][] = $constraint;
123+
if ('require' === $linkType) {
124+
$links[$linkName]['type'] = 'require';
125+
}
126+
} else {
127+
$links[$linkName] = [
128+
'type' => $linkType,
129+
'name' => $linkName,
130+
'constraints' => [$constraint],
131+
];
109132
}
110-
} else {
111-
$links[$linkName] = [
112-
'type' => $linkType,
113-
'name' => $linkName,
114-
'constraints' => [$constraint],
115-
];
116133
}
117134
}
118135
}

0 commit comments

Comments
 (0)