Skip to content

[TwigBridge] Allow twig:lint to excludes dirs #50864

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
Feb 3, 2024
Merged
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
12 changes: 7 additions & 5 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected function configure()
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())))
->addOption('show-deprecations', null, InputOption::VALUE_NONE, 'Show deprecations as errors')
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
->addOption('excludes', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Excluded directory', [])
->setHelp(<<<'EOF'
The <info>%command.name%</info> command lints a template and outputs to STDOUT
the first encountered syntax error.
Expand Down Expand Up @@ -84,6 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$filenames = $input->getArgument('filename');
$showDeprecations = $input->getOption('show-deprecations');
$excludes = $input->getOption('excludes');
$this->format = $input->getOption('format') ?? (GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt');

if (['-'] === $filenames) {
Expand Down Expand Up @@ -121,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

try {
$filesInfo = $this->getFilesInfo($filenames);
$filesInfo = $this->getFilesInfo($filenames, $excludes);
} finally {
if ($showDeprecations) {
restore_error_handler();
Expand All @@ -131,24 +133,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $this->display($input, $output, $io, $filesInfo);
}

private function getFilesInfo(array $filenames): array
private function getFilesInfo(array $filenames, array $excludes): array
{
$filesInfo = [];
foreach ($filenames as $filename) {
foreach ($this->findFiles($filename) as $file) {
foreach ($this->findFiles($filename, $excludes) as $file) {
$filesInfo[] = $this->validate(file_get_contents($file), $file);
}
}

return $filesInfo;
}

protected function findFiles(string $filename): iterable
protected function findFiles(string $filename, array $excludes): iterable
{
if (is_file($filename)) {
return [$filename];
} elseif (is_dir($filename)) {
return Finder::create()->files()->in($filename)->name($this->namePatterns);
return Finder::create()->files()->in($filename)->name($this->namePatterns)->exclude($excludes);
}

throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
Expand Down