Skip to content

Commit

Permalink
Merge pull request #235 from temp/improve-output
Browse files Browse the repository at this point in the history
Improve console output of analyze command.
  • Loading branch information
Simon Mönch committed Jun 28, 2019
2 parents 870b37b + 5ba9c48 commit 1cde6aa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
20 changes: 16 additions & 4 deletions src/Console/Command/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Command\Command;
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\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -62,13 +63,20 @@ protected function configure(): void

$this->addArgument('depfile', InputArgument::OPTIONAL, 'Path to the depfile', getcwd().'/depfile.yml');
$this->getDefinition()->addOptions($this->formatterFactory->getFormatterOptions());
$this->addOption('no-banner', null, InputOption::VALUE_NONE, 'Do not show banner');
$this->addOption('no-progress', null, InputOption::VALUE_NONE, 'Do not show progress bar');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
ini_set('memory_limit', '-1');

$this->printBanner($output);
$noBanner = $input->getOption('no-banner');
$noProgress = $input->getOption('no-progress');

if (!$noBanner) {
$this->printBanner($output);
}

try {
$configuration = $this->configurationLoader->load($input->getArgument('depfile'));
Expand All @@ -78,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$this->dispatcher->addSubscriber(new ConsoleSubscriber($output));
$this->dispatcher->addSubscriber(new ConsoleSubscriber($output, $noProgress));

$astMap = $this->astRunner->createAstMapByFiles($this->fileResolver->resolve($configuration));

Expand Down Expand Up @@ -124,12 +132,16 @@ protected function printConfigMissingError(OutputInterface $output, string $file

protected function printCollectViolations(OutputInterface $output): void
{
$output->writeln('<info>collecting violations.</info>');
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<info>collecting violations.</info>');
}
}

protected function printFormattingStart(OutputInterface $output): void
{
$output->writeln('<info>formatting dependencies.</info>');
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<info>formatting dependencies.</info>');
}
}

protected function printFormatterException(OutputInterface $output, string $formatterName, \Exception $exception): void
Expand Down
4 changes: 2 additions & 2 deletions src/OutputFormatter/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public function finish(
if ($violationCount > $skippedViolationCount) {
$output->writeln(
sprintf(
"\nFound <error>%s Violations</error>".($skippedViolationCount ? ' and %s Violations skipped' : ''),
'Found <error>%s Violations</error>'.($skippedViolationCount ? ' and %s Violations skipped' : ''),
$violationCount - $skippedViolationCount,
$skippedViolationCount
)
);
} else {
$output->writeln(
sprintf(
"\nFound <info>%s Violations</info>".($skippedViolationCount ? ' and %s Violations skipped' : ''),
'Found <info>%s Violations</info>'.($skippedViolationCount ? ' and %s Violations skipped' : ''),
$violationCount - $skippedViolationCount,
$skippedViolationCount
)
Expand Down
45 changes: 33 additions & 12 deletions src/Subscriber/ConsoleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class ConsoleSubscriber implements EventSubscriberInterface
{
protected $output;

private $noProgress;

private $progressBar;

public function __construct(OutputInterface $output)
public function __construct(OutputInterface $output, $noProgress = false)
{
$this->output = $output;
$this->noProgress = $noProgress;
}

public static function getSubscribedEvents(): array
Expand All @@ -41,24 +44,34 @@ public static function getSubscribedEvents(): array

public function onPreCreateAstMapEvent(PreCreateAstMapEvent $preCreateAstMapEvent): void
{
$this->output->writeln(sprintf(
'Start to create an AstMap for <info>%u</info> Files.',
$preCreateAstMapEvent->getExpectedFileCount()
));
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln(
sprintf(
'Start to create an AstMap for <info>%u</info> Files.',
$preCreateAstMapEvent->getExpectedFileCount()
)
);
}

if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
if (!$this->noProgress && $this->output->getVerbosity() <= OutputInterface::VERBOSITY_VERBOSE) {
$this->progressBar = new ProgressBar($this->output, $preCreateAstMapEvent->getExpectedFileCount());
}
}

public function onPostCreateAstMapEvent(PostCreateAstMapEvent $postCreateAstMapEvent): void
{
$this->output->writeln("\nAstMap created.");
if ($this->progressBar instanceof ProgressBar) {
$this->progressBar->finish();
$this->progressBar->clear();
}
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln('AstMap created.');
}
}

public function onAstFileAnalyzedEvent(AstFileAnalyzedEvent $analyzedEvent): void
{
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln(sprintf('Parsing File %s', $analyzedEvent->getFile()->getPathname()));
} elseif ($this->progressBar instanceof ProgressBar) {
$this->progressBar->advance();
Expand All @@ -76,21 +89,29 @@ public function onAstFileSyntaxErrorEvent(AstFileSyntaxErrorEvent $astFileSyntax

public function onPreDependencyEmit(PreEmitEvent $event): void
{
$this->output->writeln(sprintf('start emitting dependencies <info>"%s"</info>', $event->getEmitterName()));
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$this->output->writeln(sprintf('start emitting dependencies <info>"%s"</info>', $event->getEmitterName()));
}
}

public function onPostDependencyEmit(): void
{
$this->output->writeln('<info>end emitting dependencies</info>');
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$this->output->writeln('<info>end emitting dependencies</info>');
}
}

public function onPreDependencyFlatten(): void
{
$this->output->writeln('<info>start flatten dependencies</info>');
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$this->output->writeln('<info>start flatten dependencies</info>');
}
}

public function onPostDependencyFlatten(): void
{
$this->output->writeln('<info>end flatten dependencies</info>');
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$this->output->writeln('<info>end flatten dependencies</info>');
}
}
}
16 changes: 4 additions & 12 deletions tests/Subscriber/ConsoleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@ public function testOnPreCreateAstMapEventWithVerboseVerbosity(): void
$dispatcher = new EventDispatcher();
$formatter = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$dispatcher->addSubscriber(new ConsoleSubscriber($formatter));
$dispatcher->addSubscriber(new ConsoleSubscriber($formatter, false));

$dispatcher->dispatch(PreCreateAstMapEvent::class, new PreCreateAstMapEvent(9999999));
static::assertStringContainsString('9999999', $formatter->fetch());
}

public function testOnPreCreateAstMapEventWithDefaultVerbosity(): void
{
$dispatcher = new EventDispatcher();
$formatter = new BufferedOutput();

$dispatcher->addSubscriber(new ConsoleSubscriber($formatter));
$result = $formatter->fetch();

$dispatcher->dispatch(PreCreateAstMapEvent::class, new PreCreateAstMapEvent(9999999));
static::assertStringContainsString('.', $formatter->fetch());
static::assertStringContainsString('9999999', $result);
static::assertStringContainsString('.', $result);
}
}

0 comments on commit 1cde6aa

Please sign in to comment.