Skip to content

Allow excluding paths from the source directory #898

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 1 commit into from
Mar 19, 2024
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
31 changes: 31 additions & 0 deletions packages/guides-cli/src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
namespace phpDocumentor\Guides\Cli\Command;

use Flyfinder\Finder;
use Flyfinder\Path;
use Flyfinder\Specification\InPath;
use Flyfinder\Specification\NotSpecification;
use Flyfinder\Specification\OrSpecification;
use Flyfinder\Specification\SpecificationInterface;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Tactician\CommandBus;
Expand Down Expand Up @@ -51,7 +56,10 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

use function array_map;
use function array_pop;
use function array_reduce;
use function array_shift;
use function assert;
use function count;
use function implode;
Expand Down Expand Up @@ -93,6 +101,13 @@ public function __construct(
'If set, only the specified file is parsed, relative to the directory specified in "input"',
);

$this->addOption(
'exclude-path',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Paths to exclude, doc files in these directories will not be parsed',
);

$this->addOption(
'input-format',
null,
Expand Down Expand Up @@ -314,12 +329,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($settings->getInputFile() === '') {
$exclude = null;
if ($input->getOption('exclude-path')) {
/** @var string[] $excludedPaths */
$excludedPaths = (array) $input->getOption('exclude-path');
$excludedSpecifications = array_map(static fn (string $path) => new NotSpecification(new InPath(new Path($path))), $excludedPaths);
$excludedSpecification = array_shift($excludedSpecifications);
assert($excludedSpecification !== null);

$exclude = array_reduce(
$excludedSpecifications,
static fn (SpecificationInterface $carry, SpecificationInterface $spec) => new OrSpecification($carry, $spec),
$excludedSpecification,
);
}

$documents = $this->commandBus->handle(
new ParseDirectoryCommand(
$sourceFileSystem,
'',
$settings->getInputFormat(),
$projectNode,
$exclude,
),
);
} else {
Expand Down
15 changes: 11 additions & 4 deletions packages/guides/src/FileCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Flyfinder\Specification\AndSpecification;
use Flyfinder\Specification\HasExtension;
use Flyfinder\Specification\InPath;
use Flyfinder\Specification\NotSpecification;
use Flyfinder\Specification\SpecificationInterface;
use InvalidArgumentException;
use League\Flysystem\FilesystemInterface;

Expand All @@ -36,14 +38,19 @@ final class FileCollector
* This takes into account the presence of cached & fresh MetaEntry
* objects, and avoids adding files to the parse queue that have
* not changed and whose direct dependencies have not changed.
*
* @param SpecificationInterface|null $excludedSpecification specification that is used to exclude specific files/directories
*/
public function collect(FilesystemInterface $filesystem, string $directory, string $extension): Files
public function collect(FilesystemInterface $filesystem, string $directory, string $extension, SpecificationInterface|null $excludedSpecification = null): Files
{
$directory = trim($directory, '/');
$specification = new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension]));
if ($excludedSpecification) {
$specification = new AndSpecification($specification, new NotSpecification($excludedSpecification));
}

/** @var array<array<string>> $files */
$files = $filesystem->find(
new AndSpecification(new InPath(new Path($directory)), new HasExtension([$extension])),
);
$files = $filesystem->find($specification);

// completely populate the splFileInfos property
$this->fileInfos = [];
Expand Down
7 changes: 7 additions & 0 deletions packages/guides/src/Handlers/ParseDirectoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace phpDocumentor\Guides\Handlers;

use Flyfinder\Specification\SpecificationInterface;
use League\Flysystem\FilesystemInterface;
use phpDocumentor\Guides\Nodes\ProjectNode;

Expand All @@ -23,6 +24,7 @@ public function __construct(
private readonly string $directory,
private readonly string $inputFormat,
private readonly ProjectNode $projectNode,
private readonly SpecificationInterface|null $excludedSpecification = null,
) {
}

Expand All @@ -45,4 +47,9 @@ public function getProjectNode(): ProjectNode
{
return $this->projectNode;
}

public function getExcludedSpecification(): SpecificationInterface|null
{
return $this->excludedSpecification;
}
}
2 changes: 1 addition & 1 deletion packages/guides/src/Handlers/ParseDirectoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function handle(ParseDirectoryCommand $command): array
$extension,
);

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

$postCollectFilesForParsingEvent = $this->eventDispatcher->dispatch(
new PostCollectFilesForParsingEvent($command, $files),
Expand Down