move config and version to each particular output formatter

This commit is contained in:
Tomas Votruba 2019-05-28 16:47:55 +02:00
parent d869606655
commit 2251b2797c
6 changed files with 135 additions and 40 deletions

View File

@ -90,7 +90,7 @@ final class RectorApplication
return;
}
if (! $this->symfonyStyle->isVerbose()) {
if (! $this->symfonyStyle->isVerbose() && $this->configuration->showProgressBar()) {
// why 3? one for each cycle, so user sees some activity all the time
$this->symfonyStyle->progressStart($fileCount * 3);
}
@ -172,7 +172,7 @@ final class RectorApplication
{
if ($this->symfonyStyle->isVerbose()) {
$this->symfonyStyle->writeln($smartFileInfo->getRealPath());
} else {
} elseif ($this->configuration->showProgressBar()) {
$this->symfonyStyle->progressAdvance();
}
}

View File

@ -2,7 +2,10 @@
namespace Rector\Configuration;
use Jean85\PrettyVersions;
use Rector\Console\Output\JsonOutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\PackageBuilder\Configuration\ConfigFileFinder;
final class Configuration
{
@ -28,6 +31,21 @@ final class Configuration
*/
private $source = [];
/**
* @var string
*/
private $outputFormat;
/**
* @var string|null
*/
private $configFilePath;
/**
* @var bool
*/
private $showProgressBar = true;
/**
* Needs to run in the start of the life cycle, since the rest of workflow uses it.
*/
@ -37,6 +55,40 @@ final class Configuration
$this->source = (array) $input->getArgument(Option::SOURCE);
$this->hideAutoloadErrors = (bool) $input->getOption(Option::HIDE_AUTOLOAD_ERRORS);
$this->withStyle = (bool) $input->getOption(Option::OPTION_WITH_STYLE);
$this->outputFormat = (string) $input->getOption(Option::OPTION_OUTPUT_FORMAT);
$this->showProgressBar = $this->canShowProgressBar($input);
}
public function setConfigFilePathFromInput(InputInterface $input): void
{
if ($input->getParameterOption('--config')) {
$this->configFilePath = $input->getParameterOption('--config');
return;
}
if ($input->getParameterOption('-c')) {
$this->configFilePath = $input->getParameterOption('-c');
return;
}
$this->configFilePath = ConfigFileFinder::provide('rector');
}
public function getConfigFilePath(): ?string
{
return $this->configFilePath;
}
public function getOutputFormat(): string
{
return $this->outputFormat;
}
public function getPrettyVersion(): string
{
$version = PrettyVersions::getVersion('rector/rector');
return $version->getPrettyVersion();
}
/**
@ -69,4 +121,14 @@ final class Configuration
{
return $this->withStyle;
}
public function showProgressBar(): bool
{
return $this->showProgressBar;
}
private function canShowProgressBar(InputInterface $input): bool
{
return $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== JsonOutputFormatter::NAME;
}
}

View File

@ -3,6 +3,8 @@
namespace Rector\Console;
use Jean85\PrettyVersions;
use Rector\Configuration\Configuration;
use Rector\Console\Output\JsonOutputFormatter;
use Rector\ContributorTools\Command\DumpNodesCommand;
use Rector\ContributorTools\Command\DumpRectorsCommand;
use Rector\ContributorTools\Exception\Command\ContributorCommandInterface;
@ -24,15 +26,21 @@ final class Application extends SymfonyApplication
*/
private const NAME = 'Rector';
/**
* @var Configuration
*/
private $configuration;
/**
* @param Command[] $commands
*/
public function __construct(array $commands = [])
public function __construct(Configuration $configuration, array $commands = [])
{
parent::__construct(self::NAME, PrettyVersions::getVersion('rector/rector')->getPrettyVersion());
$commands = $this->filterCommandsByScope($commands);
$this->addCommands($commands);
$this->configuration = $configuration;
}
/**
@ -45,6 +53,8 @@ final class Application extends SymfonyApplication
public function doRun(InputInterface $input, OutputInterface $output): int
{
$this->configuration->setConfigFilePathFromInput($input);
$shouldFollowByNewline = false;
// switch working dir
@ -64,16 +74,15 @@ final class Application extends SymfonyApplication
return parent::doRun($input, $output);
}
if (! $this->isVersionPrintedElsewhere($input)) {
// always print name version to more debug info
if ($this->shouldPrintMetaInformation($input)) {
$output->writeln($this->getLongVersion());
$shouldFollowByNewline = true;
}
$configPath = $this->getConfigPath($input);
if (file_exists($configPath)) {
$output->writeln('Config file: ' . realpath($configPath));
$shouldFollowByNewline = true;
$configPath = $this->getConfigPath($input);
if (file_exists($configPath)) {
$output->writeln('Config file: ' . realpath($configPath));
$shouldFollowByNewline = true;
}
}
if ($shouldFollowByNewline) {
@ -111,11 +120,6 @@ final class Application extends SymfonyApplication
return array_values($filteredCommands);
}
private function isVersionPrintedElsewhere(InputInterface $input): bool
{
return $input->hasParameterOption('--version') || $input->getFirstArgument() === null;
}
private function getConfigPath(InputInterface $input): string
{
if ($input->getParameterOption('--config')) {
@ -134,6 +138,19 @@ final class Application extends SymfonyApplication
$inputDefinition->setOptions($options);
}
private function shouldPrintMetaInformation(InputInterface $input): bool
{
$hasNoArguments = $input->getFirstArgument() === null;
$hasVersionOption = $input->hasParameterOption('--version');
$hasJsonOutput = (
$input->getParameterOption('--output-format') === JsonOutputFormatter::NAME ||
$input->getParameterOption('-o') === JsonOutputFormatter::NAME
);
return ! ($hasVersionOption || $hasNoArguments || $hasJsonOutput);
}
private function addCustomOptions(InputDefinition $inputDefinition): void
{
$inputDefinition->addOption(new InputOption(

View File

@ -17,16 +17,10 @@ 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;
final class ProcessCommand extends AbstractCommand
{
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
/**
* @var FilesFinder
*/
@ -76,7 +70,6 @@ final class ProcessCommand extends AbstractCommand
* @param string[] $fileExtensions
*/
public function __construct(
SymfonyStyle $symfonyStyle,
FilesFinder $phpFilesFinder,
AdditionalAutoloader $additionalAutoloader,
RectorGuard $rectorGuard,
@ -87,7 +80,6 @@ final class ProcessCommand extends AbstractCommand
OutputFormatterCollector $outputFormatterCollector,
array $fileExtensions
) {
$this->symfonyStyle = $symfonyStyle;
$this->filesFinder = $phpFilesFinder;
$this->additionalAutoloader = $additionalAutoloader;
$this->rectorGuard = $rectorGuard;
@ -140,7 +132,7 @@ final class ProcessCommand extends AbstractCommand
$availableOutputFormatters = $this->outputFormatterCollector->getNames();
$this->addOption(
Option::OPTION_OUTPUT_FORMAT,
null,
'o',
InputOption::VALUE_OPTIONAL,
sprintf('Select output format: "%s".', implode('", "', $availableOutputFormatters)),
ConsoleOutputFormatter::NAME
@ -149,12 +141,12 @@ final class ProcessCommand extends AbstractCommand
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->configuration->resolveFromInput($input);
$this->rectorGuard->ensureSomeRectorsAreRegistered();
$source = (array) $input->getArgument(Option::SOURCE);
$this->configuration->resolveFromInput($input);
$phpFileInfos = $this->filesFinder->findInDirectoriesAndFiles($source, $this->fileExtensions);
$this->additionalAutoloader->autoloadWithInputAndSource($input, $source);
@ -166,20 +158,15 @@ final class ProcessCommand extends AbstractCommand
$outputFormatter->report($this->errorAndDiffCollector);
if ($this->errorAndDiffCollector->getErrors() !== []) {
return Shell::CODE_ERROR;
}
// apply coding standard
if ($this->configuration->isWithStyle()) {
$this->afterRectorCodingStyle->apply($source);
}
$this->symfonyStyle->success(sprintf(
'Rector is done! %d changed files',
count(
$this->errorAndDiffCollector->getFileDiffs()
) + $this->errorAndDiffCollector->getRemovedAndAddedFilesCount()
));
// some errors were found → fail
if ($this->errorAndDiffCollector->getErrors() !== []) {
return Shell::CODE_ERROR;
}
// inverse error code for CI dry-run
if ($this->configuration->isDryRun() && count($this->errorAndDiffCollector->getFileDiffs())) {

View File

@ -29,6 +29,15 @@ final class ConsoleOutputFormatter implements OutputFormatterInterface
{
$this->reportFileDiffs($errorAndDiffCollector->getFileDiffs());
$this->reportErrors($errorAndDiffCollector->getErrors());
if ($errorAndDiffCollector->getErrors() !== []) {
return;
}
$this->symfonyStyle->success(sprintf(
'Rector is done! %d changed files',
count($errorAndDiffCollector->getFileDiffs()) + $errorAndDiffCollector->getRemovedAndAddedFilesCount()
));
}
public function getName(): string

View File

@ -4,32 +4,52 @@ namespace Rector\Console\Output;
use Nette\Utils\Json;
use Rector\Application\ErrorAndDiffCollector;
use Rector\Configuration\Configuration;
use Rector\Contract\Console\Output\OutputFormatterInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
final class JsonOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
public const NAME = 'json';
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(SymfonyStyle $symfonyStyle)
/**
* @var Configuration
*/
private $configuration;
public function __construct(SymfonyStyle $symfonyStyle, Configuration $configuration)
{
$this->symfonyStyle = $symfonyStyle;
$this->configuration = $configuration;
}
public function getName(): string
{
return 'json';
return self::NAME;
}
public function report(ErrorAndDiffCollector $errorAndDiffCollector): void
{
$fileDiffs = $errorAndDiffCollector->getFileDiffs();
$errorsArray = [];
$errorsArray['totals']['changed_files'] = count($fileDiffs);
$errorsArray = [
'meta' => [
'version' => $this->configuration->getPrettyVersion(),
'config' => $this->configuration->getConfigFilePath(),
],
'totals' => [
'changed_files' => count($fileDiffs),
'removed_and_added_files_count' => $errorAndDiffCollector->getRemovedAndAddedFilesCount(),
],
];
ksort($fileDiffs);