|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of fast-forward/dev-tools. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + * |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward |
| 16 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace FastForward\DevTools\Command; |
| 20 | + |
| 21 | +use FastForward\DevTools\GitAttributes\CandidateProvider; |
| 22 | +use FastForward\DevTools\GitAttributes\CandidateProviderInterface; |
| 23 | +use FastForward\DevTools\GitAttributes\ExistenceChecker; |
| 24 | +use FastForward\DevTools\GitAttributes\ExistenceCheckerInterface; |
| 25 | +use FastForward\DevTools\GitAttributes\ExportIgnoreFilter; |
| 26 | +use FastForward\DevTools\GitAttributes\ExportIgnoreFilterInterface; |
| 27 | +use FastForward\DevTools\GitAttributes\Merger; |
| 28 | +use FastForward\DevTools\GitAttributes\MergerInterface; |
| 29 | +use FastForward\DevTools\GitAttributes\Reader; |
| 30 | +use FastForward\DevTools\GitAttributes\ReaderInterface; |
| 31 | +use FastForward\DevTools\GitAttributes\Writer; |
| 32 | +use FastForward\DevTools\GitAttributes\WriterInterface; |
| 33 | +use Symfony\Component\Console\Input\InputInterface; |
| 34 | +use Symfony\Component\Console\Output\OutputInterface; |
| 35 | +use Symfony\Component\Filesystem\Filesystem; |
| 36 | +use Symfony\Component\Filesystem\Path; |
| 37 | + |
| 38 | +/** |
| 39 | + * Provides functionality to manage .gitattributes export-ignore rules. |
| 40 | + * |
| 41 | + * This command adds export-ignore entries for repository-only files and directories |
| 42 | + * to keep them out of Composer package archives. |
| 43 | + */ |
| 44 | +final class GitAttributesCommand extends AbstractCommand |
| 45 | +{ |
| 46 | + private const string EXTRA_NAMESPACE = 'gitattributes'; |
| 47 | + |
| 48 | + private const string EXTRA_KEEP_IN_EXPORT = 'keep-in-export'; |
| 49 | + |
| 50 | + private const string EXTRA_NO_EXPORT_IGNORE = 'no-export-ignore'; |
| 51 | + |
| 52 | + private readonly WriterInterface $writer; |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a new GitAttributesCommand instance. |
| 56 | + * |
| 57 | + * @param Filesystem|null $filesystem the filesystem component |
| 58 | + * @param CandidateProviderInterface $candidateProvider the candidate provider |
| 59 | + * @param ExistenceCheckerInterface $existenceChecker the repository path existence checker |
| 60 | + * @param ExportIgnoreFilterInterface $exportIgnoreFilter the configured candidate filter |
| 61 | + * @param MergerInterface $merger the merger component |
| 62 | + * @param ReaderInterface $reader the reader component |
| 63 | + * @param WriterInterface|null $writer the writer component |
| 64 | + */ |
| 65 | + public function __construct( |
| 66 | + ?Filesystem $filesystem = null, |
| 67 | + private readonly CandidateProviderInterface $candidateProvider = new CandidateProvider(), |
| 68 | + private readonly ExistenceCheckerInterface $existenceChecker = new ExistenceChecker(), |
| 69 | + private readonly ExportIgnoreFilterInterface $exportIgnoreFilter = new ExportIgnoreFilter(), |
| 70 | + private readonly MergerInterface $merger = new Merger(), |
| 71 | + private readonly ReaderInterface $reader = new Reader(), |
| 72 | + ?WriterInterface $writer = null, |
| 73 | + ) { |
| 74 | + parent::__construct($filesystem); |
| 75 | + $this->writer = $writer ?? new Writer($this->filesystem); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Configures the current command. |
| 80 | + * |
| 81 | + * This method MUST define the name, description, and help text for the command. |
| 82 | + * |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + protected function configure(): void |
| 86 | + { |
| 87 | + $this |
| 88 | + ->setName('gitattributes') |
| 89 | + ->setDescription('Manages .gitattributes export-ignore rules for leaner package archives.') |
| 90 | + ->setHelp( |
| 91 | + 'This command adds export-ignore entries for repository-only files and directories ' |
| 92 | + . 'to keep them out of Composer package archives. Only paths that exist in the ' |
| 93 | + . 'repository are added, existing custom rules are preserved, and ' |
| 94 | + . '"extra.gitattributes.keep-in-export" paths stay in exported archives.' |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Configures the current command. |
| 100 | + * |
| 101 | + * This method MUST define the name, description, and help text for the command. |
| 102 | + * |
| 103 | + * @param InputInterface $input |
| 104 | + * @param OutputInterface $output |
| 105 | + */ |
| 106 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 107 | + { |
| 108 | + $output->writeln('<info>Synchronizing .gitattributes export-ignore rules...</info>'); |
| 109 | + |
| 110 | + $basePath = $this->getCurrentWorkingDirectory(); |
| 111 | + $keepInExportPaths = $this->configuredKeepInExportPaths(); |
| 112 | + |
| 113 | + $folderCandidates = $this->exportIgnoreFilter->filter($this->candidateProvider->folders(), $keepInExportPaths); |
| 114 | + $fileCandidates = $this->exportIgnoreFilter->filter($this->candidateProvider->files(), $keepInExportPaths); |
| 115 | + |
| 116 | + $existingFolders = $this->existenceChecker->filterExisting($basePath, $folderCandidates); |
| 117 | + $existingFiles = $this->existenceChecker->filterExisting($basePath, $fileCandidates); |
| 118 | + |
| 119 | + $entries = [...$existingFolders, ...$existingFiles]; |
| 120 | + |
| 121 | + if ([] === $entries) { |
| 122 | + $output->writeln( |
| 123 | + '<comment>No candidate paths found in repository. Skipping .gitattributes sync.</comment>' |
| 124 | + ); |
| 125 | + |
| 126 | + return self::SUCCESS; |
| 127 | + } |
| 128 | + |
| 129 | + $gitattributesPath = Path::join($basePath, '.gitattributes'); |
| 130 | + $existingContent = $this->reader->read($gitattributesPath); |
| 131 | + $content = $this->merger->merge($existingContent, $entries, $keepInExportPaths); |
| 132 | + $this->writer->write($gitattributesPath, $content); |
| 133 | + |
| 134 | + $output->writeln(\sprintf( |
| 135 | + '<info>Added %d export-ignore entries to .gitattributes.</info>', |
| 136 | + \count($entries) |
| 137 | + )); |
| 138 | + |
| 139 | + return self::SUCCESS; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Resolves the consumer-defined paths that MUST stay in exported archives. |
| 144 | + * |
| 145 | + * The preferred configuration key is "extra.gitattributes.keep-in-export". |
| 146 | + * The alternate "extra.gitattributes.no-export-ignore" key remains |
| 147 | + * supported as a compatibility alias. |
| 148 | + * |
| 149 | + * @return list<string> the configured keep-in-export paths |
| 150 | + */ |
| 151 | + private function configuredKeepInExportPaths(): array |
| 152 | + { |
| 153 | + $extra = $this->requireComposer() |
| 154 | + ->getPackage() |
| 155 | + ->getExtra(); |
| 156 | + |
| 157 | + $gitattributesConfig = $extra[self::EXTRA_NAMESPACE] ?? null; |
| 158 | + |
| 159 | + if (! \is_array($gitattributesConfig)) { |
| 160 | + return []; |
| 161 | + } |
| 162 | + |
| 163 | + $configuredPaths = []; |
| 164 | + |
| 165 | + foreach ([self::EXTRA_KEEP_IN_EXPORT, self::EXTRA_NO_EXPORT_IGNORE] as $key) { |
| 166 | + $values = $gitattributesConfig[$key] ?? []; |
| 167 | + |
| 168 | + if (\is_string($values)) { |
| 169 | + $values = [$values]; |
| 170 | + } |
| 171 | + |
| 172 | + if (! \is_array($values)) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + foreach ($values as $value) { |
| 177 | + if (\is_string($value)) { |
| 178 | + $configuredPaths[] = $value; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return array_values(array_unique($configuredPaths)); |
| 184 | + } |
| 185 | +} |
0 commit comments