Skip to content

Commit 66c0052

Browse files
authored
Refactor our usage of DiagnosticCollector (#1206)
* Refactor our usage of DiagnosticCollector * fix tests not starting collector
1 parent 1b383f6 commit 66c0052

File tree

22 files changed

+140
-98
lines changed

22 files changed

+140
-98
lines changed

src/Elastic.Documentation.Configuration/YamlStreamReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public record YamlToplevelKey
1616
public required KeyValuePair<YamlNode, YamlNode> Entry { get; init; }
1717
}
1818

19-
public class YamlStreamReader(IFileInfo source, DiagnosticsCollector collector)
19+
public class YamlStreamReader(IFileInfo source, IDiagnosticsCollector collector)
2020
{
2121
private IFileInfo Source { get; init; } = source;
22-
private DiagnosticsCollector Collector { get; init; } = collector;
22+
private IDiagnosticsCollector Collector { get; init; } = collector;
2323

2424
public IEnumerable<YamlToplevelKey> Read()
2525
{
@@ -185,7 +185,7 @@ private void EmitError(string message, Mark? start = null, Mark? end = null, int
185185
Column = start.HasValue ? (int)start.Value.Column : null,
186186
Length = length
187187
};
188-
Collector.Channel.Write(d);
188+
Collector.Write(d);
189189
}
190190
public void EmitWarning(string message, Mark? start = null, Mark? end = null, int? length = null)
191191
{
@@ -199,6 +199,6 @@ public void EmitWarning(string message, Mark? start = null, Mark? end = null, in
199199
Column = start.HasValue ? (int)start.Value.Column : null,
200200
Length = length
201201
};
202-
Collector.Channel.Write(d);
202+
Collector.Write(d);
203203
}
204204
}

src/Elastic.Documentation/Diagnostics/DiagnosticsCollector.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Elastic.Documentation.Diagnostics;
1010

