Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow multiple lines addition with array for add-lines Configurator #1034

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Configurator/AddLinesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,14 @@ public function executeUnconfigure(Recipe $recipe, $config): void
}
}

private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing): string
private function getPatchedContents(string $file, string|array $value, string $position, ?string $target, bool $warnIfMissing): string
{
$fileContents = $this->readFile($file);

if (\is_array($value)) {
$value = implode("\n", $value);
}

if (false !== strpos($fileContents, $value)) {
return $fileContents; // already includes value, skip
}
Expand Down
64 changes: 64 additions & 0 deletions tests/Configurator/AddLinesConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,70 @@ public function getUpdateTests()
EOF
],
];

yield 'recipe_changes_with_multiline_string' => [
['assets/app.js' => $appJsOriginal],
[
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
],
[
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';\nimport { useIntersection } from 'stimulus-use';"],
],
['assets/app.js' => <<<EOF
import './stimulus_bootstrap';
import { useIntersection } from 'stimulus-use';
import * as Turbo from '@hotwired/turbo';

console.log(Turbo);
EOF,
],
];

yield 'recipe_changes_with_multiline_array' => [
['assets/app.js' => $appJsOriginal],
[
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
],
[
['file' => 'assets/app.js', 'position' => 'top', 'content' => [
"import './stimulus_bootstrap';",
"import { useIntersection } from 'stimulus-use';",
]],
],
['assets/app.js' => <<<EOF
import './stimulus_bootstrap';
import { useIntersection } from 'stimulus-use';
import * as Turbo from '@hotwired/turbo';

console.log(Turbo);
EOF,
],
];

yield 'recipe_changes_with_complex_multiline_array' => [
['assets/app.js' => $appJsOriginal],
[
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
],
[
['file' => 'assets/app.js', 'position' => 'top', 'content' => [
"import './stimulus_bootstrap';",
"import { useIntersection } from 'stimulus-use';",
"",
" console.log(years['2025'] !== years['0225']);",
]],
],
['assets/app.js' => <<<EOF
import './stimulus_bootstrap';
import { useIntersection } from 'stimulus-use';

console.log(years['2025'] !== years['0225']);
import * as Turbo from '@hotwired/turbo';

console.log(Turbo);
EOF,
],
];
}

private function runConfigure(array $config, ?Composer $composer = null)
Expand Down
Loading