-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft command documentation export (#212)
- Loading branch information
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |