Skip to content

Commit

Permalink
blend value
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 2, 2024
1 parent a378102 commit 5b8ebbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/Command/BlendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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 = '/(?<!\\\\)\./'; // Regex pattern to match a dot not preceded by a backslash
$pointers = preg_split($pattern, $jsonPath);

Expand All @@ -112,16 +112,19 @@ private function blendJsonPath(InputInterface $input, OutputInterface $output):
$part = str_replace('\.', '.', $part);
}

$p = $pointers;
$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;
}
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);
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/BlendCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>}} */
$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)) {
Expand Down

0 comments on commit 5b8ebbb

Please sign in to comment.