Skip to content

Add warning and filter for missing files #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 59 additions & 31 deletions src/Command/CheckDocsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\RST\ErrorManager;
use Doctrine\RST\Event\PostNodeCreateEvent;
use Doctrine\RST\Meta\Metas;
use Symfony\CodeBlockChecker\Issue\IssueCollection;
use Symfony\CodeBlockChecker\Listener\CodeNodeCollector;
use Symfony\CodeBlockChecker\Service\Baseline;
use Symfony\CodeBlockChecker\Service\CodeNodeRunner;
Expand Down Expand Up @@ -81,22 +82,9 @@ protected function initialize(InputInterface $input, OutputInterface $output)

protected function execute(InputInterface $input, OutputInterface $output): int
{
$files = $input->getArgument('files');
if ([] === $files) {
$files = $this->findFiles($input->getArgument('source-dir'));
}

$parseQueue = new ParseQueue();
foreach ($files as $filename) {
// Remove ".rst"
if ('.rst' === substr($filename, -4)) {
$filename = substr($filename, 0, -4);
}
$parseQueue->addFile(ltrim($filename, '/'), true);
}

// This will collect all CodeNodes
$this->queueProcessor->process($parseQueue);
$this->queueProcessor->process($this->prepareParseQueue($input));

// Verify code blocks
$issues = $this->validator->validateNodes($this->collector->getNodes());
if ($applicationDir = $input->getOption('symfony-application')) {
Expand All @@ -119,25 +107,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$issues = $this->baseline->filter($issues, $baseline);
}

$issueCount = count($issues);
if ($issueCount > 0) {
$format = $input->getOption('output-format');
if ('console' === $format) {
foreach ($issues as $issue) {
$this->io->writeln($issue->__toString());
}

$this->io->error(sprintf('Build completed with %s errors', $issueCount));
} elseif ('github' === $format) {
foreach ($issues as $issue) {
// We use urlencoded '\n'
$text = str_replace(PHP_EOL, '%0A', $issue->getText());
$this->io->writeln(sprintf('::error file=%s,line=%s::[%s] %s', $issue->getFile(), $issue->getLine(), $issue->getType(), $text));
}
}
if (count($issues) > 0) {
$this->outputIssue($input->getOption('output-format'), $issues);

return Command::FAILURE;
}

$this->io->success('Build completed successfully!');

return Command::SUCCESS;
Expand All @@ -155,4 +130,57 @@ private function findFiles(string $directory): array

return $files;
}

private function prepareParseQueue(InputInterface $input): ParseQueue
{
$sourceDirectory = $input->getArgument('source-dir');
$files = $input->getArgument('files');
if ([] === $files) {
$files = $this->findFiles($sourceDirectory);
} else {
foreach ($files as $i => $file) {
if (!file_exists($sourceDirectory.DIRECTORY_SEPARATOR.$file)) {
unset($files[$i]);
$this->outputWarning($input->getOption('output-format'), sprintf('Could not find file "%s"', $file));
}
}
}

$parseQueue = new ParseQueue();
foreach ($files as $filename) {
// Remove ".rst"
if ('.rst' === substr($filename, -4)) {
$filename = substr($filename, 0, -4);
}
$parseQueue->addFile(ltrim($filename, '/'), true);
}

return $parseQueue;
}

private function outputWarning(string $format, string $text): void
{
if ('console' === $format) {
$this->io->warning($text);
} elseif ('github' === $format) {
$this->io->writeln('::warning::'.$text);
}
}

private function outputIssue(string $format, IssueCollection $issues): void
{
if ('console' === $format) {
foreach ($issues as $issue) {
$this->io->writeln($issue->__toString());
}

$this->io->error(sprintf('Build completed with %s errors', $issues->count()));
} elseif ('github' === $format) {
foreach ($issues as $issue) {
// We use urlencoded '\n'
$text = str_replace(PHP_EOL, '%0A', $issue->getText());
$this->io->writeln(sprintf('::error file=%s,line=%s::[%s] %s', $issue->getFile(), $issue->getLine(), $issue->getType(), $text));
}
}
}
}
2 changes: 1 addition & 1 deletion src/Listener/CodeNodeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class CodeNodeCollector
{
private array $nodes;
private array $nodes = [];

public function getNodes(): array
{
Expand Down