Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DiagnosticsCollector API to be a bit cleaner #78

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/Elastic.Markdown/Diagnostics/DiagnosticsChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void Write(Diagnostic diagnostic)
}
}


public enum Severity { Error, Warning }

public readonly record struct Diagnostic
Expand Down Expand Up @@ -70,7 +69,6 @@ public void Write(Diagnostic diagnostic)
}
}


public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollection<IDiagnosticsOutput> outputs)
: IHostedService
{
Expand All @@ -84,24 +82,32 @@ public class DiagnosticsCollector(ILoggerFactory loggerFactory, IReadOnlyCollect
public long Warnings => _warnings;
public long Errors => _errors;

private Task? _started;

public HashSet<string> OffendingFiles { get; } = new();

public async Task StartAsync(Cancel ctx)
public Task StartAsync(Cancel ctx)
{
await Channel.WaitToWrite();
while (!Channel.CancellationToken.IsCancellationRequested)
if (_started is not null) return _started;
_started = Task.Run(async () =>
{
try
{
while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken))
Drain();
}
catch
await Channel.WaitToWrite();
while (!Channel.CancellationToken.IsCancellationRequested)
{
//ignore
try
{
while (await Channel.Reader.WaitToReadAsync(Channel.CancellationToken))
Drain();
}
catch
{
//ignore
}
}
}
Drain();

Drain();
}, ctx);
return _started;

void Drain()
{
Expand All @@ -124,7 +130,12 @@ private void IncrementSeverityCount(Diagnostic item)
Interlocked.Increment(ref _warnings);
}

protected virtual void HandleItem(Diagnostic diagnostic) {}
protected virtual void HandleItem(Diagnostic diagnostic) { }

public virtual Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
if (_started is not null)
await _started;
await Channel.Reader.Completion;
}
}
5 changes: 1 addition & 4 deletions src/Elastic.Markdown/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task GenerateAll(Cancel ctx)

var handledItems = 0;

var collectTask = Task.Run(async () => await Context.Collector.StartAsync(ctx), ctx);
_ = Context.Collector.StartAsync(ctx);

await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>
{
Expand Down Expand Up @@ -126,11 +126,8 @@ await Parallel.ForEachAsync(DocumentationSet.Files, ctx, async (file, token) =>

await GenerateDocumentationState(ctx);

await collectTask;
await Context.Collector.Channel.Reader.Completion;
await Context.Collector.StopAsync(ctx);


IFileInfo OutputFile(string relativePath)
{
var outputFile = _writeFileSystem.FileInfo.New(Path.Combine(DocumentationSet.OutputPath.FullName, relativePath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
var pathOnDisk = Path.Combine(includeFrom, url.TrimStart('/'));
if (!context.Build.ReadFileSystem.File.Exists(pathOnDisk))
processor.EmitError(line, column, length, $"`{url}` does not exist. resolved to `{pathOnDisk}");
else
{

}
}
else
link.Url = "";
Expand Down
6 changes: 6 additions & 0 deletions tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public class TipTests(ITestOutputHelper output) : AdmonitionTests(output, "tip")
public void SetsTitle() => Block!.Title.Should().Be("Tip");
}

public class AttentionTests(ITestOutputHelper output) : AdmonitionTests(output, "attention")
{
[Fact]
public void SetsTitle() => Block!.Title.Should().Be("Attention");
}

public class NoteTitleTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
"""
```{note} This is my custom note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ A regular paragraph.
}

// ReSharper disable UnusedType.Global
public class AttentionTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "attention");
public class DangerTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "danger");
public class ErrorTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "error");
public class HintTests(ITestOutputHelper output) : AdmonitionUnsupportedTests(output, "hint");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ protected virtual void AddToFileSystem(MockFileSystem fileSystem) { }

public virtual async Task InitializeAsync()
{
var collectTask = Task.Run(async () => await Collector.StartAsync(default), default);
_ = Collector.StartAsync(default);

Document = await File.ParseFullAsync(default);
Html = File.CreateHtml(Document);
Collector.Channel.TryComplete();

await collectTask;
await Collector.Channel.Reader.Completion;
await Collector.StopAsync(default);
}

Expand Down
4 changes: 1 addition & 3 deletions tests/Elastic.Markdown.Tests/Inline/InlneBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ protected virtual void AddToFileSystem(MockFileSystem fileSystem) { }

public virtual async Task InitializeAsync()
{
var collectTask = Task.Run(async () => await Collector.StartAsync(default), default);
_ = Collector.StartAsync(default);

await Set.ResolveDirectoryTree(default);

Document = await File.ParseFullAsync(default);
Html = File.CreateHtml(Document);
Collector.Channel.TryComplete();

await collectTask;
await Collector.Channel.Reader.Completion;
await Collector.StopAsync(default);
}

Expand Down
Loading