using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis; using System.Linq; using System.Collections.Generic; using Microsoft.CodeAnalysis.Text; using System.Text; namespace linker.gen { [Generator(LanguageNames.CSharp)] public class InterfaceSourceGenerator : IIncrementalGenerator { private List generators = new List { new GeneratorInfo{ ClassName="FlowTypesLoader", ClassNameSpace="linker.plugins.flow", InterfaceName="linker.plugins.flow.IFlow" }, new GeneratorInfo{ ClassName="RelayValidatorTypeLoader", ClassNameSpace="linker.plugins.relay.validator", InterfaceName="linker.plugins.relay.validator.IRelayValidator" }, new GeneratorInfo{ ClassName="SignInArgsTypesLoader", ClassNameSpace="linker.plugins.signIn.args", InterfaceName="linker.plugins.signIn.args.ISignInArgs" }, new GeneratorInfo{ ClassName="RelayTypesLoader", ClassNameSpace="linker.plugins.relay", InterfaceName="linker.plugins.relay.transport.ITransport" }, new GeneratorInfo{ ClassName="ResolverTypesLoader", ClassNameSpace="linker.plugins.resolver", InterfaceName="linker.plugins.resolver.IResolver" }, new GeneratorInfo{ ClassName="TunnelExcludeIPTypesLoader", ClassNameSpace="linker.plugins.tunnel.excludeip", InterfaceName="linker.plugins.tunnel.excludeip.ITunnelExcludeIP" }, new GeneratorInfo{ ClassName="StartupTransfer", ClassNameSpace="linker.startup", InterfaceName="linker.startup.IStartup", Instance=true }, new GeneratorInfo{ ClassName="MessengerResolverTypesLoader", ClassNameSpace="linker.plugins.messenger", InterfaceName="linker.plugins.messenger.IMessenger"}, new GeneratorInfo{ ClassName="ApiClientTypesLoader", ClassNameSpace="linker.plugins.capi", InterfaceName="linker.plugins.capi.IApiClientController"}, new GeneratorInfo{ ClassName="ConfigSyncTypesLoader", ClassNameSpace="linker.plugins.config", InterfaceName="linker.plugins.config.IConfigSync"}, }; public void Initialize(IncrementalGeneratorInitializationContext context) { IncrementalValueProvider compilations = context.CompilationProvider.Select((compilation, cancellationToken) => compilation); context.RegisterSourceOutput(compilations, (sourceProductionContext, compilation) => { foreach (GeneratorInfo info in generators) { var iFlowSymbol = compilation.GetTypeByMetadataName(info.InterfaceName); List types = new List { }; List classs = new List { }; List namespaces = new List { }; foreach (var syntaxTree in compilation.SyntaxTrees) { if (syntaxTree == null) { continue; } var root = syntaxTree.GetRoot(sourceProductionContext.CancellationToken); var classDeclarationSyntaxs = root .DescendantNodes(descendIntoTrivia: false) .OfType(); foreach (var classDeclarationSyntax in classDeclarationSyntaxs) { var model = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree); var classSymbol = model.GetDeclaredSymbol(classDeclarationSyntax) as INamedTypeSymbol; if (classSymbol.AllInterfaces.Contains(iFlowSymbol)) { types.Add($"typeof({classDeclarationSyntax.Identifier.Text})"); if (info.Instance) classs.Add($"new {classDeclarationSyntax.Identifier.Text}()"); var namespaceDecl = classDeclarationSyntax.FirstAncestorOrSelf(); if (namespaceDecl != null) { namespaces.Add($"using {namespaceDecl.Name.ToString()};"); } } } } var source = $@" using System; using System.Collections.Generic; {string.Join("\r\n", namespaces)} namespace {info.ClassNameSpace} {{ public partial class {info.ClassName} {{ public static List GetSourceGeneratorTypes() {{ return new List {{ {string.Join(",", types)} }}; }} public static List<{info.InterfaceName}> GetSourceGeneratorInstances() {{ return new List<{info.InterfaceName}> {{ {string.Join(",", classs)} }}; }} }} }}"; var sourceText = SourceText.From(source, Encoding.UTF8); sourceProductionContext.AddSource($"{info.ClassName}Instances.g.cs", sourceText); } }); } public sealed class GeneratorInfo { public string ClassName { get; set; } public string ClassNameSpace { get; set; } public string InterfaceName { get; set; } public bool Instance { get; set; } } } }