Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate separate registrations #48

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Fluss.Regen/Generators/RegistrationSyntaxGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void WriteEndNamespace()

public void WriteBeginClass()
{
_writer.WriteIndentedLine("public static partial class {0}ServiceCollectionExtensions {{", _moduleName);
_writer.WriteIndentedLine("public static partial class {0}ComponentsServiceCollectionExtensions {{", _moduleName);
_writer.IncreaseIndent();
}

Expand All @@ -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
98 changes: 73 additions & 25 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())
{
generator.WriteBeginRegistrationMethod("Policies");
foreach (var policy in policies)
{
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())
{
switch (syntaxInfo)
generator.WriteBeginRegistrationMethod("Upcasters");
foreach (var upcaster in upcasters)
{
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.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
1 change: 0 additions & 1 deletion src/Fluss.UnitTest/Regen/SelectorGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Fluss.Events;
using Fluss.Regen;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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 @@ -8,9 +8,14 @@

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 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

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 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;
}
}
}
Loading