|
| 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\GitIgnore\Merger; |
| 22 | +use FastForward\DevTools\GitIgnore\MergerInterface; |
| 23 | +use FastForward\DevTools\GitIgnore\Reader; |
| 24 | +use FastForward\DevTools\GitIgnore\ReaderInterface; |
| 25 | +use FastForward\DevTools\GitIgnore\Writer; |
| 26 | +use FastForward\DevTools\GitIgnore\WriterInterface; |
| 27 | +use Symfony\Component\Console\Input\InputInterface; |
| 28 | +use Symfony\Component\Console\Input\InputOption; |
| 29 | +use Symfony\Component\Console\Output\OutputInterface; |
| 30 | +use Symfony\Component\Filesystem\Filesystem; |
| 31 | + |
| 32 | +/** |
| 33 | + * Provides functionality to merge and synchronize .gitignore files. |
| 34 | + * |
| 35 | + * This command merges the canonical .gitignore from dev-tools with the project's |
| 36 | + * existing .gitignore, removing duplicates and sorting entries. |
| 37 | + * |
| 38 | + * The command accepts two options: --source and --target to specify the paths |
| 39 | + * to the canonical and project .gitignore files respectively. |
| 40 | + */ |
| 41 | +final class GitIgnoreCommand extends AbstractCommand |
| 42 | +{ |
| 43 | + /** |
| 44 | + * @param WriterInterface $writer the writer component for handling .gitignore file writing |
| 45 | + */ |
| 46 | + private readonly WriterInterface $writer; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a new GitIgnoreCommand instance. |
| 50 | + * |
| 51 | + * @param Filesystem|null $filesystem the filesystem component |
| 52 | + * @param MergerInterface $merger the merger component |
| 53 | + * @param ReaderInterface $reader the reader component |
| 54 | + * @param WriterInterface|null $writer the writer component |
| 55 | + */ |
| 56 | + public function __construct( |
| 57 | + ?Filesystem $filesystem = null, |
| 58 | + private readonly MergerInterface $merger = new Merger(), |
| 59 | + private readonly ReaderInterface $reader = new Reader(), |
| 60 | + ?WriterInterface $writer = null |
| 61 | + ) { |
| 62 | + parent::__construct($filesystem); |
| 63 | + $this->writer = $writer ?? new Writer($this->filesystem); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Configures the current command. |
| 68 | + * |
| 69 | + * This method MUST define the name, description, and help text for the command. |
| 70 | + * It SHALL identify the tool as the mechanism for script synchronization. |
| 71 | + */ |
| 72 | + protected function configure(): void |
| 73 | + { |
| 74 | + $this |
| 75 | + ->setName('gitignore') |
| 76 | + ->setDescription('Merges and synchronizes .gitignore files.') |
| 77 | + ->setHelp( |
| 78 | + "This command merges the canonical .gitignore from dev-tools with the project's existing .gitignore." |
| 79 | + ) |
| 80 | + ->addOption( |
| 81 | + name: 'source', |
| 82 | + shortcut: 's', |
| 83 | + mode: InputOption::VALUE_OPTIONAL, |
| 84 | + description: 'Path to the source .gitignore file (canonical)', |
| 85 | + default: parent::getDevToolsFile('.gitignore'), |
| 86 | + ) |
| 87 | + ->addOption( |
| 88 | + name: 'target', |
| 89 | + shortcut: 't', |
| 90 | + mode: InputOption::VALUE_OPTIONAL, |
| 91 | + description: 'Path to the target .gitignore file (project)', |
| 92 | + default: parent::getConfigFile('.gitignore', true) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Executes the gitignore merge process. |
| 98 | + * |
| 99 | + * @param InputInterface $input the input interface |
| 100 | + * @param OutputInterface $output the output interface |
| 101 | + * |
| 102 | + * @return int the status code |
| 103 | + */ |
| 104 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 105 | + { |
| 106 | + $output->writeln('<info>Merging .gitignore files...</info>'); |
| 107 | + |
| 108 | + $sourcePath = $input->getOption('source'); |
| 109 | + $targetPath = $input->getOption('target'); |
| 110 | + |
| 111 | + $canonical = $this->reader->read($sourcePath); |
| 112 | + $project = $this->reader->read($targetPath); |
| 113 | + |
| 114 | + $merged = $this->merger->merge($canonical, $project); |
| 115 | + |
| 116 | + $this->writer->write($merged); |
| 117 | + |
| 118 | + $output->writeln('<info>Successfully merged .gitignore file.</info>'); |
| 119 | + |
| 120 | + return self::SUCCESS; |
| 121 | + } |
| 122 | +} |
0 commit comments