Skip to content

Commit bdc1250

Browse files
committed
Allow excluding paths from the source directory
(cherry picked from commit ce89264)
1 parent 418625e commit bdc1250

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

packages/guides-cli/src/Command/Run.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
namespace phpDocumentor\Guides\Cli\Command;
1515

1616
use Flyfinder\Finder;
17+
use Flyfinder\Path;
18+
use Flyfinder\Specification\InPath;
19+
use Flyfinder\Specification\NotSpecification;
20+
use Flyfinder\Specification\OrSpecification;
21+
use Flyfinder\Specification\SpecificationInterface;
1722
use League\Flysystem\Adapter\Local;
1823
use League\Flysystem\Filesystem;
1924
use League\Tactician\CommandBus;
@@ -51,7 +56,10 @@
5156
use Symfony\Component\Console\Output\OutputInterface;
5257
use Symfony\Component\EventDispatcher\EventDispatcher;
5358

59+
use function array_map;
5460
use function array_pop;
61+
use function array_reduce;
62+
use function array_shift;
5563
use function assert;
5664
use function count;
5765
use function implode;
@@ -93,6 +101,13 @@ public function __construct(
93101
'If set, only the specified file is parsed, relative to the directory specified in "input"',
94102
);
95103

104+
$this->addOption(
105+
'exclude-path',
106+
null,
107+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
108+
'Paths to exclude, doc files in these directories will not be parsed',
109+
);
110+
96111
$this->addOption(
97112
'input-format',
98113
null,
@@ -314,12 +329,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
314329
}
315330

316331
if ($settings->getInputFile() === '') {
332+
$exclude = null;
333+
if ($input->getOption('exclude-path')) {
334+
/** @var string[] $excludedPaths */
335+
$excludedPaths = (array) $input->getOption('exclude-path');
336+
$excludedSpecifications = array_map(static fn (string $path) => new NotSpecification(new InPath(new Path($path))), $excludedPaths);
337+
$excludedSpecification = array_shift($excludedSpecifications);
338+
assert($excludedSpecification !== null);
339+
340+
$exclude = array_reduce(
341+
$excludedSpecifications,
342+
static fn (SpecificationInterface $carry, SpecificationInterface $spec) => new OrSpecification($carry, $spec),
343+
$excludedSpecification,
344+
);
345+
}
346+
317347
$documents = $this->commandBus->handle(
318348
new ParseDirectoryCommand(
319349
$sourceFileSystem,
320350
'',
321351
$settings->getInputFormat(),
322352
$projectNode,
353+
$exclude,
323354
),
324355
);
325356
} else {

packages/guides/src/FileCollector.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Flyfinder\Specification\AndSpecification;
1818
use Flyfinder\Specification\HasExtension;
1919
use Flyfinder\Specification\InPath;
20+
use Flyfinder\Specification\NotSpecification;
21+
use Flyfinder\Specification\SpecificationInterface;
2022
use InvalidArgumentException;
2123
use League\Flysystem\FilesystemInterface;
2224

@@ -36,14 +38,19 @@ final class FileCollector
3638
* This takes into account the presence of cached & fresh MetaEntry
3739
* objects, and avoids adding files to the parse queue that have
3840
* not changed and whose direct dependencies have not changed.
41+
*
42+
* @param SpecificationInterface|null $excludedSpecification specification that is used to exclude specific files/directories
3943
*/
40-
public function collect(FilesystemInterface $filesystem, string $directory, string $extension): Files
44+
public function collect(FilesystemInterface $filesystem, string $directory, string $extension, SpecificationInterface|null $excludedSpecification = null): Files
4145
{
4246
$directory = trim($directory, '/');
47+
$specification = new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension]));
48+
if ($excludedSpecification) {
49+
$specification = new AndSpecification($specification, new NotSpecification($excludedSpecification));
50+
}
51+
4352
/** @var array<array<string>> $files */
44-
$files = $filesystem->find(
45-
new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension])),
46-
);
53+
$files = $filesystem->find($specification);
4754

4855
// completely populate the splFileInfos property
4956
$this->fileInfos = [];

packages/guides/src/Handlers/ParseDirectoryCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Guides\Handlers;
1515

16+
use Flyfinder\Specification\SpecificationInterface;
1617
use League\Flysystem\FilesystemInterface;
1718
use phpDocumentor\Guides\Nodes\ProjectNode;
1819

@@ -23,6 +24,7 @@ public function __construct(
2324
private readonly string $directory,
2425
private readonly string $inputFormat,
2526
private readonly ProjectNode $projectNode,
27+
private readonly SpecificationInterface|null $excludedSpecification = null,
2628
) {
2729
}
2830

@@ -45,4 +47,9 @@ public function getProjectNode(): ProjectNode
4547
{
4648
return $this->projectNode;
4749
}
50+
51+
public function getExcludedSpecification(): SpecificationInterface|null
52+
{
53+
return $this->excludedSpecification;
54+
}
4855
}

packages/guides/src/Handlers/ParseDirectoryHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function handle(ParseDirectoryCommand $command): array
5656
$extension,
5757
);
5858

59-
$files = $this->fileCollector->collect($origin, $currentDirectory, $extension);
59+
$files = $this->fileCollector->collect($origin, $currentDirectory, $extension, $command->getExcludedSpecification());
6060

6161
$postCollectFilesForParsingEvent = $this->eventDispatcher->dispatch(
6262
new PostCollectFilesForParsingEvent($command, $files),

0 commit comments

Comments
 (0)