|
|
|
@ -14,7 +14,7 @@ namespace LightweightIocContainer.FactoryGenerator; |
|
|
|
public class FactoryGenerator : IIncrementalGenerator |
|
|
|
public class FactoryGenerator : IIncrementalGenerator |
|
|
|
{ |
|
|
|
{ |
|
|
|
private const string EXTENSION_CLASS_NAME = "FactoryExtensions"; |
|
|
|
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 GENERATED_FILE_HEADER = "//---GENERATED by FactoryGenerator! DO NOT EDIT!---"; |
|
|
|
private const string INDENT = " "; |
|
|
|
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))); |
|
|
|
context.RegisterPostInitializationOutput(c => c.AddSource($"{EXTENSION_CLASS_NAME}.g.cs", GenerateFactoryExtensionsClass(classNamespace, EXTENSION_CLASS_NAME))); |
|
|
|
|
|
|
|
|
|
|
|
IncrementalValuesProvider<ITypeSymbol?> syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(IsCallToGenerateFactory, GetTypeArgument); |
|
|
|
IncrementalValuesProvider<ITypeSymbol?> syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(IsCallToGenerateFactory, GetTypeArgument); |
|
|
|
|
|
|
|
|
|
|
|
context.RegisterSourceOutput(syntaxProvider.Collect(), GenerateTypeDependentClasses); |
|
|
|
context.RegisterSourceOutput(syntaxProvider.Collect(), GenerateTypeDependentClasses); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -35,8 +36,8 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
stringBuilder.AppendLine(GENERATED_FILE_HEADER); |
|
|
|
stringBuilder.AppendLine(GENERATED_FILE_HEADER); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Factories;"); |
|
|
|
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations;"); |
|
|
|
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations;"); |
|
|
|
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations.Fluent;"); |
|
|
|
|
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(classNamespace)) |
|
|
|
if (!string.IsNullOrEmpty(classNamespace)) |
|
|
|
@ -50,8 +51,8 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
|
|
|
|
|
|
|
stringBuilder.AppendLine($"{INDENT}public static IRegistrationBase WithGeneratedFactory<TFactory>(this IRegistrationBase registration)"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}public static IRegistrationBase WithGeneratedFactory<TFactory>(this IRegistrationBase registration)"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{{"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{{"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}TFactory factory = Builder.Create<TFactory>();"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}FactoryBuilder factoryBuilder = new();"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}registration.AddGeneratedFactory<TFactory>(factory);"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}registration.AddGeneratedFactory<TFactory>(factoryBuilder);"); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}return registration;"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}return registration;"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}}}"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}}}"); |
|
|
|
@ -94,6 +95,14 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
string? classNamespace = typeof(FactoryGenerator).Namespace; |
|
|
|
string? classNamespace = typeof(FactoryGenerator).Namespace; |
|
|
|
context.AddSource($"{BUILDER_CLASS_NAME}.g.cs", GenerateBuilderClassSourceCode(classNamespace, types)); |
|
|
|
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<ITypeSymbol?> types) |
|
|
|
private string GenerateBuilderClassSourceCode(string? classNamespace, ImmutableArray<ITypeSymbol?> types) |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -102,6 +111,9 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
stringBuilder.AppendLine(GENERATED_FILE_HEADER); |
|
|
|
stringBuilder.AppendLine(GENERATED_FILE_HEADER); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Factories;"); |
|
|
|
|
|
|
|
stringBuilder.AppendLine("using LightweightIocContainer.Factories;"); |
|
|
|
|
|
|
|
|
|
|
|
foreach (string typeNamespace in GetNamespacesOfTypes(types)) |
|
|
|
foreach (string typeNamespace in GetNamespacesOfTypes(types)) |
|
|
|
stringBuilder.AppendLine($"using {typeNamespace};"); |
|
|
|
stringBuilder.AppendLine($"using {typeNamespace};"); |
|
|
|
|
|
|
|
|
|
|
|
@ -113,9 +125,9 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
stringBuilder.AppendLine("public static class Builder"); |
|
|
|
stringBuilder.AppendLine("public class FactoryBuilder : IFactoryBuilder"); |
|
|
|
stringBuilder.AppendLine("{"); |
|
|
|
stringBuilder.AppendLine("{"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}public static TFactory Create<TFactory>()"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}public TFactory Create<TFactory>(IocContainer container)"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{{"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{{"); |
|
|
|
|
|
|
|
|
|
|
|
foreach (ITypeSymbol? type in types) |
|
|
|
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}if (typeof(TFactory) == typeof({type.Name}))"); |
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}{{"); |
|
|
|
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($"{INDENT}{INDENT}}}"); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
stringBuilder.AppendLine(); |
|
|
|
} |
|
|
|
} |
|
|
|
|