Skip to content

Commit cf632a4

Browse files
committed
Adding the ability to exclude paths through a docs.json file
Depends on doctrine/rst-parser#122
1 parent b6a3444 commit cf632a4

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

src/BuildContext.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace SymfonyDocsBuilder;
1111

1212
use Doctrine\RST\Configuration;
13+
use Symfony\Component\Finder\Finder;
1314

1415
class BuildContext
1516
{
@@ -25,6 +26,8 @@ class BuildContext
2526
private $disableCache = false;
2627
private $theme;
2728
private $cacheDirectory;
29+
private $excludedPaths = [];
30+
private $fileFinder;
2831

2932
public function __construct(
3033
string $symfonyVersion,
@@ -121,6 +124,32 @@ public function setCacheDirectory(string $cacheDirectory)
121124
$this->cacheDirectory = $cacheDirectory;
122125
}
123126

127+
public function setExcludedPaths(array $excludedPaths)
128+
{
129+
if (null !== $this->fileFinder) {
130+
throw new \LogicException('setExcludePaths() cannot be called after getFileFinder() (because the Finder has been initialized).');
131+
}
132+
133+
$this->excludedPaths = $excludedPaths;
134+
}
135+
136+
public function createFileFinder(): Finder
137+
{
138+
if (null === $this->fileFinder) {
139+
$this->fileFinder = new Finder();
140+
$this->fileFinder
141+
->in($this->getSourceDir())
142+
// TODO - read this from the rst-parser Configuration
143+
->name('*.rst')
144+
->notName('*.rst.inc')
145+
->files()
146+
->exclude($this->excludedPaths);
147+
}
148+
149+
// clone to get a fresh instance and not share state
150+
return clone $this->fileFinder;
151+
}
152+
124153
private function checkThatRuntimeConfigIsInitialized()
125154
{
126155
if (false === $this->runtimeInitialized) {

src/CI/MissingFilesChecker.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@
1717

1818
class MissingFilesChecker
1919
{
20-
private $finder;
2120
private $filesystem;
2221
private $buildContext;
2322

2423
public function __construct(BuildContext $buildContext)
2524
{
26-
$this->finder = new Finder();
2725
$this->filesystem = new Filesystem();
2826
$this->buildContext = $buildContext;
2927
}
3028

3129
public function getMissingFiles(): array
3230
{
33-
$this->finder->in($this->buildContext->getSourceDir())
34-
->exclude(['_build', '.github', '.platform', '_images'])
35-
->notName('*.rst.inc')
36-
->files()
37-
->name('*.rst');
31+
$finder = $this->buildContext->createFileFinder();
3832

3933
$orphanedFiles = [];
4034

41-
foreach ($this->finder as $file) {
35+
foreach ($finder as $file) {
4236
$sourcePath = ltrim(substr($file->getPathname(), \strlen($this->buildContext->getSourceDir())), '/');
4337

4438
$htmlFile = sprintf(

src/Command/BuildDocsCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Filesystem\Filesystem;
2525
use SymfonyDocsBuilder\BuildContext;
2626
use SymfonyDocsBuilder\CI\MissingFilesChecker;
27+
use SymfonyDocsBuilder\ConfigFileParser;
2728
use SymfonyDocsBuilder\Generator\HtmlForPdfGenerator;
2829
use SymfonyDocsBuilder\Generator\JsonGenerator;
2930
use SymfonyDocsBuilder\KernelFactory;
@@ -120,6 +121,9 @@ protected function initialize(InputInterface $input, OutputInterface $output)
120121
$input->getOption('disable-cache'),
121122
$input->getOption('no-theme') ? Configuration::THEME_DEFAULT : 'rtd'
122123
);
124+
125+
$configFileParser = new ConfigFileParser($this->buildContext, $output);
126+
$configFileParser->processConfigFile($sourceDir);
123127
}
124128

125129
protected function execute(InputInterface $input, OutputInterface $output)

src/ConfigFileParser.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace SymfonyDocsBuilder;
4+
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
/**
8+
* Parses the docs.json config file
9+
*/
10+
class ConfigFileParser
11+
{
12+
private $buildContext;
13+
private $output;
14+
15+
public function __construct(BuildContext $buildContext, OutputInterface $output)
16+
{
17+
$this->buildContext = $buildContext;
18+
$this->output = $output;
19+
}
20+
21+
public function processConfigFile(string $sourceDir): void
22+
{
23+
$configPath = $sourceDir.'/docs.json';
24+
if (!file_exists($configPath)) {
25+
$this->output->writeln(sprintf('No config file present at <info>%s</info>', $configPath));
26+
27+
return;
28+
}
29+
30+
$this->output->writeln(sprintf('Loading config file: <info>%s</info>', $configPath));
31+
$configData = json_decode(file_get_contents($configPath), true);
32+
33+
$exclude = $configData['exclude'] ?? [];
34+
$this->buildContext->setExcludedPaths($exclude);
35+
unset($configData['exclude']);
36+
37+
if (count($configData) > 0) {
38+
throw new \Exception(sprintf('Unsupported keys in docs.json: %s', implode(', ', array_keys($configData))));
39+
}
40+
}
41+
}

src/DocsKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function initBuilder(Builder $builder): void
3636
$builder->getConfiguration()->getEventManager(),
3737
$builder->getErrorManager()
3838
);
39+
40+
$builder->setScannerFinder($this->buildContext->createFileFinder());
3941
}
4042

4143
private function initializeListeners(EventManager $eventManager, ErrorManager $errorManager)

0 commit comments

Comments
 (0)