From 636aed29bbdc865eb4cec427399fe8441a5a125f Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Oct 2024 12:00:00 +0000 Subject: [PATCH 1/6] Parse filenames from a .bcignore file --- src/IgnoreFile.php | 15 +++++++++++++++ tests/Kernel/IgnoreFileTest.php | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/IgnoreFile.php create mode 100644 tests/Kernel/IgnoreFileTest.php diff --git a/src/IgnoreFile.php b/src/IgnoreFile.php new file mode 100644 index 0000000..40c3822 --- /dev/null +++ b/src/IgnoreFile.php @@ -0,0 +1,15 @@ + Date: Thu, 31 Oct 2024 12:00:00 +0000 Subject: [PATCH 2/6] Return an empty array if there is no ignore file --- src/IgnoreFile.php | 4 ++++ tests/Kernel/IgnoreFileTest.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/IgnoreFile.php b/src/IgnoreFile.php index 40c3822..30ff2e6 100644 --- a/src/IgnoreFile.php +++ b/src/IgnoreFile.php @@ -10,6 +10,10 @@ final class IgnoreFile public static function parse(): array { + if (@stat(self::FILENAME) === false) { + return []; + } + return explode("\n", file_get_contents(self::FILENAME)); } } diff --git a/tests/Kernel/IgnoreFileTest.php b/tests/Kernel/IgnoreFileTest.php index 4dafefc..4a85329 100644 --- a/tests/Kernel/IgnoreFileTest.php +++ b/tests/Kernel/IgnoreFileTest.php @@ -18,4 +18,9 @@ class IgnoreFileTest extends KernelTestCase self::assertSame(['phpstan.neon.dist'], IgnoreFile::parse()); } + + public function test_it_returns_an_empty_array_of_filenames_if_there_is_no_ignore_file(): void + { + self::assertSame([], IgnoreFile::parse()); + } } From f0121f8bc6f466d942026bd156d30b0bb7516407 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Oct 2024 12:00:00 +0000 Subject: [PATCH 3/6] Remove files that are ignored before they are generated --- src/Command/RemoveIgnoredFilesCommand.php | 34 +++++++++++++++++++ .../Command/RemoveIgnoredFilesCommandTest.php | 25 ++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/Command/RemoveIgnoredFilesCommand.php create mode 100644 tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php diff --git a/src/Command/RemoveIgnoredFilesCommand.php b/src/Command/RemoveIgnoredFilesCommand.php new file mode 100644 index 0000000..08bb72e --- /dev/null +++ b/src/Command/RemoveIgnoredFilesCommand.php @@ -0,0 +1,34 @@ + $filesToGenerate + * @var ConfigDto $configurationDataDto, + * @var array $configurationData + */ + [$configurationData, $configurationDataDto, $filesToGenerate] = $filesToGenerateAndConfigurationData; + + $filesToGenerate = array_filter($filesToGenerate, function (TemplateFile $templateFile): bool { + return !collect($this->filenames)->contains($templateFile->name); + }); + + return $next([$configurationDataDto, $filesToGenerate]); + } +} diff --git a/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php b/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php new file mode 100644 index 0000000..00e8c01 --- /dev/null +++ b/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php @@ -0,0 +1,25 @@ +execute([[], [], $filenamesToGenerate], function ($result) { + self::assertCount(1, $result[1]); + self::assertSame('phpcs.xml.dist', $result[1][0]->name); + }); + } +} From 2b180e967c34269c37368d37befd355adfd1e7e7 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Oct 2024 12:00:00 +0000 Subject: [PATCH 4/6] Files to generate should be a Collection --- src/Command/RemoveIgnoredFilesCommand.php | 4 ++-- tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Command/RemoveIgnoredFilesCommand.php b/src/Command/RemoveIgnoredFilesCommand.php index 08bb72e..6c2eea6 100644 --- a/src/Command/RemoveIgnoredFilesCommand.php +++ b/src/Command/RemoveIgnoredFilesCommand.php @@ -19,13 +19,13 @@ final class RemoveIgnoredFilesCommand public function execute(array $filesToGenerateAndConfigurationData, \Closure $next) { /** - * @var array $filesToGenerate + * @var Collection $filesToGenerate * @var ConfigDto $configurationDataDto, * @var array $configurationData */ [$configurationData, $configurationDataDto, $filesToGenerate] = $filesToGenerateAndConfigurationData; - $filesToGenerate = array_filter($filesToGenerate, function (TemplateFile $templateFile): bool { + $filesToGenerate = $filesToGenerate->filter(function (TemplateFile $templateFile): bool { return !collect($this->filenames)->contains($templateFile->name); }); diff --git a/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php b/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php index 00e8c01..3a51f39 100644 --- a/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php +++ b/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php @@ -8,10 +8,10 @@ class RemoveIgnoredFilesCommandTest extends KernelTestCase { public function test_it_removes_any_ignored_files(): void { - $filenamesToGenerate = [ + $filenamesToGenerate = collect([ new TemplateFile(data: '', name: 'phpcs.xml.dist'), new TemplateFile(data: '', name: 'phpstan.neon.dist'), - ]; + ]); $filenamesToIgnore = ['phpstan.neon.dist']; From 05f7c2b60942ca0cff79f6dddc79324a884e453c Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Oct 2024 12:00:00 +0000 Subject: [PATCH 5/6] Pass the correct values to the next command in the ...chain --- src/Command/RemoveIgnoredFilesCommand.php | 2 +- tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Command/RemoveIgnoredFilesCommand.php b/src/Command/RemoveIgnoredFilesCommand.php index 6c2eea6..07728db 100644 --- a/src/Command/RemoveIgnoredFilesCommand.php +++ b/src/Command/RemoveIgnoredFilesCommand.php @@ -29,6 +29,6 @@ final class RemoveIgnoredFilesCommand return !collect($this->filenames)->contains($templateFile->name); }); - return $next([$configurationDataDto, $filesToGenerate]); + return $next([$configurationData, $configurationDataDto, $filesToGenerate]); } } diff --git a/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php b/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php index 3a51f39..edd4a74 100644 --- a/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php +++ b/tests/Kernel/Command/RemoveIgnoredFilesCommandTest.php @@ -18,8 +18,8 @@ class RemoveIgnoredFilesCommandTest extends KernelTestCase $command = new RemoveIgnoredFilesCommand($filenamesToIgnore); $command->execute([[], [], $filenamesToGenerate], function ($result) { - self::assertCount(1, $result[1]); - self::assertSame('phpcs.xml.dist', $result[1][0]->name); + self::assertCount(1, $result[2]); + self::assertSame('phpcs.xml.dist', $result[2][0]->name); }); } } From 2a6718c650ce7f20943a400d424e7d2442a03523 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 31 Oct 2024 12:00:00 +0000 Subject: [PATCH 6/6] Add the new command to the chain --- src/Console/Command/GenerateCommand.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index 5d4dc3f..884405a 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -8,9 +8,11 @@ use App\Command\CreateFinalConfigurationDataCommand; use App\Command\CreateListOfFilesToGenerateCommand; use App\Command\FindBuildConfigurationFileCommand; use App\Command\GenerateConfigurationFilesCommand; +use App\Command\RemoveIgnoredFilesCommand; use App\Command\ValidateConfigurationDataCommand; use App\DataTransferObject\ConfigDto; use App\DataTransferObject\TemplateFile; +use App\IgnoreFile; use Illuminate\Pipeline\Pipeline; use Illuminate\Support\Collection; use Symfony\Component\Console\Attribute\AsCommand; @@ -72,6 +74,7 @@ class GenerateCommand extends Command new CreateFinalConfigurationDataCommand(), new ValidateConfigurationDataCommand(), new CreateListOfFilesToGenerateCommand(), + new RemoveIgnoredFilesCommand(IgnoreFile::parse()), new GenerateConfigurationFilesCommand( $this->filesystem, $this->twig,