Skip to content

Commit 9418170

Browse files
feature #655 Auto-unpack on create-project (nicolas-grekas)
This PR was merged into the 1.8-dev branch. Discussion ---------- Auto-unpack on create-project This PR ensures that packs are auto-unpacked when they are found in the `flex-require` or `flex-require-dev` sections, which happens when running e.g. `composer create-project symfony/(website-)skeleton`. On the path to #645 Commits ------- 998d18f Auto-unpack on create-project
2 parents 894c994 + 998d18f commit 9418170

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

src/Command/UnpackCommand.php

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@
1212
namespace Symfony\Flex\Command;
1313

1414
use Composer\Command\BaseCommand;
15-
use Composer\Config\JsonConfigSource;
16-
use Composer\Factory;
1715
use Composer\Installer;
18-
use Composer\Json\JsonFile;
19-
use Composer\Package\Locker;
2016
use Composer\Package\Version\VersionParser;
21-
use Composer\Plugin\PluginInterface;
2217
use Symfony\Component\Console\Input\InputArgument;
2318
use Symfony\Component\Console\Input\InputInterface;
2419
use Symfony\Component\Console\Input\InputOption;
@@ -55,10 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5550
$composer = $this->getComposer();
5651
$packages = $this->resolver->resolve($input->getArgument('packages'), true);
5752
$io = $this->getIO();
58-
$json = new JsonFile(Factory::getComposerFile());
59-
$manipulator = new JsonConfigSource($json);
60-
$locker = $composer->getLocker();
61-
$lockData = $locker->getLockData();
53+
$lockData = $composer->getLocker()->getLockData();
6254
$installedRepo = $composer->getRepositoryManager()->getLocalRepository();
6355
$versionParser = new VersionParser();
6456
$dryRun = $input->hasOption('dry-run') && $input->getOption('dry-run');
@@ -97,40 +89,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
9789
$io->writeError(sprintf('<info>Unpacked %s dependencies</>', $pkg->getName()));
9890
}
9991

100-
foreach ($result->getUnpacked() as $package) {
101-
$manipulator->removeLink('require-dev', $package->getName());
102-
foreach ($lockData['packages-dev'] as $i => $pkg) {
103-
if ($package->getName() === $pkg['name']) {
104-
unset($lockData['packages-dev'][$i]);
105-
}
106-
}
107-
$manipulator->removeLink('require', $package->getName());
108-
foreach ($lockData['packages'] as $i => $pkg) {
109-
if ($package->getName() === $pkg['name']) {
110-
unset($lockData['packages'][$i]);
111-
}
112-
}
113-
}
114-
$lockData['packages'] = array_values($lockData['packages']);
115-
$lockData['packages-dev'] = array_values($lockData['packages-dev']);
116-
$lockData['content-hash'] = $locker->getContentHash(file_get_contents($json->getPath()));
117-
$lockFile = new JsonFile(substr($json->getPath(), 0, -4).'lock', null, $io);
118-
119-
if (!$dryRun) {
120-
$lockFile->write($lockData);
121-
}
92+
$unpacker->updateLock($result, $io);
12293

12394
if ($input->hasOption('no-install') && $input->getOption('no-install')) {
12495
return 0;
12596
}
12697

127-
// force removal of files under vendor/
128-
if (version_compare('2.0.0', PluginInterface::PLUGIN_API_VERSION, '>')) {
129-
$locker = new Locker($io, $lockFile, $composer->getRepositoryManager(), $composer->getInstallationManager(), file_get_contents($json->getPath()));
130-
} else {
131-
$locker = new Locker($io, $lockFile, $composer->getInstallationManager(), file_get_contents($json->getPath()));
132-
}
133-
$composer->setLocker($locker);
13498
$install = Installer::create($io, $composer);
13599
$install
136100
->setDryRun($dryRun)

src/Flex.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Composer\Script\ScriptEvents;
4848
use Symfony\Component\Console\Input\ArgvInput;
4949
use Symfony\Flex\Event\UpdateEvent;
50+
use Symfony\Flex\Unpack\Operation;
5051
use Symfony\Thanks\Thanks;
5152

