diff --git a/Makefile b/Makefile index 58bcc48..dcfca6e 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,7 @@ include $(_MAKEFILE_DIR)makefiles/PHPCodeSniffer.mk include $(_MAKEFILE_DIR)makefiles/PHPUnit.mk include $(_MAKEFILE_DIR)makefiles/Psalm.mk include $(_MAKEFILE_DIR)makefiles/Rector.mk +include $(_MAKEFILE_DIR)makefiles/Utilities.mk # Include the Tools fragment after all other tooling so that bump and update of stand-alone tools collects all necessary targets include $(_MAKEFILE_DIR)makefiles/Tools.mk diff --git a/makefiles/Utilities.mk b/makefiles/Utilities.mk new file mode 100644 index 0000000..a158be4 --- /dev/null +++ b/makefiles/Utilities.mk @@ -0,0 +1,6 @@ + +HELP += $(call MK_HELP,'set-default-ignores','Ensures all the usual entries are added to .gitignore') + +set-default-ignores: install + $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} php -f $(_MAKEFILE_DIR)migrations/bin/standard-gitignores.php $(PROJECT_DIR) +.PHONY: set-default-ignores diff --git a/migrations/bin/standard-gitignores.php b/migrations/bin/standard-gitignores.php new file mode 100644 index 0000000..3928c1d --- /dev/null +++ b/migrations/bin/standard-gitignores.php @@ -0,0 +1,36 @@ +gitIgnore); +$toIgnore = [ + '/.phpunit.cache', + '/docs/html/', + '/doc/html/', + '/laminas-mkdoc-theme.tgz', + '/laminas-mkdoc-theme/', + '/phpunit.xml', + '/vendor/', + '/documentation-theme/', + '.markdownlint.json', +]; + +foreach ($toIgnore as $item) { + $ignore->ignore($item); +} diff --git a/migrations/src/GitIgnore.php b/migrations/src/GitIgnore.php new file mode 100644 index 0000000..36bebf2 --- /dev/null +++ b/migrations/src/GitIgnore.php @@ -0,0 +1,100 @@ + */ + private function getLines(): array + { + return explode(PHP_EOL, $this->read()); + } + + public function ignore(string $path): void + { + if ($this->isIgnored($path)) { + return; + } + + $content = sprintf( + "%s\n%s\n", + trim($this->read()), + $path, + ); + + $this->write($content); + } + + public function unignore(string $path): void + { + if (! $this->isIgnored($path)) { + return; + } + + $lines = $this->getLines(); + + foreach ($lines as $index => $line) { + if (! $this->matches($line, $path)) { + continue; + } + + unset($lines[$index]); + } + + $content = implode(PHP_EOL, $lines); + assert($content !== '', 'Expected non-empty content'); + + $this->write($content); + } + + private function isIgnored(string $path): bool + { + foreach ($this->getLines() as $line) { + if ($this->matches($line, $path)) { + return true; + } + } + + return false; + } + + private function matches(string $line, string $ignore): bool + { + return trim(trim($line), DIRECTORY_SEPARATOR) === trim(trim($ignore), DIRECTORY_SEPARATOR); + } + + /** @return non-empty-string */ + private function read(): string + { + $contents = file_get_contents($this->file); + assert( + $contents !== false && $contents !== '', + 'The file could not be read or it is empty', + ); + + return $contents; + } + + /** @param non-empty-string $data */ + private function write(string $data): void + { + file_put_contents($this->file, $data); + } +} diff --git a/migrations/src/ProjectInformation.php b/migrations/src/ProjectInformation.php index ac8fb4a..4be51a0 100644 --- a/migrations/src/ProjectInformation.php +++ b/migrations/src/ProjectInformation.php @@ -31,6 +31,8 @@ * @param non-empty-string|null $laminasCiWorkflow * @param non-empty-string|null $magoConfiguration * @param non-empty-string|null $makefile + * @param non-empty-string $gitAttributes + * @param non-empty-string $gitIgnore * * @mago-expect lint:excessive-parameter-list */ @@ -42,6 +44,7 @@ public function __construct( public string|null $magoConfiguration, public string|null $makefile, public string $gitAttributes, + public string $gitIgnore, ) {} /** @throws Throwable */ @@ -65,6 +68,7 @@ public static function fromDirectory(string $directory): self self::fileExistsOrNull($directory . '/mago.toml'), self::fileExistsOrNull($directory . '/Makefile'), self::assertFileExists($directory . '/.gitattributes'), + self::assertFileExists($directory . '/.gitignore'), ); } diff --git a/migrations/test/GitIgnoreTest.php b/migrations/test/GitIgnoreTest.php new file mode 100644 index 0000000..cafa032 --- /dev/null +++ b/migrations/test/GitIgnoreTest.php @@ -0,0 +1,226 @@ + + */ + public static function addIgnoreProvider(): iterable + { + yield 'Happy Path, adding' => [ + << [ + << [ + << [ + << [ + << [ + <<ignore($toIgnore); + + self::assertStringEqualsFile($file, $expect); + } + + /** + * @return iterable + */ + public static function removeIgnoreProvider(): iterable + { + yield 'Happy Path, removing' => [ + << [ + << [ + << [ + << [ + << [ + <<unignore($toIgnore); + + self::assertStringEqualsFile($file, $expect); + } +}