Skip to content

Commit 22f0669

Browse files
authored
Add warning and filter for missing files (#30)
* Add warning and filter for missing files * cs
1 parent 88e5c10 commit 22f0669

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

src/Command/CheckDocsCommand.php

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\RST\ErrorManager;
1111
use Doctrine\RST\Event\PostNodeCreateEvent;
1212
use Doctrine\RST\Meta\Metas;
13+
use Symfony\CodeBlockChecker\Issue\IssueCollection;
1314
use Symfony\CodeBlockChecker\Listener\CodeNodeCollector;
1415
use Symfony\CodeBlockChecker\Service\Baseline;
1516
use Symfony\CodeBlockChecker\Service\CodeNodeRunner;
@@ -81,22 +82,9 @@ protected function initialize(InputInterface $input, OutputInterface $output)
8182

8283
protected function execute(InputInterface $input, OutputInterface $output): int
8384
{
84-
$files = $input->getArgument('files');
85-
if ([] === $files) {
86-
$files = $this->findFiles($input->getArgument('source-dir'));
87-
}
88-
89-
$parseQueue = new ParseQueue();
90-
foreach ($files as $filename) {
91-
// Remove ".rst"
92-
if ('.rst' === substr($filename, -4)) {
93-
$filename = substr($filename, 0, -4);
94-
}
95-
$parseQueue->addFile(ltrim($filename, '/'), true);
96-
}
97-
9885
// This will collect all CodeNodes
99-
$this->queueProcessor->process($parseQueue);
86+
$this->queueProcessor->process($this->prepareParseQueue($input));
87+
10088
// Verify code blocks
10189
$issues = $this->validator->validateNodes($this->collector->getNodes());
10290
if ($applicationDir = $input->getOption('symfony-application')) {
@@ -119,25 +107,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
119107
$issues = $this->baseline->filter($issues, $baseline);
120108
}
121109

122-
$issueCount = count($issues);
123-
if ($issueCount > 0) {
124-
$format = $input->getOption('output-format');
125-
if ('console' === $format) {
126-
foreach ($issues as $issue) {
127-
$this->io->writeln($issue->__toString());
128-
}
129-
130-
$this->io->error(sprintf('Build completed with %s errors', $issueCount));
131-
} elseif ('github' === $format) {
132-
foreach ($issues as $issue) {
133-
// We use urlencoded '\n'
134-
$text = str_replace(PHP_EOL, '%0A', $issue->getText());
135-
$this->io->writeln(sprintf('::error file=%s,line=%s::[%s] %s', $issue->getFile(), $issue->getLine(), $issue->getType(), $text));
136-
}
137-
}
110+
if (count($issues) > 0) {
111+
$this->outputIssue($input->getOption('output-format'), $issues);
138112

139113
return Command::FAILURE;
140114
}
115+
141116
$this->io->success('Build completed successfully!');
142117

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

156131
return $files;
157132
}
133+
134+
private function prepareParseQueue(InputInterface $input): ParseQueue
135+
{
136+
$sourceDirectory = $input->getArgument('source-dir');
137+
$files = $input->getArgument('files');
138+
if ([] === $files) {
139+
$files = $this->findFiles($sourceDirectory);
140+
} else {
141+
foreach ($files as $i => $file) {
142+
if (!file_exists($sourceDirectory.DIRECTORY_SEPARATOR.$file)) {
143+
unset($files[$i]);
144+
$this->outputWarning($input->getOption('output-format'), sprintf('Could not find file "%s"', $file));
145+
}
146+
}
147+
}
148+
149+
$parseQueue = new ParseQueue();
150+
foreach ($files as $filename) {
151+
// Remove ".rst"
152+
if ('.rst' === substr($filename, -4)) {
153+
$filename = substr($filename, 0, -4);
154+
}
155+
$parseQueue->addFile(ltrim($filename, '/'), true);
156+
}
157+
158+
return $parseQueue;
159+
}
160+
161+
private function outputWarning(string $format, string $text): void
162+
{
163+
if ('console' === $format) {
164+
$this->io->warning($text);
165+
} elseif ('github' === $format) {
166+
$this->io->writeln('::warning::'.$text);
167+
}
168+
}
169+
170+
private function outputIssue(string $format, IssueCollection $issues): void
171+
{
172+
if ('console' === $format) {
173+
foreach ($issues as $issue) {
174+
$this->io->writeln($issue->__toString());
175+
}
176+
177+
$this->io->error(sprintf('Build completed with %s errors', $issues->count()));
178+
} elseif ('github' === $format) {
179+
foreach ($issues as $issue) {
180+
// We use urlencoded '\n'
181+
$text = str_replace(PHP_EOL, '%0A', $issue->getText());
182+
$this->io->writeln(sprintf('::error file=%s,line=%s::[%s] %s', $issue->getFile(), $issue->getLine(), $issue->getType(), $text));
183+
}
184+
}
185+
}
158186
}

src/Listener/CodeNodeCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
class CodeNodeCollector
1616
{
17-
private array $nodes;
17+
private array $nodes = [];
1818

1919
public function getNodes(): array
2020
{

0 commit comments

Comments
 (0)