Пример #1
0
        public static ClassDeclarationSyntax GenerateMetadata(Compilation compilation, List <TypeDescription> serializableTypes)
        {
            var configParam = "config".ToIdentifierName();
            var addMethod   = configParam.Member("PartialSerializers").Member("Add");
            var body        = new List <StatementSyntax>();

            body.AddRange(
                serializableTypes.Select(
                    type =>
                    (StatementSyntax)ExpressionStatement(InvocationExpression(addMethod, ArgumentList(SingletonSeparatedList(Argument(TypeOfExpression(GetPartialSerializerTypeName(type.Type)))))))
                    ));

            var libraryTypes    = LibraryTypes.FromCompilation(compilation);
            var configType      = libraryTypes.SerializerConfiguration;
            var configureMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), "Configure")
                                  .AddModifiers(Token(SyntaxKind.PublicKeyword))
                                  .AddParameterListParameters(
                Parameter(configParam.Identifier).WithType(configType.ToTypeSyntax()))
                                  .AddBodyStatements(body.ToArray());

            var interfaceType = libraryTypes.ConfigurationProvider.Construct(configType);

            return(ClassDeclaration(CodeGenerator.CodeGeneratorName + "_Metadata_" + compilation.AssemblyName.Replace('.', '_'))
                   .AddBaseListTypes(SimpleBaseType(interfaceType.ToTypeSyntax()))
                   .AddModifiers(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.SealedKeyword))
                   .AddAttributeLists(AttributeList(SingletonSeparatedList(CodeGenerator.GetGeneratedCodeAttributeSyntax())))
                   .AddMembers(configureMethod));

            TypeSyntax GetPartialSerializerTypeName(INamedTypeSymbol type)
            {
                var genericArity = type.TypeParameters.Length;
                var name         = PartialSerializerGenerator.GetSimpleClassName(type);

                if (genericArity > 0)
                {
                    name += $"<{new string(',', genericArity - 1)}>";
                }

                return(ParseTypeName(name));
            }
        }
Пример #2
0
        public async Task <CompilationUnitSyntax> GenerateCode(CancellationToken cancellationToken)
        {
            // Collect metadata from the compilation.
            var serializableTypes = await GetSerializableTypes(cancellationToken);

            // Generate code.
            var members = new List <MemberDeclarationSyntax>();

            foreach (var type in serializableTypes)
            {
                // Generate a partial serializer class for each serializable type.
                members.Add(PartialSerializerGenerator.GenerateSerializer(this.compilation, type));
            }

            var namespaceName = "HagarGeneratedCode." + this.compilation.AssemblyName;

            // Generate metadata.
            var metadataClass = MetadataGenerator.GenerateMetadata(this.compilation, serializableTypes);

            members.Add(metadataClass);

            var metadataAttribute = AttributeList()
                                    .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))
                                    .WithAttributes(
                SingletonSeparatedList(
                    Attribute(this.compilation.GetTypeByMetadataName("Hagar.Configuration.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")) })))));
        }