From 015aec04349f1056da7f1aaf46ac7075a3ea7bc4 Mon Sep 17 00:00:00 2001 From: mvarendorff Date: Tue, 1 Oct 2024 12:38:15 +0200 Subject: [PATCH] feat: generate separate registrations --- .../Generators/RegistrationSyntaxGenerator.cs | 17 ++- src/Fluss.Regen/SelectorGenerator.cs | 100 +++++++++++++----- ...dator.DotNet8_0#Registration.g.verified.cs | 9 +- ...olicy.DotNet8_0#Registration.g.verified.cs | 9 +- ...ffect.DotNet8_0#Registration.g.verified.cs | 9 +- ...aster.DotNet8_0#Registration.g.verified.cs | 9 +- ...dator.DotNet8_0#Registration.g.verified.cs | 9 +- 7 files changed, 122 insertions(+), 40 deletions(-) diff --git a/src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.cs b/src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.cs index 066ca73..a9e7c3b 100644 --- a/src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.cs +++ b/src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.cs @@ -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) @@ -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(); diff --git a/src/Fluss.Regen/SelectorGenerator.cs b/src/Fluss.Regen/SelectorGenerator.cs index fd60fe0..589b319 100644 --- a/src/Fluss.Regen/SelectorGenerator.cs +++ b/src/Fluss.Regen/SelectorGenerator.cs @@ -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().ToImmutableHashSet(); + var eventValidators = syntaxInfos.OfType().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().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().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().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(); diff --git a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForEventValidator.DotNet8_0#Registration.g.verified.cs b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForEventValidator.DotNet8_0#Registration.g.verified.cs index 8146b72..1156c40 100644 --- a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForEventValidator.DotNet8_0#Registration.g.verified.cs +++ b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForEventValidator.DotNet8_0#Registration.g.verified.cs @@ -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(sc); return sc; } + + public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) { + AddSelectorGeneratorTestsESValidators(sc); + return sc; + } } } diff --git a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForPolicy.DotNet8_0#Registration.g.verified.cs b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForPolicy.DotNet8_0#Registration.g.verified.cs index 4459032..2df774e 100644 --- a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForPolicy.DotNet8_0#Registration.g.verified.cs +++ b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForPolicy.DotNet8_0#Registration.g.verified.cs @@ -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(sc); return sc; } + + public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) { + AddSelectorGeneratorTestsESPolicies(sc); + return sc; + } } } diff --git a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForSideEffect.DotNet8_0#Registration.g.verified.cs b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForSideEffect.DotNet8_0#Registration.g.verified.cs index 1047a25..183e298 100644 --- a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForSideEffect.DotNet8_0#Registration.g.verified.cs +++ b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForSideEffect.DotNet8_0#Registration.g.verified.cs @@ -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(sc); return sc; } + + public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) { + AddSelectorGeneratorTestsESSideEffects(sc); + return sc; + } } } diff --git a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForUpcaster.DotNet8_0#Registration.g.verified.cs b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForUpcaster.DotNet8_0#Registration.g.verified.cs index eb21806..16418fd 100644 --- a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForUpcaster.DotNet8_0#Registration.g.verified.cs +++ b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GenerateForUpcaster.DotNet8_0#Registration.g.verified.cs @@ -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(sc); return sc; } + + public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) { + AddSelectorGeneratorTestsESUpcasters(sc); + return sc; + } } } diff --git a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GeneratesForAggregateValidator.DotNet8_0#Registration.g.verified.cs b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GeneratesForAggregateValidator.DotNet8_0#Registration.g.verified.cs index cb12f1a..0d65617 100644 --- a/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GeneratesForAggregateValidator.DotNet8_0#Registration.g.verified.cs +++ b/src/Fluss.UnitTest/Regen/Snapshots/SelectorGeneratorTests.GeneratesForAggregateValidator.DotNet8_0#Registration.g.verified.cs @@ -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(sc); return sc; } + + public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddSelectorGeneratorTestsESComponents(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection sc) { + AddSelectorGeneratorTestsESValidators(sc); + return sc; + } } }