Esempio n. 1
0
        public void Execute(GeneratorExecutionContext context)
        {
            if (context.SyntaxReceiver is not ConfigurationTemplateInterfaceSyntaxReceiver receiver)
            {
                return;
            }
            var compilation = context.Compilation;
            var types       = new ConfigurationTypes(compilation);

            if (!types.CheckContext(context))
            {
                return;
            }
            foreach (var ifaceSyntax in receiver.CandidateInterfaces)
            {
                var model       = compilation.GetSemanticModel(ifaceSyntax.SyntaxTree);
                var ifaceSymbol = model.GetDeclaredSymbol(ifaceSyntax, context.CancellationToken);

                if (ifaceSymbol == null)
                {
                    continue;
                }

                if (!ifaceSymbol.GetAttributes()
                    .Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, types.ConfigurationCollectionAttribute)))
                {
                    continue;
                }

                var diagnostics = Analyzers.AsParallel()
                                  .SelectMany(a => a.Analyze(context.Compilation, model, ifaceSyntax, context.CancellationToken))
                                  .ToList();

                foreach (var diag in diagnostics)
                {
                    context.ReportDiagnostic(diag);
                }
                if (diagnostics.Any())
                {
                    return;
                }

                var properties = new List <(INamedTypeSymbol, IPropertySymbol)>();

                // Add collection props bottom up.
                foreach (var childIface in ifaceSymbol.AllInterfaces.Reverse().Concat(new[] { ifaceSymbol }))
                {
                    foreach (var member in childIface.GetMembers())
                    {
                        if (member is IPropertySymbol property)
                        {
                            properties.Add((childIface, property !));
                        }
                    }
                }
                string classSource = GenerateSource(ifaceSymbol, properties, types);
                context.AddSource($"{ifaceSymbol.Name}_ConfigurationCollection.g.cs", SourceText.From(classSource, Encoding.UTF8));
            }
        }
        public void Execute(GeneratorExecutionContext context)
        {
            if (context.SyntaxReceiver is not ConfigurationTemplateInterfaceSyntaxReceiver receiver)
            {
                return;
            }
            var compilation = context.Compilation;
            var types       = new ConfigurationTypes(compilation);

            if (!types.CheckContext(context))
            {
                return;
            }

            foreach (var ifaceSyntax in receiver.CandidateInterfaces)
            {
                var model       = compilation.GetSemanticModel(ifaceSyntax.SyntaxTree);
                var ifaceSymbol = model.GetDeclaredSymbol(ifaceSyntax, context.CancellationToken);
                if (ifaceSymbol == null)
                {
                    continue;
                }
                if (!ifaceSymbol.GetAttributes()
                    .Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, types.InputConfigurationAttribute)))
                {
                    continue;
                }

                var diagnostics = Analyzers.AsParallel()
                                  .SelectMany(a => a.Analyze(context.Compilation, model, ifaceSyntax, context.CancellationToken))
                                  .ToList();

                foreach (var diag in diagnostics)
                {
                    context.ReportDiagnostic(diag);
                }
                if (diagnostics.Any())
                {
                    return;
                }

                var configProperties = new List <(INamedTypeSymbol, IPropertySymbol)>();
                var inputProperties  = new List <(INamedTypeSymbol, IPropertySymbol)>();

                foreach (var childIface in ifaceSymbol.AllInterfaces.Reverse().Concat(new[] { ifaceSymbol }))
                {
                    // No need to check if child interfaces are partial because we only add to the root interface.
                    foreach (var member in childIface.GetMembers())
                    {
                        if (member is IMethodSymbol accessor && accessor.AssociatedSymbol is IPropertySymbol)
                        {
                            continue;
                        }

                        bool isConfigOption = member.GetAttributes()
                                              .Where(attr => attr?.AttributeClass?
                                                     .Equals(types.ConfigurationOptionAttribute, SymbolEqualityComparer.Default) == true)
                                              .Any();
                        bool isInputOption = member.GetAttributes()
                                             .Where(attr => attr?.AttributeClass?
                                                    .Equals(types.InputOptionAttribute, SymbolEqualityComparer.Default) == true)
                                             .Any();

                        // case where neither/both handled by analyzer guards above.
                        if (isConfigOption && member is IPropertySymbol optionProperty)
                        {
                            configProperties.Add((childIface, optionProperty));
                        }
                        else if (isInputOption && member is IPropertySymbol inputProperty)
                        {
                            inputProperties.Add((childIface, inputProperty));
                        }
                    }
                }

                string classSource = GenerateSource(ifaceSymbol, configProperties, inputProperties, types);
                context.AddSource($"{ifaceSymbol.Name}_InputConfigurationSection.g.cs", SourceText.From(classSource, Encoding.UTF8));
            }
        }