|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PMU project. |
| 5 | + * |
| 6 | + * (c) Antoine Bluchet <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Pmu\Command; |
| 15 | + |
| 16 | +use Composer\Command\BaseCommand; |
| 17 | +use Composer\Composer; |
| 18 | +use Composer\Console\Input\InputArgument; |
| 19 | +use Composer\Console\Input\InputOption; |
| 20 | +use Composer\Json\JsonFile; |
| 21 | +use Composer\Package\PackageInterface; |
| 22 | +use Pmu\Config; |
| 23 | +use Symfony\Component\Console\Input\InputInterface; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | + |
| 26 | +final class BlendCommand extends BaseCommand |
| 27 | +{ |
| 28 | + private Composer $composer; |
| 29 | + private Config $config; |
| 30 | + |
| 31 | + protected function configure(): void |
| 32 | + { |
| 33 | + $this->setName('blend') |
| 34 | + ->setDefinition([ |
| 35 | + new InputOption('dev', 'D', InputOption::VALUE_NONE, 'Blend dev requirements.'), |
| 36 | + new InputOption('json-path', null, InputOption::VALUE_REQUIRED, 'Json path to blend'), |
| 37 | + new InputOption('force', null, InputOption::VALUE_NONE, 'Force'), |
| 38 | + new InputArgument('projects', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), |
| 39 | + ]) |
| 40 | + ->setDescription('Blend the mono-repository dependencies into each projects.'); |
| 41 | + } |
| 42 | + |
| 43 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 44 | + { |
| 45 | + $this->composer = $this->requireComposer(); |
| 46 | + $this->config = Config::create($this->composer); |
| 47 | + $requireKey = true === $input->getOption('dev') ? 'require-dev' : 'require'; |
| 48 | + $packageAccessor = true === $input->getOption('dev') ? 'getDevRequires' : 'getRequires'; |
| 49 | + |
| 50 | + if (is_string($input->getOption('json-path'))) { |
| 51 | + return $this->blendJsonPath($input, $output); |
| 52 | + } |
| 53 | + |
| 54 | + $projects = $this->getProjects($input); |
| 55 | + $repo = $this->composer->getRepositoryManager(); |
| 56 | + $package = $this->composer->getPackage(); |
| 57 | + foreach ($this->config->projects as $p) { |
| 58 | + if ($projects && !in_array($p, $projects, true)) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $projectPackage = $repo->findPackage($p, '*'); |
| 63 | + if (!$projectPackage || !$projectPackage instanceof PackageInterface) { |
| 64 | + $output->writeln(sprintf('Package "%s" could not be found.', $p)); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + $dir = $projectPackage->getDistUrl(); |
| 69 | + if (!is_string($dir) || !is_dir($dir)) { |
| 70 | + $output->writeln(sprintf('Package "%s" could not be found at path "%s".', $p, $dir)); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $packagesToUpdate = []; |
| 75 | + foreach ($package->{$packageAccessor}() as $g) { |
| 76 | + // Only update the package if it's found in the project's package |
| 77 | + if (!$input->getOption('force')) { |
| 78 | + $hasPackage = false; |
| 79 | + foreach ($projectPackage->{$packageAccessor}() as $r) { |
| 80 | + if ($g->getTarget() === $r->getTarget()) { |
| 81 | + $hasPackage = true; |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!$hasPackage) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + $packagesToUpdate[$g->getTarget()] = $g->getPrettyConstraint(); |
| 92 | + } |
| 93 | + |
| 94 | + if (!$packagesToUpdate) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $json = new JsonFile($dir . '/composer.json'); |
| 99 | + /** @var array{require: array<string, string>, 'require-dev': array<string, string>} */ |
| 100 | + $composerDefinition = $json->read(); |
| 101 | + foreach ($packagesToUpdate as $target => $constraint) { |
| 102 | + $composerDefinition[$requireKey][$target] = $constraint; |
| 103 | + } |
| 104 | + |
| 105 | + $json->write($composerDefinition); |
| 106 | + } |
| 107 | + |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + private function blendJsonPath(InputInterface $input, OutputInterface $output): int { |
| 112 | + /** @var string */ |
| 113 | + $jsonPath = $input->getOption('json-path'); |
| 114 | + $path = $this->composer->getConfig()->getConfigSource()->getName(); |
| 115 | + $file = file_get_contents($path) ?: throw new \RuntimeException(sprintf('File "%s" not found.', $path)); |
| 116 | + $data = json_decode($file, true) ?: throw new \RuntimeException(sprintf('File "%s" is not JSON.', $path)); |
| 117 | + $pointers = explode('.', $jsonPath); |
| 118 | + |
| 119 | + $p = $pointers; |
| 120 | + $value = $data; |
| 121 | + while($pointer = array_shift($p)) { |
| 122 | + /** @var string $pointer */ |
| 123 | + if (!is_array($value) || !isset($value[$pointer])) { |
| 124 | + $output->writeln(sprintf('Node "%s" not found.', $jsonPath)); |
| 125 | + return 1; |
| 126 | + } |
| 127 | + |
| 128 | + $value = $value[$pointer]; |
| 129 | + } |
| 130 | + |
| 131 | + $repo = $this->composer->getRepositoryManager(); |
| 132 | + $projects = $this->getProjects($input); |
| 133 | + foreach ($this->config->projects as $project) { |
| 134 | + if ($projects && !in_array($project, $projects, true)) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + $package = $repo->findPackage($project, '*'); |
| 139 | + if (!$package || !$package instanceof PackageInterface) { |
| 140 | + $output->writeln(sprintf('Package "%s" could not be found.', $project)); |
| 141 | + return 1; |
| 142 | + } |
| 143 | + |
| 144 | + $dir = $package->getDistUrl(); |
| 145 | + if (!is_string($dir) || !is_dir($dir)) { |
| 146 | + $output->writeln(sprintf('Package "%s" could not be found at path "%s".', $project, $dir)); |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + $path = $dir . '/composer.json'; |
| 151 | + $fileContent = file_get_contents($path) ?: throw new \RuntimeException(sprintf('File "%s" not found.', $path)); |
| 152 | + $json = json_decode($fileContent, true) ?: throw new \RuntimeException(sprintf('File "%s" is not JSON.', $path)); |
| 153 | + $force = $input->getOption('force'); |
| 154 | + |
| 155 | + $p = $pointers; |
| 156 | + $ref =& $json; |
| 157 | + while($pointer = array_shift($p)) { |
| 158 | + if (!is_array($ref)) { |
| 159 | + $output->writeln(sprintf('Package "%s" has no pointer "%s".', $project, $pointer)); |
| 160 | + break 2; |
| 161 | + } |
| 162 | + |
| 163 | + if (!isset($ref[$pointer])) { |
| 164 | + if (!$force) { |
| 165 | + $output->writeln(sprintf('Package "%s" has no pointer "%s".', $project, $pointer)); |
| 166 | + break 2; |
| 167 | + } |
| 168 | + |
| 169 | + $ref[$pointer] = []; |
| 170 | + } |
| 171 | + |
| 172 | + if (\count($p) === 0) { |
| 173 | + $ref[$pointer] = $value; |
| 174 | + break; |
| 175 | + } |
| 176 | + |
| 177 | + $ref = &$ref[$pointer]; |
| 178 | + } |
| 179 | + |
| 180 | + unset($ref); |
| 181 | + $ref = $value; |
| 182 | + $fileContent = file_put_contents($path, json_encode($json, JSON_PRETTY_PRINT)); |
| 183 | + |
| 184 | + if (!$fileContent) { |
| 185 | + $output->writeln(sprintf('Could not write JSON at path "%s".', $path)); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return 0; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @return string[]|null |
| 194 | + */ |
| 195 | + private function getProjects(InputInterface $input): ?array { |
| 196 | + if (is_array($p = $input->getArgument('projects')) && $p) { |
| 197 | + return array_map('strval', $p); |
| 198 | + } |
| 199 | + |
| 200 | + return null; |
| 201 | + } |
| 202 | +} |
0 commit comments