public async Task <CompilationUnitSyntax> GenerateCode(CancellationToken cancellationToken) { var namespaceName = "HagarGeneratedCode." + this.compilation.AssemblyName; // Collect metadata from the compilation. var metadataModel = await this.GenerateMetadataModel(cancellationToken); var members = new List <MemberDeclarationSyntax>(); foreach (var type in metadataModel.InvokableInterfaces) { foreach (var method in type.Methods) { var(invokable, generatedInvokerDescription) = InvokableGenerator.Generate(this.compilation, this.libraryTypes, type, method); metadataModel.SerializableTypes.Add(generatedInvokerDescription); metadataModel.GeneratedInvokables[method] = generatedInvokerDescription; members.Add(invokable); } var(proxy, generatedProxyDescription) = ProxyGenerator.Generate(this.compilation, this.libraryTypes, type, metadataModel); metadataModel.GeneratedProxies.Add(generatedProxyDescription); members.Add(proxy); } // Generate code. foreach (var type in metadataModel.SerializableTypes) { // Generate a partial serializer class for each serializable type. members.Add(SerializerGenerator.GenerateSerializer(this.compilation, this.libraryTypes, type)); if (type.IsEmptyConstructable) { metadataModel.ActivatableTypes.Add(type); // Generate a partial serializer class for each serializable type. members.Add(ActivatorGenerator.GenerateActivator(this.compilation, this.libraryTypes, type)); } } // Generate metadata. var metadataClass = MetadataGenerator.GenerateMetadata(this.compilation, metadataModel, this.libraryTypes); members.Add(metadataClass); var metadataAttribute = AttributeList() .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))) .WithAttributes( SingletonSeparatedList( Attribute(this.libraryTypes.MetadataProviderAttribute.ToNameSyntax()) .AddArgumentListArguments(AttributeArgument(TypeOfExpression(ParseTypeName($"{namespaceName}.{metadataClass.Identifier.Text}")))))); return(CompilationUnit() .WithAttributeLists(List(new [] { metadataAttribute })) .WithMembers( SingletonList <MemberDeclarationSyntax>( NamespaceDeclaration(ParseName(namespaceName)) .WithMembers(List(members)) .WithUsings(List(new[] { UsingDirective(ParseName("global::Hagar.Codecs")), UsingDirective(ParseName("global::Hagar.GeneratedCodeHelpers")) }))))); }
public static TypeSyntax GetActivatorTypeName(this ISerializableTypeDescription type) { var genericArity = type.TypeParameters.Length; var name = ActivatorGenerator.GetSimpleClassName(type); if (genericArity > 0) { name += $"<{new string(',', genericArity - 1)}>"; } return(ParseTypeName(name)); }