diff --git a/src/Command/BlendCommand.php b/src/Command/BlendCommand.php index 153e3a8..37603d3 100644 --- a/src/Command/BlendCommand.php +++ b/src/Command/BlendCommand.php @@ -32,12 +32,13 @@ protected function configure(): void { $this->setName('blend') ->setDefinition([ - new InputOption('dev', 'D', InputOption::VALUE_NONE, 'Blend dev requirements.'), - new InputOption('all', 'A', InputOption::VALUE_NONE, 'Blend all (dev + non-dev) requirements.'), - new InputOption('self', 'S', InputOption::VALUE_NONE, 'Blend only the mono-repository projects requirements.'), + new InputOption('dev', 'D', InputOption::VALUE_NONE, 'Blend dev requirements'), + new InputOption('all', 'A', InputOption::VALUE_NONE, 'Blend all (dev + non-dev) requirements'), + new InputOption('self', 'S', InputOption::VALUE_NONE, 'Blend only the mono-repository projects requirements'), new InputOption('json-path', null, InputOption::VALUE_REQUIRED, 'Json path to blend'), - new InputOption('force', null, InputOption::VALUE_NONE, 'Force'), - new InputArgument('projects', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), + new InputOption('force', null, InputOption::VALUE_NONE, 'Force to write value even if it is not present yet'), + new InputOption('value', null, InputOption::VALUE_REQUIRED, 'Value to use'), + new InputArgument('projects', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Projects to generate the graph for'), ]) ->setDescription('Blend the mono-repository dependencies into each projects. We read all the dependencies of the root package and map them to either dev, non-dev or all.'); } @@ -99,7 +100,6 @@ private function blendJsonPath(InputInterface $input, OutputInterface $output): /** @var string */ $jsonPath = $input->getOption('json-path'); $path = $this->composer->getConfig()->getConfigSource()->getName(); - $data = $this->readJsonFile($path); $pattern = '/(?writeln(sprintf('Node "%s" not found.', $jsonPath)); - return 1; - } + if (!$value = $input->getOption('value')) { + $p = $pointers; + $data = $this->readJsonFile($path); + $value = $data; + while($pointer = array_shift($p)) { + /** @var string $pointer */ + if (!is_array($value) || !isset($value[$pointer])) { + $output->writeln(sprintf('Node "%s" not found.', $jsonPath)); + return 1; + } - $value = $value[$pointer]; + $value = $value[$pointer]; + } } $projects = $this->getProjects($input); diff --git a/tests/functional/BlendCommandTest.php b/tests/functional/BlendCommandTest.php index 414629f..acbad45 100644 --- a/tests/functional/BlendCommandTest.php +++ b/tests/functional/BlendCommandTest.php @@ -135,6 +135,26 @@ public function testBlendSelf(): void { $this->assertEquals($new['require-dev']['test/b'], '^1.0.0 || @dev'); } + public function testBlendJsonPathWithValue(): void { + $this->files = [ + __DIR__ . '/../monorepo/packages/A/composer.json', + __DIR__ . '/../monorepo/packages/B/composer.json', + __DIR__ . '/../monorepo/packages/C/composer.json', + __DIR__ . '/../monorepo/packages/D/composer.json' + ]; + $this->backups = array_map('file_get_contents', $this->files); + $output = new BufferedOutput; + $this->application->run(new StringInput('blend --json-path=extra.branch-alias.dev-3\\\.4 --value foo --force'), $output); + + foreach ($this->files as $f) { + $json = file_get_contents($f) ?: throw new RuntimeException; + /** @var array{extra?: array{branch-alias?: array}} */ + $new = json_decode($json, true); + $this->assertEquals($new['extra']['branch-alias']['dev-3.4'] ?? null, 'foo'); + } + $this->assertEquals("", $output->fetch()); + } + protected function tearDown(): void { while ($file = array_shift($this->files)) { if ($b = array_shift($this->backups)) {