From e4947349577c04af71b4f153b0aea5e10869a3a0 Mon Sep 17 00:00:00 2001 From: Arnaud Oltra Date: Tue, 7 Nov 2023 21:46:37 -0500 Subject: [PATCH] Clean command, with option for path to export, an clear json export --- .../Command/ExportListCommandsCommand.php | 107 +++++++++--------- .../Command/ExportListCommandsCommandTest.php | 2 +- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/packages/console/Command/ExportListCommandsCommand.php b/packages/console/Command/ExportListCommandsCommand.php index f86e6c19..0d9d6475 100644 --- a/packages/console/Command/ExportListCommandsCommand.php +++ b/packages/console/Command/ExportListCommandsCommand.php @@ -2,14 +2,13 @@ namespace Draw\Component\Console\Command; -use Symfony\Component\Process\Process; -use Symfony\Component\Console\Application; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Process\Process; #[AsCommand(name: 'app:list-commands')] class ExportListCommandsCommand extends Command @@ -18,85 +17,81 @@ protected function configure(): void { $this ->setDescription('List all available Symfony commands with their documentation.') - ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output file', 'commands_help.txt'); + ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output file', 'commands_help.json'); } protected function execute(InputInterface $input, OutputInterface $output): int { $application = $this->getApplication(); $outputFile = $input->getOption('output'); + $data = []; foreach ($application->all() as $command) { $process = new Process(['bin/console', $command->getName(), '--help']); $process->mustRun(); if ($process->isSuccessful()) { - file_put_contents($outputFile, $this->addNameToTheFullDescription($this->generateBlockText($command->getName()), $process->getOutput()), FILE_APPEND); + $data[] = $this->strToJson($this->addNameToTheFullDescription($this->generateBlockText($command->getName()), $process->getOutput())); } else { $output->writeln(sprintf('Error running command: %s', $command->getName())); } } + file_put_contents($outputFile, json_encode($data, \JSON_PRETTY_PRINT), \FILE_APPEND); + $io = new SymfonyStyle($input, $output); $io->title('Export completed'); + return Command::SUCCESS; } - private function generateBlockText($title, $width = 30, $borderChar = '#', $paddingChar = ' '): string + private function generateBlockText($title): string { - $titleLength = strlen($title); - - if ($titleLength > $width - 4) { - $width = $titleLength + 4; - } - - $topLine = str_repeat($borderChar, $width); - $titleLine = $borderChar . ' ' . str_pad($title, $width - 4, $paddingChar, STR_PAD_BOTH) . ' ' . $borderChar; - $bottomLine = $topLine; - $blockText = $topLine . PHP_EOL . $titleLine . PHP_EOL . $bottomLine; - return $blockText; + return 'Name: '.\PHP_EOL.$title; } private function addNameToTheFullDescription($name, $fullDescription): string { - return $name . PHP_EOL . $fullDescription; + return $name.\PHP_EOL.$fullDescription; } + private function strToJson($input): array + { + $lines = explode("\n", $input); + $indexes = ['Name:', 'Description:', 'Usage:', 'Arguments:', 'Options:', 'Help:']; + $result = []; + $currentKey = ''; + $subArray = []; + foreach ($lines as $line) { + $line = trim($line); -// option with json - - // private function generateBlockText($title): string - // { - // return 'Name: ' . PHP_EOL . $title; - // } - // private function addNameToTheFullDescription($name, $fullDescription): string - // { - // return $name . PHP_EOL . $fullDescription; - // } - // private function strToJson($input): array - // { - // $lines = explode("\n", $input); - // $result = []; - // $os = ['Name:', 'Description:', 'Usage:', 'Arguments:', 'Options:', 'Help:']; - // $currentSection = ''; - // foreach ($lines as $line) { - // $line = trim($line); - // if (empty($line)) { - // continue; - // } - // if (in_array($line, $os) !== false) { - // list($key, $value) = array_map('trim', explode(':', $line, 2)); - // $currentSection = $line; - // $currentSection = str_replace(':', '', $currentSection); - // $currentSection = ucfirst(strtolower($currentSection)); - // $result[$currentSection] = ''; - // } elseif (!empty($currentSection)) { - // $result[$currentSection] .= ' ' . $line; - // } - // } - // foreach ($result as $key => $value) { - // $result[$key] = trim($value); - // } - // return $result; - // } + if (false !== \in_array($line, $indexes)) { + if (!empty($subArray)) { + $result[$currentKey] = $subArray; + $subArray = []; + } + $currentKey = trim($line); + } else { + $subArray[] = $line; + } + } + + if (!empty($subArray)) { + $result[$currentKey] = $subArray; + } + + foreach ($result as $key => $values) { + foreach ($values as $index => $value) { + $result[$key][$index] = trim($value); + } + } + + foreach ($indexes as $index) { + if (isset($result[$index])) { + $result[$index] = array_filter($result[$index]); + } + } + + return $result; + } } diff --git a/tests/Command/ExportListCommandsCommandTest.php b/tests/Command/ExportListCommandsCommandTest.php index d1f5973e..b520c08c 100644 --- a/tests/Command/ExportListCommandsCommandTest.php +++ b/tests/Command/ExportListCommandsCommandTest.php @@ -2,8 +2,8 @@ namespace App\Tests\Command; -use App\Command\ExportListCommandsCommand; use App\Tests\TestCase; +use Draw\Component\Console\Command\ExportListCommandsCommand; use Draw\Component\Tester\Application\CommandDataTester; use Draw\Component\Tester\Application\CommandTestTrait; use Symfony\Component\Console\Command\Command;