Skip to content

Commit 6b46a00

Browse files
committed
feature #990 [Feature] Add options expansion to Add Lines Configuration (mnocon)
This PR was squashed before being merged into the 1.x branch. Discussion ---------- [Feature] Add options expansion to Add Lines Configuration Hi! I've got a chance to play with the new AddLines configurator and it's an extremely useful feature, thanks for introducing it! There is one small thing that seems missing to me: support for placeholders that are available in the `copy-from-package` and `copy-from-recipe` configurators. It's this feature: ``` These are the available placeholders: %BIN_DIR%, %CONF_DIR%, %CONFIG_DIR%, %SRC_DIR% %VAR_DIR% and %PUBLIC_DIR%. Recipes must use these placeholders instead of hardcoding the paths to be truly reusable. The placeholder values can be overridden in the extra section of your composer.json file (where you can define your own placeholders too): ``` (this text comes from https://github.com/symfony/recipes) This small PR makes it possible to use placeholders in the add-lines configurator. I'm not sure about the target branch, please let me know if I should rebase to 1.x - also not sure if I should mention it in the doc somewhere. Commits ------- 644e426 [Feature] Add options expansion to Add Lines Configuration
2 parents 7b40eec + 644e426 commit 6b46a00

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Diff for: src/Configurator/AddLinesConfigurator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function executeConfigure(Recipe $recipe, $config): void
9797
}
9898
$content = $patch['content'];
9999

100-
$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
100+
$file = $this->path->concatenate([$this->options->get('root-dir'), $this->options->expandTargetDir($patch['file'])]);
101101
$warnIfMissing = isset($patch['warn_if_missing']) && $patch['warn_if_missing'];
102102
if (!is_file($file)) {
103103
$this->write([
@@ -147,7 +147,7 @@ public function executeUnconfigure(Recipe $recipe, $config): void
147147
// Ignore "requires": the target packages may have just become uninstalled.
148148
// Checking for a "content" match is enough.
149149

150-
$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
150+
$file = $this->path->concatenate([$this->options->get('root-dir'), $this->options->expandTargetDir($patch['file'])]);
151151
if (!is_file($file)) {
152152
continue;
153153
}

Diff for: tests/Configurator/AddLinesConfiguratorTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public function testLinesAddedToTopOfFile()
6767
$actualContents);
6868
}
6969

70+
public function testExpandTargetDirWhenConfiguring()
71+
{
72+
$this->saveFile('config/file.txt', 'FirstLine');
73+
74+
$this->runConfigure([
75+
[
76+
'file' => '%CONFIG_DIR%/file.txt',
77+
'position' => 'top',
78+
'content' => 'NewFirstLine',
79+
],
80+
]);
81+
$actualContents = $this->readFile('config/file.txt');
82+
$this->assertSame(<<<EOF
83+
NewFirstLine
84+
FirstLine
85+
EOF
86+
,
87+
$actualContents);
88+
}
89+
7090
public function testLinesAddedToBottomOfFile()
7191
{
7292
$this->saveFile('assets/app.js', <<<EOF
@@ -318,6 +338,28 @@ public function testUnconfigure(string $originalContents, string $value, string
318338
$this->assertSame($expectedContents, $actualContents);
319339
}
320340

341+
public function testExpandTargetDirWhenUnconfiguring()
342+
{
343+
$this->saveFile('config/file.txt',
344+
<<<EOF
345+
Line1
346+
Line2
347+
EOF
348+
);
349+
350+
$this->runUnconfigure([
351+
[
352+
'file' => '%CONFIG_DIR%/file.txt',
353+
'content' => 'Line1',
354+
],
355+
]);
356+
$actualContents = $this->readFile('config/file.txt');
357+
$this->assertSame(<<<EOF
358+
Line2
359+
EOF
360+
, $actualContents);
361+
}
362+
321363
public function getUnconfigureTests()
322364
{
323365
yield 'found_middle' => [

0 commit comments

Comments
 (0)