Skip to content

Commit

Permalink
Auto-unpack on create-project
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jul 14, 2020
1 parent 894c994 commit 998d18f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
40 changes: 2 additions & 38 deletions src/Command/UnpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@
namespace Symfony\Flex\Command;

use Composer\Command\BaseCommand;
use Composer\Config\JsonConfigSource;
use Composer\Factory;
use Composer\Installer;
use Composer\Json\JsonFile;
use Composer\Package\Locker;
use Composer\Package\Version\VersionParser;
use Composer\Plugin\PluginInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -55,10 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$composer = $this->getComposer();
$packages = $this->resolver->resolve($input->getArgument('packages'), true);
$io = $this->getIO();
$json = new JsonFile(Factory::getComposerFile());
$manipulator = new JsonConfigSource($json);
$locker = $composer->getLocker();
$lockData = $locker->getLockData();
$lockData = $composer->getLocker()->getLockData();
$installedRepo = $composer->getRepositoryManager()->getLocalRepository();
$versionParser = new VersionParser();
$dryRun = $input->hasOption('dry-run') && $input->getOption('dry-run');
Expand Down Expand Up @@ -97,40 +89,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->writeError(sprintf('<info>Unpacked %s dependencies</>', $pkg->getName()));
}

foreach ($result->getUnpacked() as $package) {
$manipulator->removeLink('require-dev', $package->getName());
foreach ($lockData['packages-dev'] as $i => $pkg) {
if ($package->getName() === $pkg['name']) {
unset($lockData['packages-dev'][$i]);
}
}
$manipulator->removeLink('require', $package->getName());
foreach ($lockData['packages'] as $i => $pkg) {
if ($package->getName() === $pkg['name']) {
unset($lockData['packages'][$i]);
}
}
}
$lockData['packages'] = array_values($lockData['packages']);
$lockData['packages-dev'] = array_values($lockData['packages-dev']);
$lockData['content-hash'] = $locker->getContentHash(file_get_contents($json->getPath()));
$lockFile = new JsonFile(substr($json->getPath(), 0, -4).'lock', null, $io);

if (!$dryRun) {
$lockFile->write($lockData);
}
$unpacker->updateLock($result, $io);

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

// force removal of files under vendor/
if (version_compare('2.0.0', PluginInterface::PLUGIN_API_VERSION, '>')) {
$locker = new Locker($io, $lockFile, $composer->getRepositoryManager(), $composer->getInstallationManager(), file_get_contents($json->getPath()));
} else {
$locker = new Locker($io, $lockFile, $composer->getInstallationManager(), file_get_contents($json->getPath()));
}
$composer->setLocker($locker);
$install = Installer::create($io, $composer);
$install
->setDryRun($dryRun)
Expand Down
7 changes: 7 additions & 0 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Composer\Script\ScriptEvents;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Flex\Event\UpdateEvent;
use Symfony\Flex\Unpack\Operation;
use Symfony\Thanks\Thanks;

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

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

foreach (['require', 'require-dev'] as $type) {
if (isset($json['flex-'.$type])) {
foreach ($json['flex-'.$type] as $package => $constraint) {
$unpackOp->addPackage($package, $constraint, 'require-dev' === $type);
$manipulator->addLink($type, $package, $constraint, $sortPackages);
}

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

$unpacker = new Unpacker($this->composer, new PackageResolver($this->downloader), $this->dryRun);
$result = $unpacker->unpack($unpackOp);
$unpacker->updateLock($result, $this->io);

$status = $this->installer->run();
if (0 !== $status) {
exit($status);
Expand Down
44 changes: 44 additions & 0 deletions src/Unpacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
namespace Symfony\Flex;

use Composer\Composer;
use Composer\Config\JsonConfigSource;
use Composer\DependencyResolver\Pool;
use Composer\Factory;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Package\Locker;
use Composer\Package\Version\VersionSelector;
use Composer\Plugin\PluginInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\RepositorySet;
use Symfony\Flex\Unpack\Operation;
Expand Down Expand Up @@ -106,4 +111,43 @@ public function unpack(Operation $op, Result $result = null): Result

return $result;
}

public function updateLock(Result $result, IOInterface $io): void
{
$json = new JsonFile(Factory::getComposerFile());
$manipulator = new JsonConfigSource($json);
$locker = $this->composer->getLocker();
$lockData = $locker->getLockData();

foreach ($result->getUnpacked() as $package) {
$manipulator->removeLink('require-dev', $package->getName());
foreach ($lockData['packages-dev'] as $i => $pkg) {
if ($package->getName() === $pkg['name']) {
unset($lockData['packages-dev'][$i]);
}
}
$manipulator->removeLink('require', $package->getName());
foreach ($lockData['packages'] as $i => $pkg) {
if ($package->getName() === $pkg['name']) {
unset($lockData['packages'][$i]);
}
}
}
$lockData['packages'] = array_values($lockData['packages']);
$lockData['packages-dev'] = array_values($lockData['packages-dev']);
$lockData['content-hash'] = $locker->getContentHash(file_get_contents($json->getPath()));
$lockFile = new JsonFile(substr($json->getPath(), 0, -4).'lock', null, $io);

if (!$this->dryRun) {
$lockFile->write($lockData);
}

// force removal of files under vendor/
if (version_compare('2.0.0', PluginInterface::PLUGIN_API_VERSION, '>')) {
$locker = new Locker($io, $lockFile, $this->composer->getRepositoryManager(), $this->composer->getInstallationManager(), file_get_contents($json->getPath()));
} else {
$locker = new Locker($io, $lockFile, $this->composer->getInstallationManager(), file_get_contents($json->getPath()));
}
$this->composer->setLocker($locker);
}
}

0 comments on commit 998d18f

Please sign in to comment.