11using System . Diagnostics . CodeAnalysis ;
22using System . Text ;
3+ using Actions . Core ;
4+ using Actions . Core . Services ;
35using Cysharp . IO ;
46using Elastic . Markdown . Diagnostics ;
57using Errata ;
6- using Microsoft . Extensions . Hosting ;
78using Microsoft . Extensions . Logging ;
89using Spectre . Console ;
910using Diagnostic = Elastic . Markdown . Diagnostics . Diagnostic ;
@@ -21,8 +22,27 @@ public bool TryGet(string id, [NotNullWhen(true)] out Source? source)
2122 }
2223}
2324
24- public class ConsoleDiagnosticsCollector ( ILoggerFactory loggerFactory )
25- : DiagnosticsCollector ( loggerFactory , [ ] )
25+ public class GithubAnnotationOutput ( ICoreService githubActions ) : IDiagnosticsOutput
26+ {
27+ public void Write ( Diagnostic diagnostic )
28+ {
29+ if ( string . IsNullOrWhiteSpace ( Environment . GetEnvironmentVariable ( "GITHUB_ACTION" ) ) ) return ;
30+ var properties = new AnnotationProperties
31+ {
32+ File = diagnostic . File ,
33+ StartColumn = diagnostic . Column ,
34+ StartLine = diagnostic . Line ,
35+ EndColumn = diagnostic . Column + diagnostic . Length ?? 1
36+ } ;
37+ if ( diagnostic . Severity == Severity . Error )
38+ githubActions . WriteError ( diagnostic . Message , properties ) ;
39+ if ( diagnostic . Severity == Severity . Warning )
40+ githubActions . WriteWarning ( diagnostic . Message , properties ) ;
41+ }
42+ }
43+
44+ public class ConsoleDiagnosticsCollector ( ILoggerFactory loggerFactory , ICoreService ? githubActions = null )
45+ : DiagnosticsCollector ( loggerFactory , githubActions != null ? [ new GithubAnnotationOutput ( githubActions ) ] : [ ] )
2646{
2747 private readonly List < Diagnostic > _items = new ( ) ;
2848
@@ -42,35 +62,17 @@ public override async Task StopAsync(Cancel ctx)
4262 {
4363 Severity . Error =>
4464 Errata . Diagnostic . Error ( item . Message )
45- . WithLabel ( new Label ( item . File , new Location ( item . Line , item . Position ?? 0 ) , "bad substitution" )
46- . WithLength ( 8 )
47- . WithPriority ( 1 )
48- . WithColor ( Color . Red ) )
49-
50- ,
65+ . WithLabel ( new Label ( item . File , new Location ( item . Line , item . Column ?? 0 ) , "bad substitution" )
66+ . WithLength ( item . Length ?? 3 )
67+ . WithPriority ( 1 )
68+ . WithColor ( Color . Red ) ) ,
5169 Severity . Warning =>
5270 Errata . Diagnostic . Warning ( item . Message ) ,
5371 _ => Errata . Diagnostic . Info ( item . Message )
5472 } ;
5573 report . AddDiagnostic ( d ) ;
56- /*
57- report.AddDiagnostic(
58- Errata.Diagnostic.Error("Operator '/' cannot be applied to operands of type 'string' and 'int'")
59- .WithCode("CS0019")
60- .WithNote("Try changing the type")
61- .WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 23), "This is of type 'int'")
62- .WithLength(3)
63- .WithPriority(1)
64- .WithColor(Color.Yellow))
65- .WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 27), "Division is not possible")
66- .WithPriority(3)
67- .WithColor(Color.Red))
68- .WithLabel(new Label("Demo/Files/Program.cs", new Location(15, 29), "This is of type 'string'")
69- .WithLength(3)
70- .WithPriority(2)
71- .WithColor(Color.Blue)));
72- */
7374 }
75+
7476 // Render the report
7577 report . Render ( AnsiConsole . Console ) ;
7678 AnsiConsole . WriteLine ( ) ;
0 commit comments