Skip to content

Commit 2a32137

Browse files
Preserve position of require-dev when it's empty before running "require"
1 parent b298435 commit 2a32137

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Command/RequireCommand.php

+29-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Flex\Command;
1313

1414
use Composer\Command\RequireCommand as BaseRequireCommand;
15+
use Composer\Factory;
16+
use Composer\Json\JsonFile;
17+
use Composer\Json\JsonManipulator;
1518
use Composer\Plugin\PluginInterface;
1619
use Symfony\Component\Console\Input\InputInterface;
1720
use Symfony\Component\Console\Input\InputOption;
@@ -51,15 +54,35 @@ protected function execute(InputInterface $input, OutputInterface $output)
5154
$input->setOption('no-suggest', true);
5255
}
5356

54-
$ret = parent::execute($input, $output) ?? 0;
57+
$file = Factory::getComposerFile();
58+
$contents = file_get_contents($file);
59+
$json = JsonFile::parseJson($contents);
5560

56-
if (0 !== $ret || $input->getOption('no-unpack') || $input->getOption('no-update')) {
57-
return $ret;
61+
if (\array_key_exists('require-dev', $json) && !$json['require-dev'] && (new JsonManipulator($contents))->removeMainKey('require-dev')) {
62+
$manipulator = new JsonManipulator($contents);
63+
$manipulator->addLink('require-dev', 'php', '*');
64+
file_put_contents($file, $manipulator->getContents());
65+
} else {
66+
$file = null;
5867
}
68+
unset($contents, $json, $manipulator);
5969

60-
$unpackCommand = new UnpackCommand($this->resolver);
61-
$unpackCommand->setApplication($this->getApplication());
70+
try {
71+
$ret = parent::execute($input, $output) ?? 0;
6272

63-
return $unpackCommand->execute($input, $output);
73+
if (0 !== $ret || $input->getOption('no-unpack') || $input->getOption('no-update')) {
74+
return $ret;
75+
}
76+
77+
$unpackCommand = new UnpackCommand($this->resolver);
78+
$unpackCommand->setApplication($this->getApplication());
79+
80+
return $unpackCommand->execute($input, $output);
81+
} finally {
82+
if (null !== $file) {
83+
$manipulator = new JsonManipulator(file_get_contents($file));
84+
$manipulator->removeSubNode('require-dev', 'php');
85+
}
86+
}
6487
}
6588
}

src/Command/UnpackCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
8686
return 0;
8787
}
8888

89+
$io->writeError('<info>Unpacking Symfony packs</>');
8990
foreach ($result->getUnpacked() as $pkg) {
90-
$io->writeError(sprintf('<info>Unpacked %s dependencies</>', $pkg->getName()));
91+
$io->writeError(sprintf(' - Unpacked <info>%s</>', $pkg->getName()));
9192
}
9293

9394
$unpacker->updateLock($result, $io);

0 commit comments

Comments
 (0)