-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce dedicated error collector for serve command
- Loading branch information
Showing
14 changed files
with
225 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/docs-builder/Diagnostics/Console/ConsoleDiagnosticsCollector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Actions.Core.Services; | ||
using Elastic.Markdown.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
using Spectre.Console; | ||
using Diagnostic = Elastic.Markdown.Diagnostics.Diagnostic; | ||
|
||
namespace Documentation.Builder.Diagnostics.Console; | ||
|
||
public class ConsoleDiagnosticsCollector(ILoggerFactory loggerFactory, ICoreService? githubActions = null) | ||
: DiagnosticsCollector([new Log(loggerFactory.CreateLogger<Log>()), new GithubAnnotationOutput(githubActions)] | ||
) | ||
{ | ||
private readonly List<Diagnostic> _items = new(); | ||
|
||
protected override void HandleItem(Diagnostic diagnostic) => _items.Add(diagnostic); | ||
|
||
public override async Task StopAsync(Cancel ctx) | ||
{ | ||
var repository = new ErrataFileSourceRepository(); | ||
repository.WriteDiagnosticsToConsole(_items); | ||
|
||
AnsiConsole.WriteLine(); | ||
AnsiConsole.Write(new Markup($" [bold red]{Errors} Errors[/] / [bold blue]{Warnings} Warnings[/]")); | ||
AnsiConsole.WriteLine(); | ||
AnsiConsole.WriteLine(); | ||
|
||
await Task.CompletedTask; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/docs-builder/Diagnostics/Console/ErrataFileSourceRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
using Cysharp.IO; | ||
using Elastic.Markdown.Diagnostics; | ||
using Errata; | ||
using Spectre.Console; | ||
using Diagnostic = Elastic.Markdown.Diagnostics.Diagnostic; | ||
|
||
namespace Documentation.Builder.Diagnostics.Console; | ||
|
||
public class ErrataFileSourceRepository : ISourceRepository | ||
{ | ||
public bool TryGet(string id, [NotNullWhen(true)] out Source? source) | ||
{ | ||
using var reader = new Utf8StreamReader(id); | ||
var text = Encoding.UTF8.GetString(reader.ReadToEndAsync().GetAwaiter().GetResult()); | ||
source = new Source(id, text); | ||
return true; | ||
} | ||
|
||
public void WriteDiagnosticsToConsole(IReadOnlyCollection<Diagnostic> items) | ||
{ | ||
var report = new Report(this); | ||
foreach (var item in items) | ||
{ | ||
var d = item.Severity switch | ||
{ | ||
Severity.Error => Errata.Diagnostic.Error(item.Message), | ||
Severity.Warning => Errata.Diagnostic.Warning(item.Message), | ||
_ => Errata.Diagnostic.Info(item.Message) | ||
}; | ||
if (item is { Line: not null, Column: not null }) | ||
{ | ||
var location = new Location(item.Line ?? 0, item.Column ?? 0); | ||
d = d.WithLabel(new Label(item.File, location, "") | ||
.WithLength(item.Length == null ? 1 : Math.Clamp(item.Length.Value, 1, item.Length.Value + 3)) | ||
.WithPriority(1) | ||
.WithColor(item.Severity == Severity.Error ? Color.Red : Color.Blue)); | ||
} | ||
else | ||
d = d.WithNote(item.File); | ||
|
||
report.AddDiagnostic(d); | ||
} | ||
|
||
// Render the report | ||
report.Render(AnsiConsole.Console); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/docs-builder/Diagnostics/Console/GithubAnnotationOutput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Actions.Core; | ||
using Actions.Core.Services; | ||
using Elastic.Markdown.Diagnostics; | ||
|
||
namespace Documentation.Builder.Diagnostics.Console; | ||
|
||
public class GithubAnnotationOutput(ICoreService? githubActions) : IDiagnosticsOutput | ||
{ | ||
public void Write(Diagnostic diagnostic) | ||
{ | ||
if (githubActions == null) return; | ||
if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("GITHUB_ACTION"))) | ||
return; | ||
var properties = new AnnotationProperties | ||
{ | ||
File = diagnostic.File, | ||
StartColumn = diagnostic.Column, | ||
StartLine = diagnostic.Line, | ||
EndColumn = diagnostic.Column + diagnostic.Length ?? 1 | ||
}; | ||
if (diagnostic.Severity == Severity.Error) | ||
githubActions.WriteError(diagnostic.Message, properties); | ||
if (diagnostic.Severity == Severity.Warning) | ||
githubActions.WriteWarning(diagnostic.Message, properties); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
src/docs-builder/Diagnostics/LiveMode/LiveModeDiagnosticsCollector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Markdown.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
using Diagnostic = Elastic.Markdown.Diagnostics.Diagnostic; | ||
|
||
namespace Documentation.Builder.Diagnostics.LiveMode; | ||
|
||
public class LiveModeDiagnosticsCollector(ILoggerFactory loggerFactory) | ||
: DiagnosticsCollector([new Log(loggerFactory.CreateLogger<Log>())]) | ||
{ | ||
protected override void HandleItem(Diagnostic diagnostic) { } | ||
|
||
public override async Task StopAsync(Cancel ctx) => await Task.CompletedTask; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Markdown.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Documentation.Builder; | ||
|
||
// named Log for terseness on console output | ||
public class Log(ILogger logger) : IDiagnosticsOutput | ||
{ | ||
public void Write(Diagnostic diagnostic) | ||
{ | ||
if (diagnostic.Severity == Severity.Error) | ||
logger.LogError($"{diagnostic.Message} ({diagnostic.File}:{diagnostic.Line})"); | ||
else | ||
logger.LogWarning($"{diagnostic.Message} ({diagnostic.File}:{diagnostic.Line})"); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.