Skip to content

Commit

Permalink
feat: generate separate registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarendorff2 committed Oct 1, 2024
1 parent 949242e commit 015aec0
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 40 deletions.
17 changes: 13 additions & 4 deletions src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,23 @@ public void WriteEndClass()
_writer.WriteIndentedLine("}");
}

public void WriteBeginRegistrationMethod()
public void WriteBeginRegistrationMethod(string componentType)
{
_writer.WriteIndentedLine(
"public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection Add{0}(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {{",
_moduleName);
"public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection Add{0}{1}(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {{",
_moduleName, componentType);
_writer.IncreaseIndent();
}

public void WriteEndRegistrationMethod()
public void WriteEndRegistrationMethod(bool includeNewLine = true)
{
_writer.WriteIndentedLine("return sc;");
_writer.DecreaseIndent();
_writer.WriteIndentedLine("}");
if (includeNewLine)
{
_writer.WriteLine();
}
}

public void WriteAggregateValidatorRegistration(string aggregateValidatorType)
Expand Down Expand Up @@ -91,6 +95,11 @@ public void WriteUpcasterRegistration(string upcasterType)
_writer.WriteIndentedLine("global::Fluss.ServiceCollectionExtensions.AddUpcaster<{0}>(sc);", upcasterType);
}

public void WriteComponentRegistration(string componentName)
{
_writer.WriteIndentedLine("Add{0}{1}(sc);", _moduleName, componentName);
}

public override string ToString()
=> _sb.ToString();

Expand Down
100 changes: 74 additions & 26 deletions src/Fluss.Regen/SelectorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,45 +190,93 @@ private static void WriteRegistration(
return;
}

var moduleName = (compilation.AssemblyName ?? "Assembly").Split('.').Last() + "ESComponents";
var moduleName = (compilation.AssemblyName ?? "Assembly").Split('.').Last() + "ES";

using var generator = new RegistrationSyntaxGenerator(moduleName, "Microsoft.Extensions.DependencyInjection");

generator.WriteHeader();
generator.WriteBeginNamespace();
generator.WriteBeginClass();
generator.WriteBeginRegistrationMethod();


var foundInfo = false;

foreach (var syntaxInfo in syntaxInfos)
var aggregateValidators = syntaxInfos.OfType<AggregateValidatorInfo>().ToImmutableHashSet();
var eventValidators = syntaxInfos.OfType<EventValidatorInfo>().ToImmutableHashSet();
if (aggregateValidators.Any() || eventValidators.Any())
{
generator.WriteBeginRegistrationMethod("Validators");

foreach (var aggregateValidator in aggregateValidators)
{
generator.WriteAggregateValidatorRegistration(aggregateValidator.Type.ToFullyQualified());
}
foreach (var eventValidator in eventValidators)
{
generator.WriteEventValidatorRegistration(eventValidator.Type.ToFullyQualified());
}

generator.WriteEndRegistrationMethod();
foundInfo = true;
}

var policies = syntaxInfos.OfType<PolicyInfo>().ToImmutableHashSet();
if (policies.Any())
{
switch (syntaxInfo)
generator.WriteBeginRegistrationMethod("Policies");
foreach (var policy in policies)
{
case AggregateValidatorInfo aggregateValidatorInfo:
generator.WriteAggregateValidatorRegistration(aggregateValidatorInfo.Type.ToFullyQualified());
foundInfo = true;
break;
case EventValidatorInfo eventValidatorInfo:
generator.WriteEventValidatorRegistration(eventValidatorInfo.Type.ToFullyQualified());
foundInfo = true;
break;
case PolicyInfo policyInfo:
generator.WritePolicyRegistration(policyInfo.Type.ToFullyQualified());
foundInfo = true;
break;
case SideEffectInfo sideEffectInfo:
generator.WriteSideEffectRegistration(sideEffectInfo.Type.ToFullyQualified());
foundInfo = true;
break;
case UpcasterInfo upcasterInfo:
generator.WriteUpcasterRegistration(upcasterInfo.Type.ToFullyQualified());
foundInfo = true;
break;
generator.WritePolicyRegistration(policy.Type.ToFullyQualified());
}
generator.WriteEndRegistrationMethod();
foundInfo = true;
}

