Dislay file names in debug mode, merge ConsoleStyle to SymfonyStyle

This commit is contained in:
Tomas Votruba 2018-11-02 18:27:00 +01:00
parent db680aabed
commit 3c4a110f6a
8 changed files with 87 additions and 93 deletions

View File

@ -3,6 +3,8 @@
use Psr\Container\ContainerInterface;
use Rector\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\PackageBuilder\Console\ThrowableRenderer;
// Performance boost
@ -15,8 +17,12 @@ try {
/** @var ContainerInterface $container */
$container = require_once __DIR__ . '/container.php';
/** this calls @see \Symfony\Component\Console\Application::configureIO() and configure Input + Output services */
$input = $container->get(InputInterface::class);
$output = $container->get(OutputInterface::class);
$application = $container->get(Application::class);
exit($application->run());
exit($application->run($input, $output));
} catch (Throwable $throwable) {
(new ThrowableRenderer())->render($throwable);
exit($throwable->getCode());

View File

@ -5,13 +5,13 @@ namespace Rector\ContributorTools\Command;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Rector\CodingStyle\AfterRectorCodingStyle;
use Rector\Console\ConsoleStyle;
use Rector\ContributorTools\Configuration\Configuration;
use Rector\ContributorTools\Configuration\ConfigurationFactory;
use Rector\ContributorTools\TemplateVariablesFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
@ -44,9 +44,9 @@ final class CreateRectorCommand extends Command
private $generatedFiles = [];
/**
* @var ConsoleStyle
* @var SymfonyStyle
*/
private $consoleStyle;
private $symfonyStyle;
/**
* @var ConfigurationFactory
@ -69,14 +69,14 @@ final class CreateRectorCommand extends Command
private $templateVariablesFactory;
public function __construct(
ConsoleStyle $consoleStyle,
SymfonyStyle $symfonyStyle,
ConfigurationFactory $configurationFactory,
FinderSanitizer $finderSanitizer,
AfterRectorCodingStyle $afterRectorCodingStyle,
TemplateVariablesFactory $templateVariablesFactory
) {
parent::__construct();
$this->consoleStyle = $consoleStyle;
$this->symfonyStyle = $symfonyStyle;
$this->configurationFactory = $configurationFactory;
$this->finderSanitizer = $finderSanitizer;
$this->afterRectorCodingStyle = $afterRectorCodingStyle;
@ -190,11 +190,11 @@ final class CreateRectorCommand extends Command
private function printSuccess(string $name): void
{
$this->consoleStyle->title(sprintf('New files generated for "%s"', $name));
$this->symfonyStyle->title(sprintf('New files generated for "%s"', $name));
sort($this->generatedFiles);
$this->consoleStyle->listing($this->generatedFiles);
$this->symfonyStyle->listing($this->generatedFiles);
$this->consoleStyle->success(sprintf(
$this->symfonyStyle->success(sprintf(
'Now make these tests green again:%svendor/bin/phpunit %s',
PHP_EOL . PHP_EOL,
$this->testCasePath

View File

@ -4,7 +4,6 @@ namespace Rector\Console\Command;
use Nette\Loaders\RobotLoader;
use Nette\Utils\Strings;
use Rector\Console\ConsoleStyle;
use Rector\Console\Shell;
use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
use Rector\Contract\Rector\RectorInterface;
@ -14,6 +13,7 @@ use ReflectionClass;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use function Safe\ksort;
@ -22,20 +22,20 @@ use function Safe\sprintf;
final class GenerateRectorOverviewCommand extends Command
{
/**
* @var ConsoleStyle
* @var SymfonyStyle
*/
private $consoleStyle;
private $symfonyStyle;
/**
* @var MarkdownDifferAndFormatter
*/
private $markdownDifferAndFormatter;
public function __construct(ConsoleStyle $consoleStyle, MarkdownDifferAndFormatter $markdownDifferAndFormatter)
public function __construct(SymfonyStyle $symfonyStyle, MarkdownDifferAndFormatter $markdownDifferAndFormatter)
{
parent::__construct();
$this->consoleStyle = $consoleStyle;
$this->symfonyStyle = $symfonyStyle;
$this->markdownDifferAndFormatter = $markdownDifferAndFormatter;
}
@ -47,23 +47,23 @@ final class GenerateRectorOverviewCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->consoleStyle->writeln('# All Rectors Overview');
$this->consoleStyle->newLine();
$this->symfonyStyle->writeln('# All Rectors Overview');
$this->symfonyStyle->newLine();
$this->consoleStyle->writeln('- [Projects](#projects)');
$this->consoleStyle->writeln('- [General](#general)');
$this->consoleStyle->newLine();
$this->symfonyStyle->writeln('- [Projects](#projects)');
$this->symfonyStyle->writeln('- [General](#general)');
$this->symfonyStyle->newLine();
$this->consoleStyle->writeln('## Projects');
$this->consoleStyle->newLine();
$this->symfonyStyle->writeln('## Projects');
$this->symfonyStyle->newLine();
$rectorsByGroup = $this->groupRectors($this->getProjectsRectors());
$this->printRectorsByGroup($rectorsByGroup);
$this->consoleStyle->writeln('---');
$this->symfonyStyle->writeln('---');
$this->consoleStyle->writeln('## General');
$this->consoleStyle->newLine();
$this->symfonyStyle->writeln('## General');
$this->symfonyStyle->newLine();
$rectorsByGroup = $this->groupRectors($this->getGeneralRectors());
$this->printRectorsByGroup($rectorsByGroup);
@ -108,8 +108,8 @@ final class GenerateRectorOverviewCommand extends Command
$this->printGroupsMenu($rectorsByGroup);
foreach ($rectorsByGroup as $group => $rectors) {
$this->consoleStyle->writeln('## ' . $group);
$this->consoleStyle->newLine();
$this->symfonyStyle->writeln('## ' . $group);
$this->symfonyStyle->newLine();
foreach ($rectors as $rector) {
$this->printRector($rector);
@ -206,28 +206,28 @@ final class GenerateRectorOverviewCommand extends Command
$escapedGroup = str_replace('\\', '', $group);
$escapedGroup = Strings::webalize($escapedGroup, '_');
$this->consoleStyle->writeln(sprintf('- [%s](#%s)', $group, $escapedGroup));
$this->symfonyStyle->writeln(sprintf('- [%s](#%s)', $group, $escapedGroup));
}
$this->consoleStyle->newLine();
$this->symfonyStyle->newLine();
}
private function printRector(RectorInterface $rector): void
{
$headline = $this->getRectorClassWithoutNamespace($rector);
$this->consoleStyle->writeln(sprintf('### `%s`', $headline));
$this->symfonyStyle->writeln(sprintf('### `%s`', $headline));
$this->consoleStyle->newLine();
$this->consoleStyle->writeln(sprintf('- class: `%s`', get_class($rector)));
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln(sprintf('- class: `%s`', get_class($rector)));
$rectorDefinition = $rector->getDefinition();
if ($rectorDefinition->getDescription()) {
$this->consoleStyle->newLine();
$this->consoleStyle->writeln($rectorDefinition->getDescription());
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln($rectorDefinition->getDescription());
}
foreach ($rectorDefinition->getCodeSamples() as $codeSample) {
$this->consoleStyle->newLine();
$this->symfonyStyle->newLine();
if ($codeSample instanceof ConfiguredCodeSample) {
$configuration = [
@ -240,9 +240,9 @@ final class GenerateRectorOverviewCommand extends Command
$this->printCodeWrapped($configuration, 'yaml');
$this->consoleStyle->newLine();
$this->consoleStyle->writeln('↓');
$this->consoleStyle->newLine();
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln('↓');
$this->symfonyStyle->newLine();
}
$diff = $this->markdownDifferAndFormatter->bareDiffAndFormatWithoutColors(
@ -252,7 +252,7 @@ final class GenerateRectorOverviewCommand extends Command
$this->printCodeWrapped($diff, 'diff');
}
$this->consoleStyle->newLine(1);
$this->symfonyStyle->newLine(1);
}
private function getRectorClassWithoutNamespace(RectorInterface $rector): string
@ -265,6 +265,6 @@ final class GenerateRectorOverviewCommand extends Command
private function printCodeWrapped(string $content, string $format): void
{
$this->consoleStyle->writeln(sprintf('```%s%s%s%s```', $format, PHP_EOL, rtrim($content), PHP_EOL));
$this->symfonyStyle->writeln(sprintf('```%s%s%s%s```', $format, PHP_EOL, rtrim($content), PHP_EOL));
}
}

View File

@ -3,12 +3,12 @@
namespace Rector\Console\Command;
use Nette\Utils\Strings;
use Rector\Console\ConsoleStyle;
use Rector\Console\Shell;
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\Finder\Finder;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use function Safe\sort;
@ -17,13 +17,13 @@ use function Safe\sprintf;
final class LevelsCommand extends Command
{
/**
* @var ConsoleStyle
* @var SymfonyStyle
*/
private $consoleStyle;
private $symfonyStyle;
public function __construct(ConsoleStyle $consoleStyle)
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->consoleStyle = $consoleStyle;
$this->symfonyStyle = $symfonyStyle;
parent::__construct();
}
@ -43,8 +43,8 @@ final class LevelsCommand extends Command
$levels = $this->filterLevelsByName($input, $levels);
}
$this->consoleStyle->title(sprintf('%d available levels:', count($levels)));
$this->consoleStyle->listing($levels);
$this->symfonyStyle->title(sprintf('%d available levels:', count($levels)));
$this->symfonyStyle->listing($levels);
return Shell::CODE_SUCCESS;
}

