|
|
|
|
@ -101,8 +101,13 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
|
|
|
|
|
private void GenerateFactory(SourceProductionContext context, ImmutableArray<ITypeSymbol?> types) |
|
|
|
|
{ |
|
|
|
|
foreach (ITypeSymbol typeSymbol in GetDistinctTypes(types)) |
|
|
|
|
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) |
|
|
|
|
@ -130,8 +135,11 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
stringBuilder.AppendLine($"{INDENT}public TFactory Create<TFactory>(IocContainer container)"); |
|
|
|
|
stringBuilder.AppendLine($"{INDENT}{{"); |
|
|
|
|
|
|
|
|
|
foreach (ITypeSymbol type in GetDistinctTypes(types)) |
|
|
|
|
foreach (ISymbol? symbol in types.Distinct(SymbolEqualityComparer.IncludeNullability)) |
|
|
|
|
{ |
|
|
|
|
if (symbol is not ITypeSymbol type) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}if (typeof(TFactory) == typeof({GetTypeText(type, false)}))"); |
|
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}{{"); |
|
|
|
|
stringBuilder.AppendLine($"{INDENT}{INDENT}{INDENT}return (TFactory) (object) new Generated{GetTypeText(type, false)}(container);"); |
|
|
|
|
@ -255,27 +263,7 @@ public class FactoryGenerator : IIncrementalGenerator |
|
|
|
|
return stringBuilder.ToString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private IEnumerable<ITypeSymbol> GetDistinctTypes(ImmutableArray<ITypeSymbol?> types) |
|
|
|
|
{ |
|
|
|
|
List<ITypeSymbol> distinctTypes = []; |
|
|
|
|
foreach (ITypeSymbol? typeSymbol in types) |
|
|
|
|
{ |
|
|
|
|
if (typeSymbol is null) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
if (distinctTypes.Contains(typeSymbol, SymbolEqualityComparer.IncludeNullability)) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
if (distinctTypes.Any(t => t.Name == typeSymbol.Name)) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
distinctTypes.Add(typeSymbol); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return distinctTypes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private string? GetNamespaceOfType(ITypeSymbol typeSymbol) => typeSymbol.ContainingNamespace.IsGlobalNamespace ? null : typeSymbol.ContainingNamespace.ToString(); |
|
|
|
|
private string? GetNamespaceOfType(ITypeSymbol s) => s.ContainingNamespace.IsGlobalNamespace ? null : s.ContainingNamespace.ToString(); |
|
|
|
|
private List<string?> GetNamespacesOfType(ITypeSymbol typeSymbol) |
|
|
|
|
{ |
|
|
|
|
List<string?> namespaces = []; |
|
|
|
|
|