8000 feature #50864 [TwigBridge] Allow `twig:lint` to excludes dirs (94noni) · symfony/symfony@f9bdfb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit f9bdfb2

Browse files
committed
feature #50864 [TwigBridge] Allow twig:lint to excludes dirs (94noni)
This PR was merged into the 7.1 branch. Discussion ---------- [TwigBridge] Allow `twig:lint` to excludes dirs | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Read PR desc | License | MIT | Doc PR | >**Note** if it is already possible somehow and I did not found, please feel free to answer and close PR I added a twig file related to [data_collector](https://symfony.com/doc/current/profiler.html#creating-a-data-collector) and used a profiler_dump inside it (in dev env this function exists) ([PR doc pending](symfony/symfony-docs#18470)) I do have a CI checks running this `twig:lint` on my `templates`, and since I’ve added the twig file inside it for a dev profiler data collector, it crashs with `>> Unknown "profiler_dump" function.` (FYI i do have severals directories in this `templates/` I do not want to list them all) This PR (opened just to see how it goes) adds an optional option as array to excludes dirs like `--excludes=data_collector` before: `APP_DEBUG=0 APP_ENV=preprod symfony console lint:twig templates` 🔴 after: `APP_DEBUG=0 APP_ENV=preprod symfony console lint:twig templates --excludes=data_collector` 🟢 Commits ------- 85e0c42 [TwigBridge] Allow `twig:lint` to excludes dirs
2 parents 40a2cfb + 85e0c42 commit f9bdfb2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected function configure(): void
5454
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())))
5555
->addOption('show-deprecations', null, InputOption::VALUE_NONE, 'Show deprecations as errors')
5656
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
57+
->addOption('excludes', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Excluded directory', [])
5758
->setHelp(<<<'EOF'
5859
The <info>%command.name%</info> command lints a template and outputs to STDOUT
5960
the first encountered syntax error.
@@ -81,6 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8182
$io = new SymfonyStyle($input, $output);
8283
$filenames = $input->getArgument('filename');
8384
$showDeprecations = $input->getOption('show-deprecations');
85+
$excludes = $input->getOption('excludes');
8486
$this->format = $input->getOption('format') ?? (GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt');
8587

8688
if (['-'] === $filenames) {
@@ -118,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
118120
}
119121

120122
try {
121-
$filesInfo = $this->getFilesInfo($filenames);
123+
$filesInfo = $this->getFilesInfo($filenames, $excludes);
122124
} finally {
123125
if ($showDeprecations) {
124126
restore_error_handler();
@@ -128,24 +130,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
128130
return $this->display($input, $output, $io, $filesInfo);
129131
}
130132

131-
private function getFilesInfo(array $filenames): array
133+
private function getFilesInfo(array $filenames, array $excludes): array
132134
{
133135
$filesInfo = [];
134136
foreach ($filenames as $filename) {
135-
foreach ($this->findFiles($filename) as $file) {
137+
foreach ($this->findFiles($filename, $excludes) as $file) {
136138
$filesInfo[] = $this->validate(file_get_contents($file), $file);
137139
}
138140
}
139141

140142
return $filesInfo;
141143
}
142144

143-
protected function findFiles(string $filename): iterable
145+
protected function findFiles(string $filename, array $excludes): iterable
144146
{
145147
if (is_file($filename)) {
146148
return [$filename];
147149
} elseif (is_dir($filename)) {
148-
return Finder::create()->files()->in($filename)->name($this->namePatterns);
150+
return Finder::create()->files()->in($filename)->name($this->namePatterns)->exclude($excludes);
149151
}
150152

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

0 commit comments

Comments
 (0)
0