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 ();
@@ -50,7 +56,7 @@ protected function configure()
50
56
{
51
57
$ this
52
58
->setDescription (self ::$ defaultDescription )
53
- ->addOption ('format ' , null , InputOption::VALUE_REQUIRED , 'The output format ' , ' txt ' )
59
+ ->addOption ('format ' , null , InputOption::VALUE_REQUIRED , 'The output format ' )
54
60
->addOption ('show-deprecations ' , null , InputOption::VALUE_NONE , 'Show deprecations as errors ' )
55
61
->addArgument ('filename ' , InputArgument::IS_ARRAY , 'A file, a directory or "-" for reading from STDIN ' )
56
62
->setHelp (<<<'EOF'
@@ -80,6 +86,11 @@ 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 (null === $ this ->format ) {
92
+ $ this ->format = class_exists (GithubActionReporter::class) && GithubActionReporter::isGithubActionEnvironment () ? 'github ' : 'txt ' ;
93
+ }
83
94
84
95
if (['- ' ] === $ filenames ) {
85
96
return $ this ->display ($ input , $ output , $ io , [$ this ->validate (file_get_contents ('php://stdin ' ), uniqid ('sf_ ' , true ))]);
@@ -169,26 +180,29 @@ private function validate(string $template, string $file): array
169
180
170
181
private function display (InputInterface $ input , OutputInterface $ output , SymfonyStyle $ io , array $ files )
171
182
{
172
- switch ($ input -> getOption ( ' format ' ) ) {
183
+ switch ($ this -> format ) {
173
184
case 'txt ' :
174
185
return $ this ->displayTxt ($ output , $ io , $ files );
175
186
case 'json ' :
176
187
return $ this ->displayJson ($ output , $ files );
188
+ case 'github ' :
189
+ return $ this ->displayTxt ($ output , $ io , $ files , true );
177
190
default :
178
191
throw new InvalidArgumentException (sprintf ('The format "%s" is not supported. ' , $ input ->getOption ('format ' )));
179
192
}
180
193
}
181
194
182
- private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , array $ filesInfo )
195
+ private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , array $ filesInfo, bool $ errorAsGithubAnnotations = false )
183
196
{
184
197
$ errors = 0 ;
198
+ $ githubReporter = $ errorAsGithubAnnotations ? new GithubActionReporter ($ output ) : null ;
185
199
186
200
foreach ($ filesInfo as $ info ) {
187
201
if ($ info ['valid ' ] && $ output ->isVerbose ()) {
188
202
$ io ->comment ('<info>OK</info> ' .($ info ['file ' ] ? sprintf (' in %s ' , $ info ['file ' ]) : '' ));
189
203
} elseif (!$ info ['valid ' ]) {
190
204
++$ errors ;
191
- $ this ->renderException ($ io , $ info ['template ' ], $ info ['exception ' ], $ info ['file ' ]);
205
+ $ this ->renderException ($ io , $ info ['template ' ], $ info ['exception ' ], $ info ['file ' ], $ githubReporter );
192
206
}
193
207
}
194
208
@@ -220,10 +234,14 @@ private function displayJson(OutputInterface $output, array $filesInfo)
220
234
return min ($ errors , 1 );
221
235
}
222
236
223
- private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , string $ file = null )
237
+ private function renderException (SymfonyStyle $ output , string $ template , Error $ exception , string $ file = null , ? GithubActionReporter $ githubReporter = null )
224
238
{
225
239
$ line = $ exception ->getTemplateLine ();
226
240
241
+ if ($ githubReporter ) {
242
+ $ githubReporter ->error ($ exception ->getRawMessage (), $ file , $ line <= 0 ? null : $ line );
243
+ }
244
+
227
245
if ($ file ) {
228
246
$ output ->text (sprintf ('<error> ERROR </error> in %s (line %s) ' , $ file , $ line ));
229
247
} else {
0 commit comments