-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace assembly scanning for registration with codegeneration
- Loading branch information
1 parent
a5653f8
commit bedbf72
Showing
47 changed files
with
999 additions
and
184 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
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
112 changes: 112 additions & 0 deletions
112
src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.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,112 @@ | ||
using System; | ||
using System.Text; | ||
using Fluss.Regen.Helpers; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Fluss.Regen.Generators; | ||
|
||
public sealed class RegistrationSyntaxGenerator : IDisposable | ||
{ | ||
private readonly string _moduleName; | ||
private readonly string _ns; | ||
private StringBuilder _sb; | ||
private CodeWriter _writer; | ||
private bool _disposed; | ||
|
||
public RegistrationSyntaxGenerator(string moduleName, string ns) | ||
{ | ||
_moduleName = moduleName; | ||
_ns = ns; | ||
_sb = StringBuilderPool.Get(); | ||
_writer = new CodeWriter(_sb); | ||
} | ||
|
||
public void WriteHeader() | ||
{ | ||
_writer.WriteFileHeader(); | ||
_writer.WriteLine(); | ||
} | ||
|
||
public void WriteBeginNamespace() | ||
{ | ||
_writer.WriteIndentedLine("namespace {0} {{", _ns); | ||
_writer.IncreaseIndent(); | ||
} | ||
|
||
public void WriteEndNamespace() | ||
{ | ||
_writer.DecreaseIndent(); | ||
_writer.WriteIndentedLine("}"); | ||
} | ||
|
||
public void WriteBeginClass() | ||
{ | ||
_writer.WriteIndentedLine("public static partial class {0}ServiceCollectionExtensions {{", _moduleName); | ||
_writer.IncreaseIndent(); | ||
} | ||
|
||
public void WriteEndClass() | ||
{ | ||
_writer.DecreaseIndent(); | ||
_writer.WriteIndentedLine("}"); | ||
} | ||
|
||
public void WriteBeginRegistrationMethod() | ||
{ | ||
_writer.WriteIndentedLine( | ||
"public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection Add{0}(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {{", | ||
_moduleName); | ||
_writer.IncreaseIndent(); | ||
} | ||
|
||
public void WriteEndRegistrationMethod() | ||
{ | ||
_writer.WriteIndentedLine("return sc;"); | ||
_writer.DecreaseIndent(); | ||
_writer.WriteIndentedLine("}"); | ||
} | ||
|
||
public void WriteAggregateValidatorRegistration(string aggregateValidatorType) | ||
{ | ||
_writer.WriteIndentedLine("global::Fluss.Validation.ValidationServiceCollectionExtension.AddAggregateValidator<{0}>(sc);", aggregateValidatorType); | ||
} | ||
|
||
public void WriteEventValidatorRegistration(string eventValidatorType) | ||
{ | ||
_writer.WriteIndentedLine("global::Fluss.Validation.ValidationServiceCollectionExtension.AddEventValidator<{0}>(sc);", eventValidatorType); | ||
} | ||
|
||
public void WritePolicyRegistration(string policyType) | ||
{ | ||
_writer.WriteIndentedLine("global::Fluss.Authentication.ServiceCollectionExtensions.AddPolicy<{0}>(sc);", policyType); | ||
} | ||
|
||
public void WriteSideEffectRegistration(string sideEffectType) | ||
{ | ||
_writer.WriteIndentedLine("global::Fluss.SideEffects.SideEffectsServiceCollectionExtension.AddSideEffect<{0}>(sc);", sideEffectType); | ||
} | ||
|
||
public void WriteUpcasterRegistration(string upcasterType) | ||
{ | ||
_writer.WriteIndentedLine("global::Fluss.ServiceCollectionExtensions.AddUpcaster<{0}>(sc);", upcasterType); | ||
} | ||
|
||
public override string ToString() | ||
=> _sb.ToString(); | ||
|
||
public SourceText ToSourceText() | ||
=> SourceText.From(ToString(), Encoding.UTF8); | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
StringBuilderPool.Return(_sb); | ||
_sb = default!; | ||
_writer = default!; | ||
_disposed = true; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Fluss.Regen.Inspectors; | ||
|
||
public sealed class AggregateValidatorInfo( | ||
INamedTypeSymbol classSymbol, | ||
ClassDeclarationSyntax classSyntax) | ||
: ISyntaxInfo | ||
{ | ||
public INamedTypeSymbol Type { get; } = classSymbol; | ||
private ClassDeclarationSyntax ClassSyntax { get; } = classSyntax; | ||
|
||
public bool Equals(AggregateValidatorInfo? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return ClassSyntax.Equals(other.ClassSyntax); | ||
} | ||
|
||
public bool Equals(ISyntaxInfo? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return other is AggregateValidatorInfo info && Equals(info); | ||
} | ||
|
||
public override bool Equals(object? obj) => | ||
ReferenceEquals(this, obj) || (obj is AggregateValidatorInfo other && Equals(other)); | ||
|
||
public override int GetHashCode() => ClassSyntax.GetHashCode(); | ||
} |
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,29 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Fluss.Regen.Inspectors; | ||
|
||
public sealed class AggregateValidatorInspector : ISyntaxInspector | ||
{ | ||
public bool TryHandle( | ||
GeneratorSyntaxContext context, | ||
[NotNullWhen(true)] out ISyntaxInfo? syntaxInfo) | ||
{ | ||
if (context.Node is ClassDeclarationSyntax classSyntax) | ||
{ | ||
var symbol = context.SemanticModel.GetDeclaredSymbol(classSyntax); | ||
if (symbol is INamedTypeSymbol classSymbol && | ||
classSymbol.AllInterfaces.Any(i => i.ToDisplayString().StartsWith("Fluss.Validation.AggregateValidator"))) | ||
{ | ||
syntaxInfo = new AggregateValidatorInfo(classSymbol, classSyntax); | ||
return true; | ||
} | ||
} | ||
|
||
syntaxInfo = null; | ||
return false; | ||
} | ||
} |
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,32 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Fluss.Regen.Inspectors; | ||
|
||
public sealed class EventValidatorInfo( | ||
INamedTypeSymbol classSymbol, | ||
ClassDeclarationSyntax classSyntax) | ||
: ISyntaxInfo | ||
{ | ||
public INamedTypeSymbol Type { get; } = classSymbol; | ||
private ClassDeclarationSyntax ClassSyntax { get; } = classSyntax; | ||
|
||
public bool Equals(EventValidatorInfo? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return ClassSyntax.Equals(other.ClassSyntax); | ||
} | ||
|
||
public bool Equals(ISyntaxInfo? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return other is EventValidatorInfo info && Equals(info); | ||
} | ||
|
||
public override bool Equals(object? obj) => | ||
ReferenceEquals(this, obj) || (obj is EventValidatorInfo other && Equals(other)); | ||
|
||
public override int GetHashCode() => ClassSyntax.GetHashCode(); | ||
} |
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,29 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Fluss.Regen.Inspectors; | ||
|
||
public sealed class EventValidatorInspector : ISyntaxInspector | ||
{ | ||
public bool TryHandle( | ||
GeneratorSyntaxContext context, | ||
[NotNullWhen(true)] out ISyntaxInfo? syntaxInfo) | ||
{ | ||
if (context.Node is ClassDeclarationSyntax classSyntax) | ||
{ | ||
var symbol = context.SemanticModel.GetDeclaredSymbol(classSyntax); | ||
if (symbol is INamedTypeSymbol classSymbol && | ||
classSymbol.AllInterfaces.Any(i => i.ToDisplayString().StartsWith("Fluss.Validation.EventValidator<"))) | ||
{ | ||
syntaxInfo = new EventValidatorInfo(classSymbol, classSyntax); | ||
return true; | ||
} | ||
} | ||
|
||
syntaxInfo = null; | ||
return false; | ||
} | ||
} |
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,32 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Fluss.Regen.Inspectors; | ||
|
||
public sealed class PolicyInfo( | ||
INamedTypeSymbol classSymbol, | ||
ClassDeclarationSyntax classSyntax) | ||
: ISyntaxInfo | ||
{ | ||
public INamedTypeSymbol Type { get; } = classSymbol; | ||
private ClassDeclarationSyntax ClassSyntax { get; } = classSyntax; | ||
|
||
public bool Equals(PolicyInfo? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return ClassSyntax.Equals(other.ClassSyntax); | ||
} | ||
|
||
public bool Equals(ISyntaxInfo? other) | ||
{ | ||
if (ReferenceEquals(null, other)) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return other is PolicyInfo info && Equals(info); | ||
} | ||
|
||
public override bool Equals(object? obj) => | ||
ReferenceEquals(this, obj) || (obj is PolicyInfo other && Equals(other)); | ||
|
||
public override int GetHashCode() => ClassSyntax.GetHashCode(); | ||
} |
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,29 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Fluss.Regen.Inspectors; | ||
|
||
public sealed class PolicyInspector : ISyntaxInspector | ||
{ | ||
public bool TryHandle( | ||
GeneratorSyntaxContext context, | ||
[NotNullWhen(true)] out ISyntaxInfo? syntaxInfo) | ||
{ | ||
if (context.Node is ClassDeclarationSyntax classSyntax) | ||
{ | ||
var symbol = context.SemanticModel.GetDeclaredSymbol(classSyntax); | ||
if (symbol is INamedTypeSymbol classSymbol && | ||
classSymbol.AllInterfaces.Any(i => i.ToDisplayString() == "Fluss.Authentication.Policy")) | ||
{ | ||
syntaxInfo = new PolicyInfo(classSymbol, classSyntax); | ||
return true; | ||
} | ||
} | ||
|
||
syntaxInfo = null; | ||
return false; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -62,5 +62,4 @@ public override int GetHashCode() | |
return hashCode; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.