5253
/**
@@ -389,10 +390,12 @@ public function update(Event $event = null, $operations = [])
389390
}
390391

391392
$sortPackages = $this->composer->getConfig()->get('sort-packages');
393+
$unpackOp = new Operation(true, $sortPackages);
392394

393395
foreach (['require', 'require-dev'] as $type) {
394396
if (isset($json['flex-'.$type])) {
395397
foreach ($json['flex-'.$type] as $package => $constraint) {
398+
$unpackOp->addPackage($package, $constraint, 'require-dev' === $type);
396399
$manipulator->addLink($type, $package, $constraint, $sortPackages);
397400
}
398401

@@ -412,6 +415,10 @@ public function update(Event $event = null, $operations = [])
412415
}, $this->installer, $this->installer)();
413416
$this->composer->getEventDispatcher()->__construct($this->composer, $this->io);
414417

418+
$unpacker = new Unpacker($this->composer, new PackageResolver($this->downloader), $this->dryRun);
419+
$result = $unpacker->unpack($unpackOp);
420+
$unpacker->updateLock($result, $this->io);
421+
415422
$status = $this->installer->run();
416423
if (0 !== $status) {
417424
exit($status);

src/Unpacker.php

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

1414
use Composer\Composer;
15+
use Composer\Config\JsonConfigSource;
1516
use Composer\DependencyResolver\Pool;
1617
use Composer\Factory;
18+
use Composer\IO\IOInterface;
19+
use Composer\Json\JsonFile;
1720
use Composer\Json\JsonManipulator;
21+
use Composer\Package\Locker;
1822
use Composer\Package\Version\VersionSelector;
23+
use Composer\Plugin\PluginInterface;
1924
use Composer\Repository\CompositeRepository;
2025
use Composer\Repository\RepositorySet;
2126
use Symfony\Flex\Unpack\Operation;
@@ -106,4 +111,43 @@ public function unpack(Operation $op, Result $result = null): Result
106111

107112
return $result;
108113
}
114+
115+
public function updateLock(Result $result, IOInterface $io): void
116+
{
117+
$json = new JsonFile(Factory::getComposerFile());
118+
$manipulator = new JsonConfigSource($json);
119+
$locker = $this->composer->getLocker();
120+
$lockData = $locker->getLockData();
121+
122+
foreach ($result->getUnpacked() as $package) {
123+
$manipulator->removeLink('require-dev', $package->getName());
124+
foreach ($lockData['packages-dev'] as $i => $pkg) {
125+
if ($package->getName() === $pkg['name']) {
126+
unset($lockData['packages-dev'][$i]);
127+
}
128+
}
129+
$manipulator->removeLink('require', $package->getName());
130+
foreach ($lockData['packages'] as $i => $pkg) {
131+
if ($package->getName() === $pkg['name']) {
132+
unset($lockData['packages'][$i]);
133+
}
134+
}
135+
}
136+
$lockData['packages'] = array_values($lockData['packages']);
137+
$lockData['packages-dev'] = array_values($lockData['packages-dev']);
138+
$lockData['content-hash'] = $locker->getContentHash(file_get_contents($json->getPath()));
139+
$lockFile = new JsonFile(substr($json->getPath(), 0, -4).'lock', null, $io);
140+
141+
if (!$this->dryRun) {
142+
$lockFile->write($lockData);
143+
}
144+
145+
// force removal of files under vendor/
146+
if (version_compare('2.0.0', PluginInterface::PLUGIN_API_VERSION, '>')) {
147+
$locker = new Locker($io, $lockFile, $this->composer->getRepositoryManager(), $this->composer->getInstallationManager(), file_get_contents($json->getPath()));
148+
} else {
149+
$locker = new Locker($io, $lockFile, $this->composer->getInstallationManager(), file_get_contents($json->getPath()));
150+
}
151+
$this->composer->setLocker($locker);
152+
}
109153
}

0 commit comments

Comments
 (0)