View File

@ -10,7 +10,6 @@ use Rector\Application\FileProcessor;
use Rector\Autoloading\AdditionalAutoloader;
use Rector\CodingStyle\AfterRectorCodingStyle;
use Rector\Configuration\Option;
use Rector\Console\ConsoleStyle;
use Rector\Console\Output\ProcessCommandReporter;
use Rector\Console\Shell;
use Rector\ConsoleDiffer\DifferAndFormatter;
@ -24,6 +23,7 @@ use Symfony\Component\Console\Input\InputArgument;
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 Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
@ -48,9 +48,9 @@ final class ProcessCommand extends Command
private $fileProcessor;
/**
* @var ConsoleStyle
* @var SymfonyStyle
*/
private $consoleStyle;
private $symfonyStyle;
/**
* @var FilesFinder
@ -104,7 +104,7 @@ final class ProcessCommand extends Command
public function __construct(
FileProcessor $fileProcessor,
ConsoleStyle $consoleStyle,
SymfonyStyle $symfonyStyle,
FilesFinder $phpFilesFinder,
ProcessCommandReporter $processCommandReporter,
ParameterProvider $parameterProvider,
@ -119,7 +119,7 @@ final class ProcessCommand extends Command
parent::__construct();
$this->fileProcessor = $fileProcessor;
$this->consoleStyle = $consoleStyle;
$this->symfonyStyle = $symfonyStyle;
$this->filesFinder = $phpFilesFinder;
$this->processCommandReporter = $processCommandReporter;
$this->parameterProvider = $parameterProvider;
@ -199,7 +199,7 @@ final class ProcessCommand extends Command
$this->afterRectorCodingStyle->apply($source);
}
$this->consoleStyle->success('Rector is done!');
$this->symfonyStyle->success('Rector is done!');
return Shell::CODE_SUCCESS;
}
@ -210,15 +210,22 @@ final class ProcessCommand extends Command
private function processFileInfos(array $fileInfos, bool $shouldHideAutoloadErrors): void
{
$totalFiles = count($fileInfos);
$this->consoleStyle->title(sprintf('Processing %d file%s', $totalFiles, $totalFiles === 1 ? '' : 's'));
$this->consoleStyle->progressStart($totalFiles);
$this->symfonyStyle->title(sprintf('Processing %d file%s', $totalFiles, $totalFiles === 1 ? '' : 's'));
if (! $this->symfonyStyle->isVerbose()) {
$this->symfonyStyle->progressStart($totalFiles);
}
foreach ($fileInfos as $fileInfo) {
$this->processFileInfo($fileInfo, $shouldHideAutoloadErrors);
$this->consoleStyle->progressAdvance();
if ($this->symfonyStyle->isVerbose()) {
$this->symfonyStyle->writeln($fileInfo->getRealPath());
} else {
$this->symfonyStyle->progressAdvance();
}
}
$this->consoleStyle->newLine(2);
$this->symfonyStyle->newLine(2);
}
private function processFileInfo(SmartFileInfo $fileInfo, bool $shouldHideAutoloadErrors): void

View File

@ -1,21 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Console;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Safe\sprintf;
final class ConsoleStyle extends SymfonyStyle
{
/**
* @param string[] $elements
*/
public function listing(array $elements): void
{
$elements = array_map(function ($element) {
return sprintf(' - %s', $element);
}, $elements);
$this->writeln($elements);
$this->newLine();
}
}

