|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage\Commands; |
| 6 | + |
| 7 | +use Stolt\LeanPackage\Analyser; |
| 8 | +use Stolt\LeanPackage\Exceptions\PresetNotAvailable; |
| 9 | +use Stolt\LeanPackage\Presets\CommonPreset; |
| 10 | +use Stolt\LeanPackage\Presets\Finder; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Input\InputArgument; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Input\InputOption; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class RefreshCommand extends Command |
| 18 | +{ |
| 19 | + private const DEFAULT_PRESET = 'PHP'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Package analyser. |
| 23 | + * |
| 24 | + * @var Analyser |
| 25 | + */ |
| 26 | + protected Analyser $analyser; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Finder |
| 30 | + */ |
| 31 | + private Finder $finder; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param Analyser $analyser |
| 35 | + */ |
| 36 | + public function __construct(Analyser $analyser) |
| 37 | + { |
| 38 | + $this->analyser = $analyser; |
| 39 | + $this->finder = $analyser->getFinder(); |
| 40 | + |
| 41 | + parent::__construct(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Command configuration. |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + protected function configure(): void |
| 50 | + { |
| 51 | + $this->analyser->setDirectory(WORKING_DIRECTORY); |
| 52 | + |
| 53 | + $this |
| 54 | + ->setName('refresh') |
| 55 | + ->setDescription('Refresh a present .lpv file'); |
| 56 | + |
| 57 | + $availablePresets = (new CommonPreset())->formatAvailablePresetDefinitionsForDescription( |
| 58 | + $this->finder->getAvailablePresets() |
| 59 | + ); |
| 60 | + |
| 61 | + $directoryDescription = 'The directory of a project/micro-package repository'; |
| 62 | + $presetDescription = 'The preset to use for the .lpv file. Available ones are ' . $availablePresets . '.'; |
| 63 | + |
| 64 | + $this->addArgument( |
| 65 | + 'directory', |
| 66 | + InputArgument::OPTIONAL, |
| 67 | + $directoryDescription, |
| 68 | + $this->analyser->getDirectory() |
| 69 | + ); |
| 70 | + |
| 71 | + $this->addOption( |
| 72 | + 'preset', |
| 73 | + null, |
| 74 | + InputOption::VALUE_REQUIRED, |
| 75 | + $presetDescription, |
| 76 | + self::DEFAULT_PRESET |
| 77 | + ); |
| 78 | + $this->getDefinition()->addOption(new InputOption( |
| 79 | + 'dry-run', |
| 80 | + null, |
| 81 | + InputOption::VALUE_NONE, |
| 82 | + 'Do not write any files. Output the content that would be written' |
| 83 | + )); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Execute command. |
| 88 | + * |
| 89 | + * @param InputInterface $input |
| 90 | + * @param OutputInterface $output |
| 91 | + * |
| 92 | + * @throws PresetNotAvailable |
| 93 | + * @return integer |
| 94 | + */ |
| 95 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 96 | + { |
| 97 | + $directory = (string) $input->getArgument('directory'); |
| 98 | + $chosenPreset = (string) $input->getOption('preset'); |
| 99 | + $dryRun = (bool) $input->getOption('dry-run'); |
| 100 | + |
| 101 | + if ($directory !== '' && $directory !== WORKING_DIRECTORY) { |
| 102 | + try { |
| 103 | + $this->analyser->setDirectory($directory); |
| 104 | + } catch (\RuntimeException $e) { |
| 105 | + $warning = "Warning: The provided directory " |
| 106 | + . "'$directory' does not exist or is not a directory."; |
| 107 | + $output->writeln('<error>' . $warning . '</error>'); |
| 108 | + $output->writeln($e->getMessage(), OutputInterface::VERBOSITY_DEBUG); |
| 109 | + |
| 110 | + return Command::FAILURE; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + $defaultLpvFile = WORKING_DIRECTORY . DIRECTORY_SEPARATOR . '.lpv'; |
| 115 | + |
| 116 | + if (!\file_exists($defaultLpvFile) && $dryRun !== true) { |
| 117 | + $output->writeln('<error>Warning: No default .lpv file exists to refresh.</error>'); |
| 118 | + |
| 119 | + return Command::FAILURE; |
| 120 | + } |
| 121 | + |
| 122 | + if ($chosenPreset === '' || !\in_array(\strtolower($chosenPreset), \array_map('strtolower', $this->finder->getAvailablePresets()), true)) { |
| 123 | + $warning = 'Warning: Chosen preset ' . $chosenPreset . ' is not available. Maybe contribute it?.'; |
| 124 | + $output->writeln('<error>' . $warning . '</error>'); |
| 125 | + |
| 126 | + return Command::FAILURE; |
| 127 | + } |
| 128 | + |
| 129 | + $expectedGlobPattern = $this->finder->getPresetGlobByLanguageName($chosenPreset); |
| 130 | + $expectedContent = \implode("\n", $expectedGlobPattern); |
| 131 | + |
| 132 | + $existingContent = ''; |
| 133 | + if (\file_exists($defaultLpvFile)) { |
| 134 | + $existingContent = (string) \file_get_contents($defaultLpvFile); |
| 135 | + } |
| 136 | + |
| 137 | + $existingLines = \array_values(\array_filter( |
| 138 | + \preg_split('/\r\n|\r|\n/', $existingContent) ?: [], |
| 139 | + static fn (string $line): bool => \trim($line) !== '' |
| 140 | + )); |
| 141 | + |
| 142 | + $mergedLines = $existingLines; |
| 143 | + foreach (\preg_split('/\r\n|\r|\n/', $expectedContent) ?: [] as $line) { |
| 144 | + $line = \trim((string) $line); |
| 145 | + if ($line === '') { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + if (!\in_array($line, $mergedLines, true)) { |
| 150 | + $mergedLines[] = $line; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + $refreshedContent = \implode("\n", $mergedLines); |
| 155 | + |
| 156 | + if ($dryRun === true) { |
| 157 | + $output->writeln($refreshedContent); |
| 158 | + |
| 159 | + return Command::SUCCESS; |
| 160 | + } |
| 161 | + |
| 162 | + $bytesWritten = file_put_contents($defaultLpvFile, $refreshedContent); |
| 163 | + |
| 164 | + if ($bytesWritten === false) { |
| 165 | + $output->writeln('<error>Warning: The refresh of the default .lpv file failed.</error>'); |
| 166 | + |
| 167 | + return Command::FAILURE; |
| 168 | + } |
| 169 | + |
| 170 | + $output->writeln('Refreshed default \'' . $defaultLpvFile . '\' file.'); |
| 171 | + |
| 172 | + return Command::SUCCESS; |
| 173 | + } |
| 174 | +} |
0 commit comments