Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
179 additions
and
4 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
79 changes: 79 additions & 0 deletions
79
src/components/tbc.host/Components/GlobalUsingsResolver/GlobalUsingsResolver.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,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.IO.Abstractions; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Tbc.Host.Components.Abstractions; | ||
using Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
namespace Tbc.Host.Components.GlobalUsingsResolver; | ||
|
||
public class GlobalUsingsResolver : ComponentBase<GlobalUsingsResolver>, IGlobalUsingsResolver | ||
{ | ||
private IFileSystem _fileSystem; | ||
|
||
public GlobalUsingsResolver(ILogger<GlobalUsingsResolver> logger, IFileSystem fileSystem) : base(logger) | ||
{ | ||
_fileSystem = fileSystem; | ||
} | ||
|
||
public async Task<ResolveGlobalUsingsResponse> ResolveGlobalUsings(ResolveGlobalUsingsRequest request, CancellationToken canceller = default) | ||
{ | ||
var sources = request.Sources; | ||
var usings = ImmutableList.Create<string>(); | ||
var diagnostics = ImmutableDictionary.Create<string, object>(); | ||
|
||
foreach (var source in sources) | ||
{ | ||
var (newUsings, newDiagnostics) = | ||
source.Kind switch | ||
{ | ||
GlobalUsingsSourceKind.Text => GetUsingsFromText(source.Reference), | ||
GlobalUsingsSourceKind.SearchPath => GetUsingsFromSearchPath(source.Reference, source.Context), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
|
||
usings = usings.AddRange(newUsings); | ||
diagnostics = diagnostics.AddRange(newDiagnostics); | ||
} | ||
|
||
usings = usings.Distinct().ToImmutableList(); | ||
|
||
return new ResolveGlobalUsingsResponse( | ||
sources, usings.Distinct().ToImmutableList(), | ||
usings.Any() ? String.Join(Environment.NewLine, usings) : null, | ||
diagnostics); | ||
} | ||
|
||
public (List<string> Usings, Dictionary<string, object> Diagnostics) GetUsingsFromText(string text) | ||
{ | ||
try | ||
{ | ||
var usings = text.Split(';').Select(x => $"global using global::{x}").ToList(); | ||
|
||
return new(usings, new() { [text] = $"{usings.Count} usings extracted" }); | ||
} | ||
catch (Exception ex) { return (new(), new() { [text] = ex }); } | ||
} | ||
|
||
public (List<string> Usings, Dictionary<string, object> Diagnostics) GetUsingsFromSearchPath( | ||
string path, string maybeResolutionMethod) | ||
{ | ||
maybeResolutionMethod ??= KnownGlobalUsingSearchPathResolutionApproach.LastModified; | ||
|
||
var matches = _fileSystem.Directory.GetFiles(path, "*.GlobalUsings.g.cs", SearchOption.AllDirectories) | ||
.OrderByDescending(x => _fileSystem.FileInfo.FromFileName(x).LastWriteTime) | ||
.ToList(); | ||
|
||
var usings = matches | ||
.Take(maybeResolutionMethod == KnownGlobalUsingSearchPathResolutionApproach.LastModified ? 1 : Int32.MaxValue) | ||
.Select(f => (f, u: _fileSystem.File.ReadAllLines(f).Where(x => x.StartsWith("global using ")).ToList())) | ||
.ToList(); | ||
|
||
return (usings.SelectMany(x => x.u).ToList(), usings.ToDictionary(x => x.f, x => (object) $"{x.u.Count} usings extracted")); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/components/tbc.host/Components/GlobalUsingsResolver/IGlobalUsingsResolver.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,10 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
namespace Tbc.Host.Components.GlobalUsingsResolver; | ||
|
||
public interface IGlobalUsingsResolver | ||
{ | ||
Task<ResolveGlobalUsingsResponse> ResolveGlobalUsings(ResolveGlobalUsingsRequest request, CancellationToken canceller = default); | ||
} |
3 changes: 3 additions & 0 deletions
3
src/components/tbc.host/Components/GlobalUsingsResolver/Models/GlobalUsingsSource.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,3 @@ | ||
namespace Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
public record GlobalUsingsSource(GlobalUsingsSourceKind Kind, string Reference, string? Context = null); |
7 changes: 7 additions & 0 deletions
7
src/components/tbc.host/Components/GlobalUsingsResolver/Models/GlobalUsingsSourceKind.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,7 @@ | ||
namespace Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
public enum GlobalUsingsSourceKind | ||
{ | ||
Text, | ||
SearchPath, | ||
} |
7 changes: 7 additions & 0 deletions
7
...st/Components/GlobalUsingsResolver/Models/KnownGlobalUsingSearchPathResolutionApproach.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,7 @@ | ||
namespace Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
public static class KnownGlobalUsingSearchPathResolutionApproach | ||
{ | ||
public const string LastModified = nameof(LastModified); | ||
public const string MergeAll = nameof(MergeAll); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/components/tbc.host/Components/GlobalUsingsResolver/Models/ResolveGlobalUsingsRequest.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,5 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
public record ResolveGlobalUsingsRequest(List<GlobalUsingsSource> Sources); |
12 changes: 12 additions & 0 deletions
12
...components/tbc.host/Components/GlobalUsingsResolver/Models/ResolveGlobalUsingsResponse.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,12 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
|
||
namespace Tbc.Host.Components.GlobalUsingsResolver.Models; | ||
|
||
public record ResolveGlobalUsingsResponse | ||
( | ||
List<GlobalUsingsSource> Sources, | ||
ImmutableList<string> Usings, | ||
string? UsingsSource, | ||
ImmutableDictionary<string, object> Diagnostics | ||
); |
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