View File

@ -3,19 +3,19 @@
namespace Rector\Console\Output;
use Rector\Application\Error;
use Rector\Console\ConsoleStyle;
use Rector\Contract\Rector\RectorInterface;
use Rector\NodeTraverser\RectorNodeTraverser;
use Rector\Reporting\FileDiff;
use Rector\YamlRector\YamlFileProcessor;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Safe\sprintf;
final class ProcessCommandReporter
{
/**
* @var ConsoleStyle
* @var SymfonyStyle
*/
private $consoleStyle;
private $symfonyStyle;
/**
* @var RectorNodeTraverser
@ -29,10 +29,10 @@ final class ProcessCommandReporter
public function __construct(
RectorNodeTraverser $rectorNodeTraverser,
ConsoleStyle $consoleStyle,
SymfonyStyle $symfonyStyle,
YamlFileProcessor $yamlFileProcessor
) {
$this->consoleStyle = $consoleStyle;
$this->symfonyStyle = $symfonyStyle;
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->yamlFileProcessor = $yamlFileProcessor;
}
@ -41,7 +41,7 @@ final class ProcessCommandReporter
{
$rectorCount = $this->rectorNodeTraverser->getRectorCount() + $this->yamlFileProcessor->getYamlRectorsCount();
$this->consoleStyle->title(sprintf('%d Loaded Rector%s', $rectorCount, $rectorCount === 1 ? '' : 's'));
$this->symfonyStyle->title(sprintf('%d Loaded Rector%s', $rectorCount, $rectorCount === 1 ? '' : 's'));
$allRectors = array_merge(
$this->rectorNodeTraverser->getRectors() + $this->yamlFileProcessor->getYamlRectors()
@ -51,7 +51,7 @@ final class ProcessCommandReporter
return get_class($rector);
}, $allRectors);
$this->consoleStyle->listing($rectorClasses);
$this->symfonyStyle->listing($rectorClasses);
}
/**
@ -63,10 +63,10 @@ final class ProcessCommandReporter
return;
}
$this->consoleStyle->title(
$this->symfonyStyle->title(
sprintf('%d Changed file%s', count($changedFiles), count($changedFiles) === 1 ? '' : 's')
);
$this->consoleStyle->listing($changedFiles);
$this->symfonyStyle->listing($changedFiles);
}
/**
@ -78,16 +78,16 @@ final class ProcessCommandReporter
return;
}
$this->consoleStyle->title(
$this->symfonyStyle->title(
sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's')
);
$i = 0;
foreach ($fileDiffs as $fileDiff) {
$this->consoleStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $fileDiff->getFile()));
$this->consoleStyle->newLine();
$this->consoleStyle->writeln($fileDiff->getDiff());
$this->consoleStyle->newLine();
$this->symfonyStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $fileDiff->getFile()));
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln($fileDiff->getDiff());
$this->symfonyStyle->newLine();
}
}
@ -108,7 +108,7 @@ final class ProcessCommandReporter
$message .= ' On line: ' . $error->getLine();
}
$this->consoleStyle->error($message);
$this->symfonyStyle->error($message);
}
}
}

View File

@ -22,9 +22,11 @@ services:
# Symfony\Console
Symfony\Component\Console\Style\SymfonyStyle: ~
Symfony\Component\Console\Input\ArgvInput: ~
Symfony\Component\Console\Input\InputInterface:
alias: 'Symfony\Component\Console\Input\ArgvInput'
Symfony\Component\Console\Output\ConsoleOutput: ~
Symfony\Component\Console\Output\OutputInterface:
alias: Symfony\Component\Console\Output\ConsoleOutput
alias: 'Symfony\Component\Console\Output\ConsoleOutput'
Symplify\PackageBuilder\FileSystem\FileSystem: ~
Symplify\PackageBuilder\FileSystem\FinderSanitizer: ~