diff --git a/src/Console/GenerateCommand.php b/src/Console/GenerateCommand.php index 84ceabb2..f3edb1a3 100644 --- a/src/Console/GenerateCommand.php +++ b/src/Console/GenerateCommand.php @@ -7,6 +7,7 @@ use Butschster\ContextGenerator\Application\AppScope; use Butschster\ContextGenerator\Config\ConfigurationProvider; use Butschster\ContextGenerator\Config\Exception\ConfigLoaderException; +use Butschster\ContextGenerator\Console\Renderer\GenerateCommandRenderer; use Butschster\ContextGenerator\DirectoriesInterface; use Butschster\ContextGenerator\Document\Compiler\DocumentCompiler; use Spiral\Console\Attribute\Option; @@ -64,13 +65,13 @@ public function __invoke(Container $container, DirectoriesInterface $dirs): int try { // Get the appropriate loader based on options provided if ($this->inlineJson !== null) { - $this->output->info('Using inline JSON configuration...'); + $this->logger->info('Using inline JSON configuration...'); $loader = $configProvider->fromString($this->inlineJson); } elseif ($this->configPath !== null) { - $this->output->info(\sprintf('Loading configuration from %s...', $this->configPath)); + $this->logger->info(\sprintf('Loading configuration from %s...', $this->configPath)); $loader = $configProvider->fromPath($this->configPath); } else { - $this->output->info('Loading configuration from default location...'); + $this->logger->info('Loading configuration from default location...'); $loader = $configProvider->fromDefaultLocation(); } } catch (ConfigLoaderException $e) { @@ -83,19 +84,20 @@ public function __invoke(Container $container, DirectoriesInterface $dirs): int return Command::FAILURE; } + // Create the renderer for consistent output formatting + $renderer = new GenerateCommandRenderer($this->output); + + // Display summary header + $this->output->writeln(''); + foreach ($loader->load()->getItems() as $document) { - $this->output->info(\sprintf('Compiling %s...', $document->description)); + $this->logger->info(\sprintf('Compiling %s...', $document->description)); $compiledDocument = $compiler->compile($document); - if (!$compiledDocument->errors->hasErrors()) { - $this->output->success(\sprintf('Document compiled into %s', $document->outputPath)); - continue; - } - - $this->output->warning(\sprintf('Document compiled into %s with errors', $document->outputPath)); - $this->output->listing(\iterator_to_array($compiledDocument->errors)); + $renderer->renderCompilationResult($document, $compiledDocument); } + $this->output->writeln(''); return Command::SUCCESS; }, ); diff --git a/src/Console/Renderer/GenerateCommandRenderer.php b/src/Console/Renderer/GenerateCommandRenderer.php new file mode 100644 index 00000000..75d396a9 --- /dev/null +++ b/src/Console/Renderer/GenerateCommandRenderer.php @@ -0,0 +1,105 @@ +output instanceof SymfonyStyle); + + $hasErrors = $compiledDocument->errors->hasErrors(); + $description = $document->description; + $outputPath = $document->outputPath; + + // Calculate padding to align the document descriptions + $padding = $this->calculatePadding($description, $outputPath); + + if ($hasErrors) { + // Render warning line with document info + $this->output->writeln( + \sprintf( + ' %s %s [%s]%s', + $this->padRight(self::WARNING_SYMBOL, 1), + $description, + $outputPath, + $padding, + ), + ); + + // Render errors + foreach ($compiledDocument->errors as $error) { + $this->output->writeln(\sprintf(' %s %s', self::ERROR_SYMBOL, $error)); + } + + $this->output->newLine(); + } else { + // Render success line with document info + $this->output->writeln( + \sprintf( + ' %s %s [%s]%s', + $this->padRight(self::SUCCESS_SYMBOL, 2), + $description, + $outputPath, + $padding, + ), + ); + } + } + + /** + * Calculate padding to align the document information + */ + private function calculatePadding(string $description, string $outputPath): string + { + $totalLength = \strlen($description) + \strlen($outputPath) + 5; // 5 accounts for spaces and brackets + $padding = \max(0, self::MAX_LINE_WIDTH - $totalLength); + + return \str_repeat('.', $padding); + } + + /** + * Pad a string on the right with spaces + */ + private function padRight(string $text, int $length): string + { + return \str_pad($text, $length, ' ', \STR_PAD_RIGHT); + } +}