Skip to content

Commit

Permalink
Merge pull request #42 from candy-kingdom/feature/35-primary-construc…
Browse files Browse the repository at this point in the history
…tors

Update Roslyn packages
  • Loading branch information
joshua-light authored Dec 9, 2023
2 parents 7498d7d + 8b26fd5 commit 4bf20e9
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 117 deletions.
2 changes: 2 additions & 0 deletions docs/ConsoleLoggerFactory.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public class ConsoleLoggerFactory : ILoggerFactory
```

A simple factory for console loggers that format messages in the Summary CLI way.

## Fields
### Instance
```cs
Expand Down
4 changes: 2 additions & 2 deletions docs/ScanDirectoryPipe.md → docs/ScanPipe.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Summary.Pipes.IO.ScanDirectoryPipe
# Summary.Pipes.IO.ScanPipe
```cs
public class ScanDirectoryPipe : IPipe<Unit, string[]>
public class ScanPipe : IPipe<Unit, string[]>
```

A [`IPipe{I,O}`](./IPipe{I,O}.md) that searches specified directory (recursively) for files that match specified pattern.
Expand Down
3 changes: 3 additions & 0 deletions src/Cli/Logging/ConsoleLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Summary.Cli.Logging;

/// <summary>
/// A simple factory for console loggers that format messages in the Summary CLI way.
/// </summary>
public class ConsoleLoggerFactory : ILoggerFactory
{
private class Logger : ILogger
Expand Down
1 change: 0 additions & 1 deletion src/Core/Pipelines/SummaryPipeline.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Summary.Pipes;
using Summary.Pipes.Filters;

namespace Summary.Pipelines;

Expand Down
9 changes: 2 additions & 7 deletions src/Core/Pipes/Filters/FilterMemberPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ namespace Summary.Pipes.Filters;
/// <summary>
/// A simple pipe that filters all members inside the document using the specified predicate.
/// </summary>
public class FilterMemberPipe : IPipe<Doc, Doc>
public class FilterMemberPipe(Predicate<DocMember> p) : IPipe<Doc, Doc>
{
private readonly Predicate<DocMember> _p ;

public FilterMemberPipe(Predicate<DocMember> p) =>
_p = p;

public Task<Doc> Run(Doc input) =>

Check warning on line 8 in src/Core/Pipes/Filters/FilterMemberPipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'FilterMemberPipe.Run(Doc)'

Check warning on line 8 in src/Core/Pipes/Filters/FilterMemberPipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'FilterMemberPipe.Run(Doc)'
Task.FromResult(new Doc(Filtered(input.Members)));

private DocMember[] Filtered(DocMember[] members) =>
members
.Where(x => _p(x))
.Where(x => p(x))
.Select(Filtered)
.ToArray();

Expand Down
13 changes: 2 additions & 11 deletions src/Core/Pipes/FoldPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@
/// <summary>
/// A <see cref="IPipe{I,O}" /> that aggregates the result of the specified pipe.
/// </summary>
public class FoldPipe<O> : IPipe<O[], O>
public class FoldPipe<O>(Func<O, O, O> fold, O @default) : IPipe<O[], O>
{
private readonly O _default;
private readonly Func<O, O, O> _fold;

public FoldPipe(Func<O, O, O> fold, O @default)
{
_fold = fold;
_default = @default;
}

public Task<O> Run(O[] input) =>

Check warning on line 8 in src/Core/Pipes/FoldPipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'FoldPipe<O>.Run(O[])'

Check warning on line 8 in src/Core/Pipes/FoldPipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'FoldPipe<O>.Run(O[])'
Task.FromResult(input.Length is 0 ? _default : input.Aggregate(_fold));
Task.FromResult(input.Length is 0 ? @default : input.Aggregate(fold));
}
13 changes: 4 additions & 9 deletions src/Core/Pipes/IO/CleanupDirPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ namespace Summary.Pipes.IO;
/// <summary>
/// Cleans up a given directory by deleting and re-creating it.
/// </summary>
public class CleanupDirPipe<I> : IPipe<I, I>
public class CleanupDirPipe<I>(string root) : IPipe<I, I>
{
private readonly string _root;

public CleanupDirPipe(string root) =>
_root = root;

public Task<I> Run(I input)

Check warning on line 8 in src/Core/Pipes/IO/CleanupDirPipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CleanupDirPipe<I>.Run(I)'

Check warning on line 8 in src/Core/Pipes/IO/CleanupDirPipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CleanupDirPipe<I>.Run(I)'
{
if (Directory.Exists(_root))
Directory.Delete(_root, recursive: true);
if (Directory.Exists(root))
Directory.Delete(root, recursive: true);

Directory.CreateDirectory(_root);
Directory.CreateDirectory(root);

return Task.FromResult(input);
}
Expand Down
15 changes: 3 additions & 12 deletions src/Core/Pipes/IO/SavePipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
/// <summary>
/// A <see cref="IPipe{I,O}" /> that saves the input to the file.
/// </summary>
public class SavePipe<I> : IPipe<I, Unit>
public class SavePipe<I>(string root, Func<I, (string Path, string Content)> file) : IPipe<I, Unit>
{
private readonly string _root;
private readonly Func<I, (string Path, string Content)> _file;

public SavePipe(string root, Func<I, (string Path, string Content)> file)
{
_root = root;
_file = file;
}

public async Task<Unit> Run(I input)

Check warning on line 8 in src/Core/Pipes/IO/SavePipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SavePipe<I>.Run(I)'

Check warning on line 8 in src/Core/Pipes/IO/SavePipe.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'SavePipe<I>.Run(I)'
{
var (path, content) = _file(input);
var (path, content) = file(input);

await File.WriteAllTextAsync(Path.Combine(_root, path), content).ConfigureAwait(continueOnCapturedContext: false);
await File.WriteAllTextAsync(Path.Combine(root, path), content).ConfigureAwait(continueOnCapturedContext: false);

return Unit.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@
/// <summary>
/// A <see cref="IPipe{I,O}"/> that searches specified directory (recursively) for files that match specified pattern.
/// </summary>
public class ScanDirectoryPipe : IPipe<Unit, string[]>
public class ScanPipe(string[] sources, string pattern) : IPipe<Unit, string[]>
{
private readonly string[] _sources;
private readonly string _pattern;

public ScanDirectoryPipe(string[] sources, string pattern)
{
_sources = sources;
_pattern = pattern;
}

public async Task<string[]> Run(Unit _)
{
// TODO: Consider refactoring this into more proper solution (@j.light).
if (_sources is [var root] && File.Exists(root))
if (sources is [var root] && File.Exists(root))
return new[] { await File.ReadAllTextAsync(root) };

var tasks = _sources
var tasks = sources
.SelectMany(x =>
Directory
.EnumerateFiles(x, _pattern, SearchOption.AllDirectories)
.EnumerateFiles(x, pattern, SearchOption.AllDirectories)
.Select(x => File.ReadAllTextAsync(x)));

return await Task.WhenAll(tasks).ConfigureAwait(false);
Expand Down
17 changes: 3 additions & 14 deletions src/Core/Pipes/Logging/LoggedPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,15 @@ namespace Summary.Pipes.Logging;
/// <remarks>
/// Logging is implemented by simply beginning a new scope with the given message.
/// </remarks>
public class LoggedPipe<I, O> : IPipe<I, O>
public class LoggedPipe<I, O>(IPipe<I, O> inner, ILogger logger, Func<I, string> message) : IPipe<I, O>
{
private readonly IPipe<I, O> _inner;
private readonly ILogger _logger;
private readonly Func<I, string> _message;

public LoggedPipe(IPipe<I, O> inner, ILogger logger, string message)
: this(inner, logger, _ => message) { }

public LoggedPipe(IPipe<I, O> inner, ILogger logger, Func<I, string> message)
{
_inner = inner;
_logger = logger;
_message = message;
}

public Task<O> Run(I input)
{
using var _ = _logger.BeginScope(_message(input));
using var _ = logger.BeginScope(message(input));

return _inner.Run(input);
return inner.Run(input);
}
}
15 changes: 3 additions & 12 deletions src/Core/Pipes/TeePipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
/// <summary>
/// A <see cref="IPipe{I,O}"/> that invokes an action on the output of the pipe each time it's executed.
/// </summary>
public class TeePipe<I, O> : IPipe<I, O>
public class TeePipe<I, O>(IPipe<I, O> inner, Action<O> tee) : IPipe<I, O>
{
private readonly IPipe<I, O> _inner;
private readonly Action<O> _tee;

public TeePipe(IPipe<I, O> inner, Action<O> tee)
{
_inner = inner;
_tee = tee;
}

public async Task<O> Run(I input)
{
var output = await _inner.Run(input).ConfigureAwait(false);
var output = await inner.Run(input).ConfigureAwait(false);

_tee(output);
tee(output);

return output;
}
Expand Down
15 changes: 3 additions & 12 deletions src/Core/Pipes/ThenForEach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@
/// <summary>
/// A <see cref="IPipe{I,O}"/> that aggregates the result of the specified pipe.
/// </summary>
public class ThenForEach<I, O1, O2> : IPipe<I, O2[]>
public class ThenForEach<I, O1, O2>(IPipe<I, O1[]> inner, IPipe<O1, O2> map) : IPipe<I, O2[]>
{
private readonly IPipe<I, O1[]> _inner;
private readonly IPipe<O1, O2> _map;

public ThenForEach(IPipe<I, O1[]> inner, IPipe<O1, O2> map)
{
_inner = inner;
_map = map;
}

public async Task<O2[]> Run(I input)
{
var os = await _inner.Run(input).ConfigureAwait(false);
var os = await inner.Run(input).ConfigureAwait(false);

return await Task.WhenAll(os.Select(_map.Run).ToArray()).ConfigureAwait(false);
return await Task.WhenAll(os.Select(map.Run).ToArray()).ConfigureAwait(false);
}
}
17 changes: 4 additions & 13 deletions src/Core/Pipes/ThenPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
/// <summary>
/// A <see cref="IPipe{I,O}"/> that composes two pipes together.
/// </summary>
public class ThenPipe<I, O1, O2> : IPipe<I, O2>
public class ThenPipe<I, O1, O2>(IPipe<I, O1> a, IPipe<O1, O2> b) : IPipe<I, O2>
{
private readonly IPipe<I, O1> _a;
private readonly IPipe<O1, O2> _b;

public ThenPipe(IPipe<I, O1> a, IPipe<O1, O2> b)
{
_a = a;
_b = b;
}

public async Task<O2> Run(I i)
{
var a = await _a.Run(i).ConfigureAwait(false);
var b = await _b.Run(a).ConfigureAwait(false);
var x = await a.Run(i).ConfigureAwait(false);
var y = await b.Run(x).ConfigureAwait(false);

return b;
return y;
}
}
18 changes: 9 additions & 9 deletions src/Plugins/Roslyn/Roslyn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="17.4.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.4.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Build" Version="17.8.3" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.8.3" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.6.10" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/Roslyn/RoslynPipelineExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public static SummaryPipeline UseRoslynParser(this SummaryPipeline self, string
/// </remarks>
public static SummaryPipeline UseRoslynParser(this SummaryPipeline self, string[] sources, string pattern = "*.cs") =>
self.ParseWith(options =>
new ScanDirectoryPipe(sources, pattern)
new ScanPipe(sources, pattern)
.ThenForEach(new ParseSyntaxTreePipe())
.ThenForEach(new ParseDocPipe())
.Logged(options.LoggerFactory, $"Parse directories [{sources.Select(x => $"'{x.AsFullPath()}'").Separated(with: ", ")}] with '{pattern}' pattern")
.Logged(options.LoggerFactory, $"Scan [{sources.Select(x => $"'{x.AsFullPath()}'").Separated(with: ", ")}] using '{pattern}' pattern")
.Then(new FoldPipe<Doc>(Doc.Merge, Doc.Empty).Logged(options.LoggerFactory, docs => $"Merge {docs.Length} files"))
.Then(new InlineInheritDocPipe().Logged(options.LoggerFactory, "Inline <inheritdoc> tags")));
}

0 comments on commit 4bf20e9

Please sign in to comment.