Skip to content
Merged
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions makefiles/Utilities.mk
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions migrations/bin/standard-gitignores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Laminas\Internal\Migrations\GitIgnore;
use Laminas\Internal\Migrations\ProjectInformation;

require __DIR__ . '/../autoloader.php';

/** @var mixed $rootDirectory */
$rootDirectory = $argv[1] ?? null;
if (
! is_string($rootDirectory)
|| $rootDirectory === ''
|| ! is_dir($rootDirectory)
) {
throw new RuntimeException('Pass a single argument representing the root directory of the target library');
}

$project = ProjectInformation::fromDirectory($rootDirectory);
$ignore = new GitIgnore($project->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);
}
100 changes: 100 additions & 0 deletions migrations/src/GitIgnore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Laminas\Internal\Migrations;

use function assert;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function sprintf;
use function trim;

use const DIRECTORY_SEPARATOR;
use const PHP_EOL;

final readonly class GitIgnore
{
public function __construct(
private string $file,
) {}

/** @return list<string> */
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);
}
}
4 changes: 4 additions & 0 deletions migrations/src/ProjectInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -42,6 +44,7 @@ public function __construct(
public string|null $magoConfiguration,
public string|null $makefile,
public string $gitAttributes,
public string $gitIgnore,
) {}

/** @throws Throwable */
Expand All @@ -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'),
);
}

Expand Down
Loading
Loading