11-
public class DiagnosticsCollector(IReadOnlyCollection<IDiagnosticsOutput> outputs) : IHostedService, IAsyncDisposable
11+
public class DiagnosticsCollector(IReadOnlyCollection<IDiagnosticsOutput> outputs)
12+
: IDiagnosticsCollector, IHostedService
1213
{
13-
public DiagnosticsChannel Channel { get; } = new();
14+
private DiagnosticsChannel Channel { get; } = new();
1415

1516
private int _errors;
1617
private int _warnings;
@@ -29,7 +30,13 @@ public class DiagnosticsCollector(IReadOnlyCollection<IDiagnosticsOutput> output
2930

3031
public bool NoHints { get; init; }
3132

32-
public Task StartAsync(Cancel cancellationToken)
33+
public DiagnosticsCollector StartAsync(Cancel ctx)
34+
{
35+
_ = ((IHostedService)this).StartAsync(ctx);
36+
return this;
37+
}
38+
39+
Task IHostedService.StartAsync(Cancel cancellationToken)
3340
{
3441
if (_started is not null)
3542
return _started;
@@ -59,7 +66,6 @@ void Drain()
5966
{
6067
if (item.Severity == Severity.Hint && NoHints)
6168
continue;
62-
IncrementSeverityCount(item);
6369
HandleItem(item);
6470
_ = OffendingFiles.Add(item.File);
6571
foreach (var output in outputs)
@@ -82,15 +88,22 @@ protected virtual void HandleItem(Diagnostic diagnostic) { }
8288

8389
public virtual async Task StopAsync(Cancel cancellationToken)
8490
{
91+
Channel.TryComplete();
8592
if (_started is not null)
8693
await _started;
8794
await Channel.Reader.Completion;
8895
}
8996

9097
public void EmitCrossLink(string link) => CrossLinks.Add(link);
9198

99+
public void Write(Diagnostic diagnostic)
100+
{
101+
IncrementSeverityCount(diagnostic);
102+
Channel.Write(diagnostic);
103+
}
104+
92105
private void Emit(Severity severity, string file, string message) =>
93-
Channel.Write(new Diagnostic
106+
Write(new Diagnostic
94107
{
95108
Severity = severity,
96109
File = file,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Collections.Concurrent;
6+
using System.IO.Abstractions;
7+
8+
namespace Elastic.Documentation.Diagnostics;
9+
10+
public interface IDiagnosticsCollector : IAsyncDisposable
11+
{
12+
int Warnings { get; }
13+
int Errors { get; }
14+
int Hints { get; }
15+
16+
ConcurrentBag<string> CrossLinks { get; }
17+
HashSet<string> OffendingFiles { get; }
18+
ConcurrentDictionary<string, bool> InUseSubstitutionKeys { get; }
19+
20+
void EmitError(string file, string message, Exception? e = null);
21+
void EmitWarning(string file, string message);
22+
void EmitHint(string file, string message);
23+
void Write(Diagnostic diagnostic);
24+
void CollectUsedSubstitutionKey(ReadOnlySpan<char> key);
25+
void EmitCrossLink(string link);
26+
}
27+
28+
public static class DiagnosticsCollectorExtensions
29+
{
30+
public static void EmitError(this IDiagnosticsCollector collector, IFileInfo file, string message, Exception? e = null) =>
31+
collector.EmitError(file.FullName, message, e);
32+
33+
public static void EmitWarning(this IDiagnosticsCollector collector, IFileInfo file, string message) =>
34+
collector.EmitWarning(file.FullName, message);
35+
36+
public static void EmitHint(this IDiagnosticsCollector collector, IFileInfo file, string message) =>
37+
collector.EmitHint(file.FullName, message);
38+
}
39+
40+

src/Elastic.Documentation/IDocumentationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Elastic.Documentation;
99

1010
public interface IDocumentationContext
1111
{
12-
DiagnosticsCollector Collector { get; }
12+
IDiagnosticsCollector Collector { get; }
1313
IDirectoryInfo DocumentationSourceDirectory { get; }
1414
GitCheckoutInformation Git { get; }
1515
IFileSystem ReadFileSystem { get; }

src/Elastic.Markdown/BuildContext.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public record BuildContext : IDocumentationContext
2626

2727
public GitCheckoutInformation Git { get; }
2828

29-
public DiagnosticsCollector Collector { get; }
29+
public IDiagnosticsCollector Collector { get; }
3030

3131
public bool Force { get; init; }
3232

@@ -47,14 +47,11 @@ public string? UrlPathPrefix
4747
init => _urlPathPrefix = value;
4848
}
4949

50-
public BuildContext(IFileSystem fileSystem)
51-
: this(new DiagnosticsCollector([]), fileSystem, fileSystem, null, null) { }
52-
53-
public BuildContext(DiagnosticsCollector collector, IFileSystem fileSystem)
50+
public BuildContext(IDiagnosticsCollector collector, IFileSystem fileSystem)
5451
: this(collector, fileSystem, fileSystem, null, null) { }
5552

5653
public BuildContext(
57-
DiagnosticsCollector collector,
54+
IDiagnosticsCollector collector,
5855
IFileSystem readFileSystem,
5956
IFileSystem writeFileSystem,
6057
string? source = null,

src/Elastic.Markdown/Diagnostics/ProcessorDiagnosticExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void EmitError(this InlineProcessor processor, int line, int colum
3030
Message = message,
3131
Length = length
3232
};
33-
context.Build.Collector.Channel.Write(d);
33+
context.Build.Collector.Write(d);
3434
}
3535

3636

@@ -48,7 +48,7 @@ public static void EmitWarning(this InlineProcessor processor, int line, int col
4848
Message = message,
4949
Length = length
5050
};
51-
context.Build.Collector.Channel.Write(d);
51+
context.Build.Collector.Write(d);
5252
}
5353

5454
public static void EmitError(this ParserContext context, string message, Exception? e = null)
@@ -61,7 +61,7 @@ public static void EmitError(this ParserContext context, string message, Excepti
6161
File = context.MarkdownSourcePath.FullName,
6262
Message = CreateExceptionMessage(message, e),
6363
};
64-
context.Build.Collector.Channel.Write(d);
64+
context.Build.Collector.Write(d);
6565
}
6666

6767
public static void EmitWarning(this ParserContext context, int line, int column, int length, string message)
@@ -77,7 +77,7 @@ public static void EmitWarning(this ParserContext context, int line, int column,
7777
Message = message,
7878
Length = length
7979
};
80-
context.Build.Collector.Channel.Write(d);
80+
context.Build.Collector.Write(d);
8181
}
8282

8383
public static void EmitError(this IBlockExtension block, string message, Exception? e = null) => EmitDiagnostic(block, Severity.Error, message, e);
@@ -100,7 +100,7 @@ private static void EmitDiagnostic(IBlockExtension block, Severity severity, str
100100
Length = block.OpeningLength + 5,
101101
Message = CreateExceptionMessage(message, e),
102102
};
103-
block.Build.Collector.Channel.Write(d);
103+
block.Build.Collector.Write(d);
104104
}
105105

106106

@@ -121,7 +121,7 @@ private static void LinkDiagnostic(InlineProcessor processor, Severity severity,
121121
Message = CreateExceptionMessage(message, e),
122122
Length = Math.Max(length, 1)
123123
};
124-
context.Build.Collector.Channel.Write(d);
124+
context.Build.Collector.Write(d);
125125
}
126126

127127
public static void EmitError(this InlineProcessor processor, LinkInline inline, string message) =>

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,11 @@ public async Task GenerateAll(Cancel ctx)
117117
await GenerateLinkReference(ctx);
118118
}
119119

120-
public async Task StopDiagnosticCollection(Cancel ctx)
121-
{
122-
_logger.LogInformation($"Completing diagnostics channel");
123-
Context.Collector.Channel.TryComplete();
124-
125-
_logger.LogInformation($"Stopping diagnostics collector");
126-
await Context.Collector.StopAsync(ctx);
127-
128-
_logger.LogInformation($"Completed diagnostics channel");
129-
}
130-
131120
private async Task ProcessDocumentationFiles(HashSet<string> offendingFiles, DateTimeOffset outputSeenChanges, Cancel ctx)
132121
{
133122
var processedFileCount = 0;
134123
var exceptionCount = 0;
135124
var totalFileCount = DocumentationSet.Files.Count;
136-
_ = Context.Collector.StartAsync(ctx);
137125
await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
138126
{
139127
var processedFiles = Interlocked.Increment(ref processedFileCount);

src/Elastic.Markdown/Helpers/Interpolation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ParserContext context
3131
public static bool ReplaceSubstitutions(
3232
this ReadOnlySpan<char> span,
3333
IReadOnlyDictionary<string, string>? properties,
34-
DiagnosticsCollector? collector,
34+
IDiagnosticsCollector? collector,
3535
[NotNullWhen(true)] out string? replacement
3636
)
3737
{
@@ -43,7 +43,7 @@ public static bool ReplaceSubstitutions(
4343
private static bool ReplaceSubstitutions(
4444
this ReadOnlySpan<char> span,
4545
IReadOnlyDictionary<string, string>[] properties,
46-
DiagnosticsCollector? collector,
46+
IDiagnosticsCollector? collector,
4747
[NotNullWhen(true)] out string? replacement
4848
)
4949
{

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ DocumentationSet set
6666

6767
public string Id { get; } = Guid.NewGuid().ToString("N")[..8];
6868

69-
private DiagnosticsCollector Collector { get; }
69+
private IDiagnosticsCollector Collector { get; }
7070

7171
public bool Hidden { get; internal set; }
7272
public string? UrlPathPrefix { get; }

src/Elastic.Markdown/Links/InboundLinks/LinkIndexLinkChecker.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ public class LinkIndexLinkChecker(ILoggerFactory logger)
1717

1818
private sealed record RepositoryFilter
1919
{
20-
public string? LinksTo { get; set; }
21-
public string? LinksFrom { get; set; }
20+
public string? LinksTo { get; init; }
21+
public string? LinksFrom { get; init; }
2222

2323
public static RepositoryFilter None => new();
2424
}
2525

26-
public async Task<int> CheckAll(DiagnosticsCollector collector, Cancel ctx)
26+
public async Task CheckAll(IDiagnosticsCollector collector, Cancel ctx)
2727
{
2828
var fetcher = new LinksIndexCrossLinkFetcher(logger);
2929
var resolver = new CrossLinkResolver(fetcher);
3030
var crossLinks = await resolver.FetchLinks(ctx);
3131

32-
return await ValidateCrossLinks(collector, crossLinks, resolver, RepositoryFilter.None, ctx);
32+
ValidateCrossLinks(collector, crossLinks, resolver, RepositoryFilter.None);
3333
}
3434

35-
public async Task<int> CheckRepository(DiagnosticsCollector collector, string? toRepository, string? fromRepository, Cancel ctx)
35+
public async Task CheckRepository(IDiagnosticsCollector collector, string? toRepository, string? fromRepository, Cancel ctx)
3636
{
3737
var fetcher = new LinksIndexCrossLinkFetcher(logger);
3838
var resolver = new CrossLinkResolver(fetcher);
@@ -43,10 +43,10 @@ public async Task<int> CheckRepository(DiagnosticsCollector collector, string? t
4343
LinksFrom = fromRepository
4444
};
4545

46-
return await ValidateCrossLinks(collector, crossLinks, resolver, filter, ctx);
46+
ValidateCrossLinks(collector, crossLinks, resolver, filter);
4747
}
4848

49-
public async Task<int> CheckWithLocalLinksJson(DiagnosticsCollector collector, string repository, string localLinksJson, Cancel ctx)
49+
public async Task CheckWithLocalLinksJson(IDiagnosticsCollector collector, string repository, string localLinksJson, Cancel ctx)
5050
{
5151
var fetcher = new LinksIndexCrossLinkFetcher(logger);
5252
var resolver = new CrossLinkResolver(fetcher);
@@ -80,17 +80,16 @@ public async Task<int> CheckWithLocalLinksJson(DiagnosticsCollector collector, s
8080
LinksTo = repository
8181
};
8282

83-
return await ValidateCrossLinks(collector, crossLinks, resolver, filter, ctx);
83+
ValidateCrossLinks(collector, crossLinks, resolver, filter);
8484
}
8585

86-
private async Task<int> ValidateCrossLinks(
87-
DiagnosticsCollector collector,
86+
private void ValidateCrossLinks(
87+
IDiagnosticsCollector collector,
8888
FetchedCrossLinks crossLinks,
8989
CrossLinkResolver resolver,
90-
RepositoryFilter filter,
91-
Cancel ctx)
90+
RepositoryFilter filter
91+
)
9292
{
93-
_ = collector.StartAsync(ctx);
9493
foreach (var (repository, linkReference) in crossLinks.LinkReferences)
9594
{
9695
if (!string.IsNullOrEmpty(filter.LinksTo))
@@ -130,11 +129,6 @@ private async Task<int> ValidateCrossLinks(
130129
}, s => collector.EmitWarning(linksJson, s), uri, out _);
131130
}
132131
}
133-
134-
collector.Channel.TryComplete();
135-
await collector.StopAsync(ctx);
136132
// non-strict for now
137-
return collector.Errors;
138-
// return collector.Errors + collector.Warnings;
139133
}
140134
}

0 commit comments

Comments
 (0)