Skip to content

Commit 2eec7f6

Browse files
committed
Add create_if_missing option to add-lines configurator
This option allows specifying YAML content that should be created when the target is not found in the file. The content is appended to the file followed by the regular content. Example usage: { "add-lines": [{ "file": "config/packages/ai.yaml", "position": "after_target", "target": " store:", "create_if_missing": "ai:\n store:", "content": " azuresearch:\n ..." }] }
1 parent 9cd3847 commit 2eec7f6

5 files changed

Lines changed: 77 additions & 2 deletions

File tree

src/Configurator/AddLinesConfigurator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ public function executeConfigure(Recipe $recipe, $config): void
129129
continue;
130130
}
131131
$target = isset($patch['target']) ? $patch['target'] : null;
132+
$createIfMissing = isset($patch['create_if_missing']) ? $patch['create_if_missing'] : null;
132133

133-
$newContents = $this->getPatchedContents($file, $content, $position, $target, $warnIfMissing);
134+
$newContents = $this->getPatchedContents($file, $content, $position, $target, $warnIfMissing, $createIfMissing);
134135
$this->fileContents[$file] = $newContents;
135136
}
136137
}
@@ -164,7 +165,7 @@ public function executeUnconfigure(Recipe $recipe, $config): void
164165
}
165166
}
166167

167-
private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing): string
168+
private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing, ?string $createIfMissing = null): string
168169
{
169170
$fileContents = $this->readFile($file);
170171

@@ -192,6 +193,15 @@ private function getPatchedContents(string $file, string $value, string $positio
192193
break;
193194
}
194195
}
196+
197+
if (!$targetFound && null !== $createIfMissing) {
198+
// Insert the create_if_missing content at the end of the file, then add the value after the target
199+
$createIfMissingLines = explode("\n", $createIfMissing);
200+
$lines = array_merge($lines, $createIfMissingLines);
201+
$lines[] = $value;
202+
$targetFound = true;
203+
}
204+
195205
$fileContents = implode("\n", $lines);
196206

197207
if (!$targetFound) {

tests/Configurator/AddLinesConfiguratorTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,46 @@ public function testSkippedIfTargetCannotBeFound()
198198
$this->assertSame($originalContent, $this->readFile('webpack.config.js'));
199199
}
200200

201+
public function testCreateIfMissingWithExistingTarget()
202+
{
203+
$this->copyFixture('ai_with_store.yaml', 'config/packages/ai.yaml');
204+
205+
$this->runConfigure([
206+
[
207+
'file' => 'config/packages/ai.yaml',
208+
'position' => 'after_target',
209+
'target' => ' store:',
210+
'create_if_missing' => "ai:\n store:",
211+
'content' => " azuresearch:\n default:\n endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'",
212+
],
213+
]);
214+
215+
$this->assertSame(
216+
$this->loadFixture('ai_with_store_expected.yaml'),
217+
$this->readFile('config/packages/ai.yaml')
218+
);
219+
}
220+
221+
public function testCreateIfMissingNotUsedWhenTargetNotSet()
222+
{
223+
$this->copyFixture('no_ai_key.yaml', 'config/packages/ai.yaml');
224+
225+
$this->runConfigure([
226+
[
227+
'file' => 'config/packages/ai.yaml',
228+
'position' => 'after_target',
229+
'target' => ' store:',
230+
'content' => " azuresearch:\n default:\n endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'",
231+
],
232+
]);
233+
234+
// Content should remain unchanged since target was not found and no create_if_missing was provided
235+
$this->assertSame(
236+
$this->loadFixture('no_ai_key.yaml'),
237+
$this->readFile('config/packages/ai.yaml')
238+
);
239+
}
240+
201241
public function testPatchIgnoredIfValueAlreadyExists()
202242
{
203243
$originalContents = <<<JS
@@ -677,6 +717,16 @@ private function readFile(string $filename): string
677717
return file_get_contents(FLEX_TEST_DIR.'/'.$filename);
678718
}
679719

720+
private function loadFixture(string $filename): string
721+
{
722+
return rtrim(file_get_contents(__DIR__.'/Fixtures/AddLines/'.$filename), "\n");
723+
}
724+
725+
private function copyFixture(string $fixtureName, string $targetPath): void
726+
{
727+
$this->saveFile($targetPath, $this->loadFixture($fixtureName));
728+
}
729+
680730
private function createComposerMockWithPackagesInstalled(array $packages)
681731
{
682732
$packages = array_map(fn ($package) => explode(':', $package), $packages);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ai:
2+
store:
3+
pinecone:
4+
default:
5+
api_key: 'xxx'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ai:
2+
store:
3+
azuresearch:
4+
default:
5+
endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'
6+
pinecone:
7+
default:
8+
api_key: 'xxx'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
framework:
2+
secret: '%env(APP_SECRET)%'

0 commit comments

Comments
 (0)