diff --git a/LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs b/LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs index 625c527..3d63744 100644 --- a/LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs +++ b/LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs @@ -14,7 +14,7 @@ namespace LightweightIocContainer.FactoryGenerator; public class FactoryGenerator : IIncrementalGenerator { private const string EXTENSION_CLASS_NAME = "FactoryExtensions"; - private const string BUILDER_CLASS_NAME = "GeneratedFactoryBuilder"; + private const string BUILDER_CLASS_NAME = "FactoryBuilder"; private const string GENERATED_FILE_HEADER = "//---GENERATED by FactoryGenerator! DO NOT EDIT!---"; private const string INDENT = " "; @@ -25,6 +25,7 @@ public class FactoryGenerator : IIncrementalGenerator context.RegisterPostInitializationOutput(c => c.AddSource($"{EXTENSION_CLASS_NAME}.g.cs", GenerateFactoryExtensionsClass(classNamespace, EXTENSION_CLASS_NAME))); IncrementalValuesProvider syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(IsCallToGenerateFactory, GetTypeArgument); + context.RegisterSourceOutput(syntaxProvider.Collect(), GenerateTypeDependentClasses); } @@ -35,8 +36,8 @@ public class FactoryGenerator : IIncrementalGenerator stringBuilder.AppendLine(GENERATED_FILE_HEADER); stringBuilder.AppendLine(); + stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Factories;"); stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations;"); - stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations.Fluent;"); stringBuilder.AppendLine(); if (!string.IsNullOrEmpty(classNamespace)) @@ -50,8 +51,8 @@ public class FactoryGenerator : IIncrementalGenerator stringBuilder.AppendLine($"{INDENT}public static IRegistrationBase WithGeneratedFactory(this IRegistrationBase registration)"); stringBuilder.AppendLine($"{INDENT}{{"); - stringBuilder.AppendLine($"{INDENT}{INDENT}TFactory factory = Builder.Create();"); - stringBuilder.AppendLine($"{INDENT}{INDENT}registration.AddGeneratedFactory(factory);"); + stringBuilder.AppendLine($"{INDENT}{INDENT}FactoryBuilder factoryBuilder = new();"); + stringBuilder.AppendLine($"{INDENT}{INDENT}registration.AddGeneratedFactory(factoryBuilder);"); stringBuilder.AppendLine(); stringBuilder.AppendLine($"{INDENT}{INDENT}return registration;"); stringBuilder.AppendLine($"{INDENT}}}"); @@ -94,6 +95,14 @@ public class FactoryGenerator : IIncrementalGenerator string? classNamespace = typeof(FactoryGenerator).Namespace; context.AddSource($"{BUILDER_CLASS_NAME}.g.cs", GenerateBuilderClassSourceCode(classNamespace, types)); } + + private void GenerateFactory(SourceProductionContext context, ITypeSymbol? typeSymbol) + { + if (typeSymbol is null) + return; + + context.AddSource($"Generated{typeSymbol.Name}.g.cs", GenerateFactorySourceCode(typeSymbol)); + } private string GenerateBuilderClassSourceCode(string? classNamespace, ImmutableArray types) { @@ -102,6 +111,9 @@ public class FactoryGenerator : IIncrementalGenerator stringBuilder.AppendLine(GENERATED_FILE_HEADER); stringBuilder.AppendLine(); + stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Factories;"); + stringBuilder.AppendLine("using LightweightIocContainer.Factories;"); + foreach (string typeNamespace in GetNamespacesOfTypes(types)) stringBuilder.AppendLine($"using {typeNamespace};"); @@ -113,9 +125,9 @@ public class FactoryGenerator : IIncrementalGenerator stringBuilder.AppendLine(); } - stringBuilder.AppendLine("public static class Builder"); + stringBuilder.AppendLine("public class FactoryBuilder : IFactoryBuilder"); stringBuilder.AppendLine("{"); - stringBuilder.AppendLine($"{INDENT}public static TFactory Create()"); + stringBuilder.AppendLine($"{INDENT}public TFactory Create(IocContainer container)"); stringBuilder.AppendLine($"{INDENT}{{"); foreach (ITypeSymbol? type in types) @@ -125,7 +137,7 @@ public class FactoryGenerator : IIncrementalGenerator stringBuilder.AppendLine($"{INDENT}{INDENT}if (typeof(TFactory) == typeof({type.Name}))"); stringBuilder.AppendLine($"{INDENT}{INDENT}{{"); - stringBuilder.AppendLine($"{INDENT}{INDENT}{INDENT}return (TFactory) (object) new Generated{type.Name}();"); + stringBuilder.AppendLine($"{INDENT}{INDENT}{INDENT}return (TFactory) (object) new Generated{type.Name}(container, new FactoryHelper());"); stringBuilder.AppendLine($"{INDENT}{INDENT}}}"); stringBuilder.AppendLine(); }