|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage\Commands\Concerns; |
| 6 | + |
| 7 | +use SplFileInfo; |
| 8 | +use Stolt\LeanPackage\Analyser; |
| 9 | +use Stolt\LeanPackage\Exceptions\InvalidGlobPattern; |
| 10 | +use Stolt\LeanPackage\Exceptions\InvalidGlobPatternFile; |
| 11 | +use Stolt\LeanPackage\Exceptions\NonExistentGlobPatternFile; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | + |
| 16 | +trait GeneratesGitattributesOptions |
| 17 | +{ |
| 18 | + protected string $defaultPreset = 'Php'; |
| 19 | + |
| 20 | + protected function getDefaultLpvFile(): string |
| 21 | + { |
| 22 | + $base = \defined('WORKING_DIRECTORY') ? WORKING_DIRECTORY : (\getcwd() ?: '.'); |
| 23 | + |
| 24 | + return $base . DIRECTORY_SEPARATOR . '.lpv'; |
| 25 | + } |
| 26 | + |
| 27 | + protected function addGenerationOptions(callable $addOption): void |
| 28 | + { |
| 29 | + // Glob/preset related |
| 30 | + $addOption('glob-pattern', null, InputOption::VALUE_REQUIRED, 'Use this glob pattern to match artifacts that should be export-ignored'); |
| 31 | + $addOption('glob-pattern-file', null, InputOption::VALUE_OPTIONAL, 'Use this file with glob patterns to match export-ignored artifacts', $this->getDefaultLpvFile()); |
| 32 | + $addOption('preset', null, InputOption::VALUE_OPTIONAL, 'Use the glob pattern of the given language preset', $this->defaultPreset); |
| 33 | + |
| 34 | + // Keep rules |
| 35 | + $addOption('keep-license', null, InputOption::VALUE_NONE, 'Do not export-ignore the license file'); |
| 36 | + $addOption('keep-readme', null, InputOption::VALUE_NONE, 'Do not export-ignore the README file'); |
| 37 | + $addOption('keep-glob-pattern', null, InputOption::VALUE_REQUIRED, 'Do not export-ignore matching glob pattern e.g. {LICENSE.*,README.*,docs*}'); |
| 38 | + |
| 39 | + // Layout/ordering |
| 40 | + $addOption('align-export-ignores', 'a', InputOption::VALUE_NONE, 'Align export-ignores on create or overwrite'); |
| 41 | + $addOption('sort-from-directories-to-files', 's', InputOption::VALUE_NONE, 'Sort export-ignores from directories to files'); |
| 42 | + $addOption('enforce-strict-order', null, InputOption::VALUE_NONE, 'Enforce strict order comparison (useful for consistency)'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Apply generation-related options to the analyser. |
| 47 | + */ |
| 48 | + protected function applyGenerationOptions(InputInterface $input, OutputInterface $output, Analyser $analyser): bool |
| 49 | + { |
| 50 | + $globPattern = $input->getOption('glob-pattern'); |
| 51 | + $globPatternFile = (string) $input->getOption('glob-pattern-file'); |
| 52 | + $chosenPreset = (string) $input->getOption('preset'); |
| 53 | + |
| 54 | + $keepLicense = (bool) $input->getOption('keep-license'); |
| 55 | + $keepReadme = (bool) $input->getOption('keep-readme'); |
| 56 | + $keepGlobPattern = (string) ($input->getOption('keep-glob-pattern') ?? ''); |
| 57 | + |
| 58 | + $alignExportIgnores = (bool) $input->getOption('align-export-ignores'); |
| 59 | + $sortFromDirectoriesToFiles = (bool) $input->getOption('sort-from-directories-to-files'); |
| 60 | + $enforceStrictOrderComparison = (bool) $input->getOption('enforce-strict-order'); |
| 61 | + |
| 62 | + // Order comparison (for consistency in generation/validation flow) |
| 63 | + if ($enforceStrictOrderComparison && $sortFromDirectoriesToFiles === false) { |
| 64 | + $output->writeln('+ Enforcing strict order comparison.', OutputInterface::VERBOSITY_VERBOSE); |
| 65 | + $analyser->enableStrictOrderComparison(); |
| 66 | + } |
| 67 | + |
| 68 | + if ($sortFromDirectoriesToFiles) { |
| 69 | + $output->writeln('+ Sorting from files to directories.', OutputInterface::VERBOSITY_VERBOSE); |
| 70 | + $analyser->sortFromDirectoriesToFiles(); |
| 71 | + } |
| 72 | + |
| 73 | + if ($keepLicense) { |
| 74 | + $output->writeln('+ Keeping the license file.', OutputInterface::VERBOSITY_VERBOSE); |
| 75 | + $analyser->keepLicense(); |
| 76 | + } |
| 77 | + |
| 78 | + if ($keepReadme) { |
| 79 | + $output->writeln('+ Keeping the README file.', OutputInterface::VERBOSITY_VERBOSE); |
| 80 | + $analyser->keepReadme(); |
| 81 | + } |
| 82 | + |
| 83 | + if ($keepGlobPattern !== '') { |
| 84 | + $output->writeln(\sprintf('+ Keeping files matching the glob pattern <info>%s</info>.', $keepGlobPattern), OutputInterface::VERBOSITY_VERBOSE); |
| 85 | + try { |
| 86 | + $analyser->setKeepGlobPattern($keepGlobPattern); |
| 87 | + } catch (InvalidGlobPattern $e) { |
| 88 | + $warning = "Warning: The provided glob pattern '{$keepGlobPattern}' is considered invalid."; |
| 89 | + $output->writeln('<error>' . $warning . '</error>'); |
| 90 | + $output->writeln($e->getMessage(), OutputInterface::VERBOSITY_DEBUG); |
| 91 | + |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if ($alignExportIgnores) { |
| 97 | + $output->writeln('+ Aligning the export-ignores.', OutputInterface::VERBOSITY_VERBOSE); |
| 98 | + $analyser->alignExportIgnores(); |
| 99 | + } |
| 100 | + |
| 101 | + // Glob selection/override order: explicit pattern -> glob file -> preset |
| 102 | + if ($globPattern || $globPattern === '') { |
| 103 | + try { |
| 104 | + $output->writeln("+ Using glob pattern <info>{$globPattern}</info>.", OutputInterface::VERBOSITY_VERBOSE); |
| 105 | + $analyser->setGlobPattern((string) $globPattern); |
| 106 | + } catch (InvalidGlobPattern $e) { |
| 107 | + $warning = "Warning: The provided glob pattern '{$globPattern}' is considered invalid."; |
| 108 | + $output->writeln('<error>' . $warning . '</error>'); |
| 109 | + $output->writeln($e->getMessage(), OutputInterface::VERBOSITY_DEBUG); |
| 110 | + |
| 111 | + return false; |
| 112 | + } |
| 113 | + } elseif ($this->isGlobPatternFileSettable($globPatternFile)) { |
| 114 | + try { |
| 115 | + if ($this->isDefaultGlobPatternFilePresent()) { |
| 116 | + $analyser->setGlobPatternFromFile($globPatternFile); |
| 117 | + } |
| 118 | + if ($globPatternFile) { |
| 119 | + $globPatternFileInfo = new SplFileInfo($globPatternFile); |
| 120 | + $output->writeln('+ Using ' . $globPatternFileInfo->getBasename() . ' file as glob pattern input.', OutputInterface::VERBOSITY_VERBOSE); |
| 121 | + |
| 122 | + $analyser->setGlobPatternFromFile($globPatternFile); |
| 123 | + } |
| 124 | + } catch (NonExistentGlobPatternFile $e) { |
| 125 | + $warning = "Warning: The provided glob pattern file '{$globPatternFile}' doesn't exist."; |
| 126 | + $output->writeln('<error>' . $warning . '</error>'); |
| 127 | + $output->writeln($e->getMessage(), OutputInterface::VERBOSITY_DEBUG); |
| 128 | + |
| 129 | + return false; |
| 130 | + } catch (InvalidGlobPatternFile $e) { |
| 131 | + $warning = "Warning: The provided glob pattern file '{$globPatternFile}' is considered invalid."; |
| 132 | + $output->writeln('<error>' . $warning . '</error>'); |
| 133 | + $output->writeln($e->getMessage(), OutputInterface::VERBOSITY_DEBUG); |
| 134 | + |
| 135 | + return false; |
| 136 | + } |
| 137 | + } elseif ($chosenPreset) { |
| 138 | + try { |
| 139 | + $output->writeln('+ Using the ' . $chosenPreset . ' language preset.', OutputInterface::VERBOSITY_VERBOSE); |
| 140 | + $analyser->setGlobPatternFromPreset($chosenPreset); |
| 141 | + } catch (\Stolt\LeanPackage\Exceptions\PresetNotAvailable $e) { |
| 142 | + $warning = 'Warning: The chosen language preset ' . $chosenPreset . ' is not available. Maybe contribute it?.'; |
| 143 | + $output->writeln('<error>' . $warning . '</error>'); |
| 144 | + |
| 145 | + return false; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + // Minimal copies of helper checks used in ValidateCommand |
| 153 | + protected function isGlobPatternFileSettable(string $file): bool |
| 154 | + { |
| 155 | + if ($this->isGlobPatternFileProvided($file)) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + return $this->isDefaultGlobPatternFilePresent(); |
| 160 | + } |
| 161 | + |
| 162 | + protected function isGlobPatternFileProvided(string $file): bool |
| 163 | + { |
| 164 | + return $file !== $this->getDefaultLpvFile(); |
| 165 | + } |
| 166 | + |
| 167 | + protected function isDefaultGlobPatternFilePresent(): bool |
| 168 | + { |
| 169 | + return \file_exists($this->getDefaultLpvFile()); |
| 170 | + } |
| 171 | +} |
0 commit comments