- don't generate a file twice for the same factory

pull/62/head
Simon G. 3 days ago
parent 0a006ae787
commit b5f16126fe
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 15
      LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs

@ -29,7 +29,7 @@ public class FactoryGenerator : IIncrementalGenerator
IncrementalValuesProvider<ITypeSymbol?> syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(IsCallToGenerateFactory, GetTypeArgument);
context.RegisterSourceOutput(syntaxProvider.Collect(), GenerateTypeDependentClasses);
context.RegisterSourceOutput(syntaxProvider, GenerateFactory);
context.RegisterSourceOutput(syntaxProvider.Collect(), GenerateFactory);
}
private string GenerateFactoryExtensionsClass(string? classNamespace, string className)
@ -99,13 +99,16 @@ public class FactoryGenerator : IIncrementalGenerator
context.AddSource($"{BUILDER_CLASS_NAME}.g.cs", GenerateBuilderClassSourceCode(classNamespace, types));
}
private void GenerateFactory(SourceProductionContext context, ITypeSymbol? typeSymbol)
private void GenerateFactory(SourceProductionContext context, ImmutableArray<ITypeSymbol?> types)
{
if (typeSymbol is null)
return;
foreach (ISymbol? symbol in types.Distinct(SymbolEqualityComparer.IncludeNullability))
{
if (symbol is not ITypeSymbol typeSymbol)
continue;
context.AddSource($"Generated{typeSymbol.Name}.g.cs", GenerateFactorySourceCode(typeSymbol));
}
}
private string GenerateBuilderClassSourceCode(string? classNamespace, ImmutableArray<ITypeSymbol?> types)
{
@ -132,9 +135,9 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine($"{INDENT}public TFactory Create<TFactory>(IocContainer container)");
stringBuilder.AppendLine($"{INDENT}{{");
foreach (ITypeSymbol? type in types)
foreach (ISymbol? symbol in types.Distinct(SymbolEqualityComparer.IncludeNullability))
{
if (type is null)
if (symbol is not ITypeSymbol type)
continue;
stringBuilder.AppendLine($"{INDENT}{INDENT}if (typeof(TFactory) == typeof({type.Name}))");

Loading…
Cancel
Save