|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Translation\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\RuntimeException; |
16 | 17 | use Symfony\Component\Console\Input\InputArgument; |
@@ -56,7 +57,7 @@ protected function configure() |
56 | 57 | $this |
57 | 58 | ->setDescription(self::$defaultDescription) |
58 | 59 | ->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN') |
59 | | - ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt') |
| 60 | + ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format') |
60 | 61 | ->setHelp(<<<EOF |
61 | 62 | The <info>%command.name%</info> command lints an XLIFF file and outputs to STDOUT |
62 | 63 | the first encountered syntax error. |
@@ -86,6 +87,10 @@ protected function execute(InputInterface $input, OutputInterface $output) |
86 | 87 | $this->format = $input->getOption('format'); |
87 | 88 | $this->displayCorrectFiles = $output->isVerbose(); |
88 | 89 |
|
| 90 | + if (null === $this->format) { |
| 91 | + $this->format = GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt'; |
| 92 | + } |
| 93 | + |
89 | 94 | if (['-'] === $filenames) { |
90 | 95 | return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]); |
91 | 96 | } |
@@ -160,25 +165,34 @@ private function display(SymfonyStyle $io, array $files) |
160 | 165 | return $this->displayTxt($io, $files); |
161 | 166 | case 'json': |
162 | 167 | return $this->displayJson($io, $files); |
| 168 | + case 'github': |
| 169 | + return $this->displayTxt($io, $files, true); |
163 | 170 | default: |
164 | 171 | throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format)); |
165 | 172 | } |
166 | 173 | } |
167 | 174 |
|
168 | | - private function displayTxt(SymfonyStyle $io, array $filesInfo) |
| 175 | + private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false) |
169 | 176 | { |
170 | 177 | $countFiles = \count($filesInfo); |
171 | 178 | $erroredFiles = 0; |
| 179 | + $githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($io) : null; |
172 | 180 |
|
173 | 181 | foreach ($filesInfo as $info) { |
174 | 182 | if ($info['valid'] && $this->displayCorrectFiles) { |
175 | 183 | $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : '')); |
176 | 184 | } elseif (!$info['valid']) { |
177 | 185 | ++$erroredFiles; |
178 | 186 | $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : '')); |
179 | | - $io->listing(array_map(function ($error) { |
| 187 | + $io->listing(array_map(function ($error) use ($info, $githubReporter) { |
180 | 188 | // general document errors have a '-1' line number |
181 | | - return -1 === $error['line'] ? $error['message'] : sprintf('Line %d, Column %d: %s', $error['line'], $error['column'], $error['message']); |
| 189 | + $line = -1 === $error['line'] ? null : $error['line']; |
| 190 | + |
| 191 | + if ($githubReporter) { |
| 192 | + $githubReporter->error($error['message'], $info['file'], $line, null !== $line ? $error['column'] : null); |
| 193 | + } |
| 194 | + |
| 195 | + return null === $line ? $error['message'] : sprintf('Line %d, Column %d: %s', $line, $error['column'], $error['message']); |
182 | 196 | }, $info['messages'])); |
183 | 197 | } |
184 | 198 | } |
|
0 commit comments