Skip to content

Commit

Permalink
Clean command, with option for path to export, an clear json export
Browse files Browse the repository at this point in the history
  • Loading branch information
olarno committed Nov 8, 2023
1 parent 2b495c5 commit e494734
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 57 deletions.
107 changes: 51 additions & 56 deletions packages/console/Command/ExportListCommandsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/Command/ExportListCommandsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e494734

Please sign in to comment.