|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage\Commands; |
| 6 | + |
| 7 | +use Stolt\LeanPackage\Exceptions\GitNotAvailable; |
| 8 | +use Stolt\LeanPackage\Exceptions\TreeNotAvailable; |
| 9 | +use Stolt\LeanPackage\Helpers\Str as OsHelper; |
| 10 | +use Stolt\LeanPackage\Tree; |
| 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 | +final class TreeCommand extends Command |
| 18 | +{ |
| 19 | + private Tree $tree; |
| 20 | + |
| 21 | + private string $directoryToOperateOn; |
| 22 | + |
| 23 | + public function __construct(Tree $tree) |
| 24 | + { |
| 25 | + $this->tree = $tree; |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Command configuration. |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + protected function configure(): void |
| 35 | + { |
| 36 | + $this->directoryToOperateOn = WORKING_DIRECTORY; |
| 37 | + $this->setName('tree'); |
| 38 | + $description = 'Displays the source structure of a given ' |
| 39 | + . "project/micro-package repository or it's dist package"; |
| 40 | + $this->setDescription($description); |
| 41 | + |
| 42 | + $directoryDescription = 'The directory of a project/micro-package repository'; |
| 43 | + |
| 44 | + $this->addArgument( |
| 45 | + 'directory', |
| 46 | + InputArgument::OPTIONAL, |
| 47 | + $directoryDescription, |
| 48 | + $this->directoryToOperateOn |
| 49 | + ); |
| 50 | + |
| 51 | + $srcDescription = 'Show the flat src structure of the project/micro-package repository'; |
| 52 | + $distPackageDescription = 'Show the flat dist package structure of the project/micro-package'; |
| 53 | + |
| 54 | + $this->addOption('src', null, InputOption::VALUE_NONE, $srcDescription); |
| 55 | + $this->addOption('dist-package', null, InputOption::VALUE_NONE, $distPackageDescription); |
| 56 | + } |
| 57 | + |
| 58 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 59 | + { |
| 60 | + $directory = (string) $input->getArgument('directory'); |
| 61 | + |
| 62 | + if ($directory !== $this->directoryToOperateOn) { |
| 63 | + if (!\file_get_contents($directory) || !\is_dir($directory)) { |
| 64 | + $warning = "Warning: The provided directory " |
| 65 | + . "'$directory' does not exist or is not a directory."; |
| 66 | + $outputContent = '<error>' . $warning . '</error>'; |
| 67 | + $output->writeln($outputContent); |
| 68 | + |
| 69 | + return Command::FAILURE; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + $showSrcTree = $input->getOption('src'); |
| 74 | + $showDistPackageTree = $input->getOption('dist-package'); |
| 75 | + |
| 76 | + if ($showSrcTree) { |
| 77 | + $verboseOutput = '+ Showing flat structure of package source.'; |
| 78 | + $output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE); |
| 79 | + |
| 80 | + $output->writeln('Package: <info>' . $this->getPackageName() . '</info>'); |
| 81 | + $output->write($this->tree->getTreeForSrc($this->directoryToOperateOn)); |
| 82 | + |
| 83 | + return Command::SUCCESS; |
| 84 | + } |
| 85 | + |
| 86 | + if ($showDistPackageTree) { |
| 87 | + $verboseOutput = '+ Showing flat structure of dist package.'; |
| 88 | + $output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE); |
| 89 | + |
| 90 | + $output->writeln('Package: <info>' . $this->getPackageName() . '</info>'); |
| 91 | + $output->write($this->tree->getTreeForDistPackage($this->directoryToOperateOn)); |
| 92 | + |
| 93 | + return Command::SUCCESS; |
| 94 | + } |
| 95 | + |
| 96 | + return Command::FAILURE; |
| 97 | + } |
| 98 | + |
| 99 | + protected function getPackageName(): string |
| 100 | + { |
| 101 | + $composerContentAsJson = \json_decode(\file_get_contents('composer.json'), true); |
| 102 | + return \trim($composerContentAsJson['name']); |
| 103 | + } |
| 104 | +} |
0 commit comments