| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.Generators.CodeGen; |
| | | 5 | | using NexusLabs.Needlr.Generators.Models; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.Generators; |
| | | 8 | | |
| | | 9 | | internal static class FactoryConsumerDiscoveryHelper |
| | | 10 | | { |
| | | 11 | | internal static Compilation BindFactoryConsumers( |
| | | 12 | | Compilation compilation, |
| | | 13 | | IReadOnlyList<DiscoveredFactory> factories, |
| | | 14 | | string assemblyName, |
| | | 15 | | BreadcrumbWriter breadcrumbs, |
| | | 16 | | string? projectDirectory) |
| | | 17 | | { |
| | 653 | 18 | | var interfaceFactories = factories.Where(factory => factory.GenerateInterface).ToArray(); |
| | 608 | 19 | | if (interfaceFactories.Length == 0) |
| | 565 | 20 | | return compilation; |
| | | 21 | | |
| | | 22 | | // AddSource does not update the input compilation. Reuse the real emitter in |
| | | 23 | | // a private compilation so Roslyn binds factory names, aliases and namespaces |
| | | 24 | | // without relaxing discovery's rejection of unrelated error symbols. |
| | 43 | 25 | | var source = FactoryCodeGenerator.GenerateFactoryInterfacesSource( |
| | 43 | 26 | | interfaceFactories, |
| | 43 | 27 | | assemblyName, |
| | 43 | 28 | | breadcrumbs, |
| | 43 | 29 | | projectDirectory); |
| | 43 | 30 | | var parseOptions = compilation.SyntaxTrees.FirstOrDefault()?.Options as CSharpParseOptions; |
| | 43 | 31 | | var factoryTree = CSharpSyntaxTree.ParseText( |
| | 43 | 32 | | GeneratedSourceText.Create(source), |
| | 43 | 33 | | parseOptions); |
| | | 34 | | |
| | 43 | 35 | | return compilation.AddSyntaxTrees(factoryTree); |
| | | 36 | | } |
| | | 37 | | } |