Skip to content

Commit

Permalink
[TwigBridge] Allow twig:lint to excludes dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
94noni authored and fabpot committed Feb 3, 2024
1 parent 06e630c commit 85e0c42
Showing 1 changed file with 7 additions and 5 deletions.
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

0 comments on commit 85e0c42

Please sign in to comment.