Skip to content

Commit

Permalink
Reuse query locator
Browse files Browse the repository at this point in the history
  • Loading branch information
james-cockayne-ad committed Dec 1, 2023
1 parent a5bfe67 commit 81ad9ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
10 changes: 5 additions & 5 deletions OmopTransformer/Documentation/DocumentationRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

namespace OmopTransformer.Documentation;

public class DocumentationRenderer
internal class DocumentationRenderer
{
private readonly IReadOnlyCollection<Type> _types;
private readonly Dictionary<string, Query> _aggregateQueries;
private readonly IQueryLocator _queryLocator;

public DocumentationRenderer(IReadOnlyCollection<Type> types, Dictionary<string, Query> aggregateQueries)
public DocumentationRenderer(IReadOnlyCollection<Type> types, IQueryLocator queryLocator)
{
_types = types;
_aggregateQueries = aggregateQueries;
_queryLocator = queryLocator;
}

public string Render()
Expand Down Expand Up @@ -79,7 +79,7 @@ public string Render()

private void RenderAggregateTransform(string queryFileName, StringBuilder stringBuilder)
{
var query = _aggregateQueries[queryFileName];
var query = _queryLocator.GetQuery(queryFileName);

if (query.Explanation?.Explanations == null)
{
Expand Down
29 changes: 4 additions & 25 deletions OmopTransformer/Documentation/DocumentationWriter.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Reflection;
using Microsoft.Extensions.Logging;
using OmopTransformer.Transformation;

namespace OmopTransformer.Documentation;

internal class DocumentationWriter : IDocumentationWriter
{
private readonly DocumentationOptions _documentationOption;
private readonly ILogger<DocumentationWriter> _logger;
private readonly IQueryLocator _queryLocator;

public DocumentationWriter(DocumentationOptions documentationOption, ILogger<DocumentationWriter> logger)
public DocumentationWriter(DocumentationOptions documentationOption, ILogger<DocumentationWriter> logger, IQueryLocator queryLocator)
{
_documentationOption = documentationOption;
_logger = logger;
_queryLocator = queryLocator;
}

public async Task WriteToPath(CancellationToken cancellationToken)
Expand All @@ -25,31 +26,9 @@ public async Task WriteToPath(CancellationToken cancellationToken)
return;
}

string runningDirectory = AppDomain.CurrentDomain.BaseDirectory;

string[] queryFilePaths = Directory.GetFiles(runningDirectory, "*.xml", SearchOption.AllDirectories);

var queryFiles =
queryFilePaths
.Select(async path =>
new
{
Path = path,
Text = await File.ReadAllTextAsync(path, cancellationToken)
})
.ToList();

await Task.WhenAll(queryFiles);

var aggregateQueries =
queryFiles
.ToDictionary(
keySelector: query => Path.GetFileName(query.Result.Path),
elementSelector: query => AggregateQueryParser.ParseAggregateQuery(query.Result.Text));

Assembly currentAssembly = Assembly.GetExecutingAssembly();

string documentation = new DocumentationRenderer(currentAssembly.GetTypes(), aggregateQueries).Render();
string documentation = new DocumentationRenderer(currentAssembly.GetTypes(), _queryLocator).Render();

await File.WriteAllTextAsync(_documentationOption.FilePath, documentation, cancellationToken);
}
Expand Down

0 comments on commit 81ad9ee

Please sign in to comment.