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 ;
@@ -37,6 +38,10 @@ class LintCommand extends Command
37
38
protected static $ defaultName = 'lint:twig ' ;
38
39
39
40
private $ twig ;
41
+ /**
42
+ * @var string
43
+ */
44
+ private $ format ;
40
45
41
46
public function __construct (Environment $ twig )
42
47
{
@@ -49,7 +54,7 @@ protected function configure()
49
54
{
50
55
$ this
51
56
->setDescription ('Lints a template and outputs encountered errors ' )
52
- ->addOption ('format ' , null , InputOption::VALUE_REQUIRED , 'The output format ' , ' txt ' )
57
+ ->addOption ('format ' , null , InputOption::VALUE_REQUIRED , 'The output format ' )
53
58
->addOption ('show-deprecations ' , null , InputOption::VALUE_NONE , 'Show deprecations as errors ' )
54
59
->addArgument ('filename ' , InputArgument::IS_ARRAY , 'A file, a directory or "-" for reading from STDIN ' )
55
60
->setHelp (<<<'EOF'
@@ -79,6 +84,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
79
84
$ io = new SymfonyStyle ($ input , $ output );
80
85
$ filenames = $ input ->getArgument ('filename ' );
81
86
$ showDeprecations = $ input ->getOption ('show-deprecations ' );
87
+ $ this ->format = $ input ->getOption ('format ' );
88
+
89
+ if ('github ' === $ this ->format && !class_exists (GithubActionReporter::class)) {
90
+ throw new \InvalidArgumentException ('The "github" format is only available since "symfony/console" >= 5.3. ' );
91
+ }
92
+
93
+ if (null === $ this ->format ) {
94
+ $ this ->format = class_exists (GithubActionReporter::class) && GithubActionReporter::isGithubActionEnvironment () ? 'github ' : 'txt ' ;
95
+ }
82
96
83
97
if (['- ' ] === $ filenames ) {
84
98
return $ this ->display ($ input , $ output , $ io , [$ this ->validate (file_get_contents ('php://stdin ' ), uniqid ('sf_ ' , true ))]);
@@ -168,26 +182,29 @@ private function validate(string $template, string $file): array
168
182
169
183
private function display (InputInterface $ input , OutputInterface $ output , SymfonyStyle $ io , array $ files )
170
184
{
171
- switch ($ input -> getOption ( ' format ' ) ) {
185
+ switch ($ this -> format ) {
172
186
case 'txt ' :
173
187
return $ this ->displayTxt ($ output , $ io , $ files );
174
188
case 'json ' :
175
189
return $ this ->displayJson ($ output , $ files );
190
+ case 'github ' :
191
+ return $ this ->displayTxt ($ output , $ io , $ files , true );
176
192
default :
177
193
throw new InvalidArgumentException (sprintf ('The format "%s" is not supported. ' , $ input ->getOption ('format ' )));
178
194
}
179
195
}
180
196
181
- private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , array $ filesInfo )
197
+ private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , array $ filesInfo, bool $ errorAsGithubAnnotations = false )
182
198
{
183
199
$ errors = 0 ;
200
+ $ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
184
201
185
202
foreach ($ filesInfo as $ info ) {
186
203
if ($ info ['valid ' ] && $ output ->isVerbose ()) {
187
204
$ io ->comment ('<info>OK</info> ' .($ info ['file ' ] ? sprintf (' in %s ' , $ info ['file ' ]) : '' ));
188
205
} elseif (!$ info ['valid ' ]) {
189
206
++$ errors ;
190
- $ this ->renderException ($ io , $ info ['template ' ], $ info ['exception ' ], $ info ['file ' ]);
207
+ $ this ->renderException ($ io , $ info ['template ' ], $ info ['exception ' ], $ info ['file ' ], $ githubReporter );
191
208
}
192
209
}
193
210
@@ -219,10 +236,14 @@ private function displayJson(OutputInterface $output, array $filesInfo)
219
236
return min ($ errors , 1 );
220
237
}
221
238
222
- private function renderException (OutputInterface $ output , string $ template , Error $ exception , string $ file = null )
239
+ private function renderException (OutputInterface $ output , string $ template , Error $ exception , string $ file = null , ? GithubActionReporter $ githubReporter = null )
223
240
{
224
241
$ line = $ exception ->getTemplateLine ();
225
242
243
+ if ($ githubReporter ) {
244
+ $ githubReporter ->error ($ exception ->getRawMessage (), $ file , $ line );
245
+ }
246
+
226
247
if ($ file ) {
227
248
$ output ->text (sprintf ('<error> ERROR </error> in %s (line %s) ' , $ file , $ line ));
228
249
} else {
0 commit comments