Skip to content

Commit

Permalink
feature #990 [Feature] Add options expansion to Add Lines Configurati…
Browse files Browse the repository at this point in the history
…on (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
  • Loading branch information
fabpot committed Feb 5, 2024
2 parents 7b40eec + 644e426 commit 6b46a00
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Configurator/AddLinesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function executeConfigure(Recipe $recipe, $config): void
}
$content = $patch['content'];

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

$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
$file = $this->path->concatenate([$this->options->get('root-dir'), $this->options->expandTargetDir($patch['file'])]);
if (!is_file($file)) {
continue;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Configurator/AddLinesConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public function testLinesAddedToTopOfFile()
$actualContents);
}

public function testExpandTargetDirWhenConfiguring()
{
$this->saveFile('config/file.txt', 'FirstLine');

$this->runConfigure([
[
'file' => '%CONFIG_DIR%/file.txt',
'position' => 'top',
'content' => 'NewFirstLine',
],
]);
$actualContents = $this->readFile('config/file.txt');
$this->assertSame(<<<EOF
NewFirstLine
FirstLine
EOF
,
$actualContents);
}

public function testLinesAddedToBottomOfFile()
{
$this->saveFile('assets/app.js', <<<EOF
Expand Down Expand Up @@ -318,6 +338,28 @@ public function testUnconfigure(string $originalContents, string $value, string
$this->assertSame($expectedContents, $actualContents);
}

public function testExpandTargetDirWhenUnconfiguring()
{
$this->saveFile('config/file.txt',
<<<EOF
Line1
Line2
EOF
);

$this->runUnconfigure([
[
'file' => '%CONFIG_DIR%/file.txt',
'content' => 'Line1',
],
]);
$actualContents = $this->readFile('config/file.txt');
$this->assertSame(<<<EOF
Line2
EOF
, $actualContents);
}

public function getUnconfigureTests()
{
yield 'found_middle' => [
Expand Down

0 comments on commit 6b46a00

Please sign in to comment.