Skip to content

Commit

Permalink
Draft command documentation export (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
olarno authored Nov 12, 2023
1 parent 40da812 commit a754ecf
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/console/Command/ExportListCommandsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Draw\Component\Console\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

#[AsCommand(name: 'app:list-commands')]
class ExportListCommandsCommand extends Command
{
protected function configure(): void
{
$this
->setDescription('List all available Symfony commands with their documentation.')
->addArgument('Path', InputArgument::REQUIRED, 'Output folder path')
->addArgument('Filename', InputArgument::REQUIRED, 'Output filename');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$filesystem = new Filesystem();
$folderPath = $input->getArgument('Path');
$resultFilePath = $folderPath.$input->getArgument('Filename');
$application = $this->getApplication();

$this->validateOutputPathFile($filesystem, $folderPath);

foreach ($application->all() as $command) {
$process = new Process(['bin/console', $command->getName(), '--help']);
$process->mustRun();

try {
if ($process->isSuccessful()) {
file_put_contents($resultFilePath, $this->addBreakLineAtTheEnd($this->addNameToTheFullDescription($this->generateBlockText($command->getName()), $process->getOutput())), \FILE_APPEND);
} else {
$io->warning(sprintf('Error running command: %s', $command->getName()));
}
} catch (\Throwable $th) {
$io->error('An error occurred: '.$th->getMessage());
continue;
}
}

$io->success('Export completed');

return Command::SUCCESS;
}

private function generateBlockText(string $title): string
{
$width = 30;
$titleLength = \strlen($title);

if ($titleLength > $width - 4) {
$width = $titleLength + 4;
}

$borderLine = str_repeat('#', $width);
$titleLine = '# '.str_pad($title, $width - 4, ' ', \STR_PAD_BOTH).' #';

return $borderLine.\PHP_EOL.$titleLine.\PHP_EOL.$borderLine;
}

private function addNameToTheFullDescription(string $name, string $fullDescription): string
{
return $name.\PHP_EOL.$fullDescription;
}

private function addBreakLineAtTheEnd(string $text): string
{
return $text.\PHP_EOL;
}

private function validateOutputPathFile(Filesystem $filesystem, string $path): void
{
if (!$filesystem->exists($path)) {
$filesystem->mkdir($path);
}
}
}
69 changes: 69 additions & 0 deletions tests/Console/Command/ExportListCommandsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Tests\Console\Command;

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;
use Symfony\Component\Console\Input\InputOption;

/**
* @covers \App\Command\ExportListCommandsCommand
*/
class ExportListCommandsCommandTest extends TestCase
{
use CommandTestTrait;

public function createCommand(): Command
{
return static::getContainer()->get(ExportListCommandsCommand::class);
}

public function getCommandName(): string
{
return 'app:list-commands';
}

public static function provideTestArgument(): iterable
{
return [['Path' => __DIR__.'/Fixtures/ExportListCommandsCommandTest/'], ['Filename' => 'testExecution.txt']];

}

public static function provideTestOption(): iterable
{
yield [
'draw-execution-id',
null,
InputOption::VALUE_REQUIRED,
];

yield [
'draw-execution-ignore',
null,
InputOption::VALUE_NONE,
];

yield [
'aws-newest-instance-role',
null,
InputOption::VALUE_REQUIRED,
];
}

public function testExecute(): void
{
$this->execute(['Path' => __DIR__.'/Fixtures/ExportListCommandsCommandTest/', 'Filename' => 'testExecution.txt'])
->test(
CommandDataTester::create()
->setExpectedDisplay($this->getDefaultExpectation())
);
}

private function getDefaultExpectation(): string
{
return file_get_contents(__DIR__.'/Fixtures/ExportListCommandsCommandTest/testExecution_expectedExport.txt');
}
}

0 comments on commit a754ecf

Please sign in to comment.