var sideEffects = syntaxInfos.OfType<SideEffectInfo>().ToImmutableHashSet();
if (sideEffects.Any())
{
generator.WriteBeginRegistrationMethod("SideEffects");
foreach (var sideEffect in sideEffects)
{
generator.WriteSideEffectRegistration(sideEffect.Type.ToFullyQualified());
}
generator.WriteEndRegistrationMethod();
foundInfo = true;
}

var upcasters = syntaxInfos.OfType<UpcasterInfo>().ToImmutableHashSet();
if (upcasters.Any())
{
generator.WriteBeginRegistrationMethod("Upcasters");
foreach (var upcaster in upcasters)
{
generator.WriteUpcasterRegistration(upcaster.Type.ToFullyQualified());
}
generator.WriteEndRegistrationMethod();
foundInfo = true;
}

generator.WriteBeginRegistrationMethod("Components");
if (aggregateValidators.Any() || eventValidators.Any())
{
generator.WriteComponentRegistration("Validators");
}

if (policies.Any())
{
generator.WriteComponentRegistration("Policies");
}

if (sideEffects.Any())
{
generator.WriteComponentRegistration("SideEffects");
}

if (upcasters.Any())
{
generator.WriteComponentRegistration("Upcasters");
}

generator.WriteEndRegistrationMethod();
generator.WriteEndRegistrationMethod(false);
generator.WriteEndClass();
generator.WriteEndNamespace();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
using System.Runtime.CompilerServices;

namespace Microsoft.Extensions.DependencyInjection {
public static partial class SelectorGeneratorTestsESComponentsServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
public static partial class SelectorGeneratorTestsESServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESValidators(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
global::Fluss.Validation.ValidationServiceCollectionExtension.AddEventValidator<global::TestNamespace.TestEventValidator>(sc);
return sc;
}

public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
AddSelectorGeneratorTestsESValidators(sc);
return sc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
using System.Runtime.CompilerServices;

namespace Microsoft.Extensions.DependencyInjection {
public static partial class SelectorGeneratorTestsESComponentsServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
public static partial class SelectorGeneratorTestsESServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESPolicies(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
global::Fluss.Authentication.ServiceCollectionExtensions.AddPolicy<global::TestNamespace.TestPolicy>(sc);
return sc;
}

public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
AddSelectorGeneratorTestsESPolicies(sc);
return sc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
using System.Runtime.CompilerServices;

namespace Microsoft.Extensions.DependencyInjection {
public static partial class SelectorGeneratorTestsESComponentsServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
public static partial class SelectorGeneratorTestsESServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESSideEffects(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
global::Fluss.SideEffects.SideEffectsServiceCollectionExtension.AddSideEffect<global::TestNamespace.TestSideEffect>(sc);
return sc;
}

public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
AddSelectorGeneratorTestsESSideEffects(sc);
return sc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
using System.Runtime.CompilerServices;

namespace Microsoft.Extensions.DependencyInjection {
public static partial class SelectorGeneratorTestsESComponentsServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
public static partial class SelectorGeneratorTestsESServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESUpcasters(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
global::Fluss.ServiceCollectionExtensions.AddUpcaster<global::TestNamespace.TestUpcaster>(sc);
return sc;
}

public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
AddSelectorGeneratorTestsESUpcasters(sc);
return sc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
using System.Runtime.CompilerServices;

namespace Microsoft.Extensions.DependencyInjection {
public static partial class SelectorGeneratorTestsESComponentsServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
public static partial class SelectorGeneratorTestsESServiceCollectionExtensions {
public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESValidators(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
global::Fluss.Validation.ValidationServiceCollectionExtension.AddAggregateValidator<global::TestNamespace.TestAggregateValidator>(sc);
return sc;
}

public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) {
AddSelectorGeneratorTestsESValidators(sc);
return sc;
}
}
}

0 comments on commit 015aec0

Please sign in to comment.