|
11 | 11 |
|
12 | 12 | namespace Symfony\Bridge\Twig\Command;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Console\CI\GithubActionReporter; |
14 | 15 | use Symfony\Component\Console\Command\Command;
|
15 | 16 | use Symfony\Component\Console\Exception\InvalidArgumentException;
|
16 | 17 | use Symfony\Component\Console\Exception\RuntimeException;
|
@@ -39,6 +40,11 @@ class LintCommand extends Command
|
39 | 40 |
|
40 | 41 | private $twig;
|
41 | 42 |
|
| 43 | + /** |
| 44 | + * @var string|null |
| 45 | + */ |
| 46 | + private $format; |
| 47 | + |
42 | 48 | public function __construct(Environment $twig)
|
43 | 49 | {
|
44 | 50 | parent::__construct();
|
@@ -80,6 +86,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
|
80 | 86 | $io = new SymfonyStyle($input, $output);
|
81 | 87 | $filenames = $input->getArgument('filename');
|
82 | 88 | $showDeprecations = $input->getOption('show-deprecations');
|
| 89 | + $this->format = $input->getOption('format'); |
| 90 | + |
| 91 | + if ('github' === $this->format && !class_exists(GithubActionReporter::class)) { |
| 92 | + throw new \InvalidArgumentException('The "github" format is only available since "symfony/console" >= 5.3.'); |
| 93 | + } |
| 94 | + |
| 95 | + if (null === $this->format) { |
| 96 | + $this->format = class_exists(GithubActionReporter::class) && GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt'; |
| 97 | + } |
83 | 98 |
|
84 | 99 | if (['-'] === $filenames) {
|
85 | 100 | return $this->display($input, $output, $io, [$this->validate(file_get_contents('php://stdin'), uniqid('sf_', true))]);
|
@@ -169,26 +184,29 @@ private function validate(string $template, string $file): array
|
169 | 184 |
|
170 | 185 | private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files)
|
171 | 186 | {
|
172 |
| - switch ($input->getOption('format')) { |
| 187 | + switch ($this->format) { |
173 | 188 | case 'txt':
|
174 | 189 | return $this->displayTxt($output, $io, $files);
|
175 | 190 | case 'json':
|
176 | 191 | return $this->displayJson($output, $files);
|
| 192 | + case 'github': |
| 193 | + return $this->displayTxt($output, $io, $files, true); |
177 | 194 | default:
|
178 | 195 | throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
|
179 | 196 | }
|
180 | 197 | }
|
181 | 198 |
|
182 |
| - private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo) |
| 199 | + private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false) |
183 | 200 | {
|
184 | 201 | $errors = 0;
|
| 202 | + $githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($output) : null; |
185 | 203 |
|
186 | 204 | foreach ($filesInfo as $info) {
|
187 | 205 | if ($info['valid'] && $output->isVerbose()) {
|
188 | 206 | $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
|
189 | 207 | } elseif (!$info['valid']) {
|
190 | 208 | ++$errors;
|
191 |
| - $this->renderException($io, $info['template'], $info['exception'], $info['file']); |
| 209 | + $this->renderException($io, $info['template'], $info['exception'], $info['file'], $githubReporter); |
192 | 210 | }
|
193 | 211 | }
|
194 | 212 |
|
@@ -220,10 +238,14 @@ private function displayJson(OutputInterface $output, array $filesInfo)
|
220 | 238 | return min($errors, 1);
|
221 | 239 | }
|
222 | 240 |
|
223 |
| - private function renderException(SymfonyStyle $output, string $template, Error $exception, string $file = null) |
| 241 | + private function renderException(SymfonyStyle $output, string $template, Error $exception, string $file = null, ?GithubActionReporter $githubReporter = null) |
224 | 242 | {
|
225 | 243 | $line = $exception->getTemplateLine();
|
226 | 244 |
|
| 245 | + if ($githubReporter) { |
| 246 | + $githubReporter->error($exception->getRawMessage(), $file, $line <= 0 ? null : $line); |
| 247 | + } |
| 248 | + |
227 | 249 | if ($file) {
|
228 | 250 | $output->text(sprintf('<error> ERROR </error> in %s (line %s)', $file, $line));
|
229 | 251 | } else {
|
|
0 commit comments