Пример #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));
            }
        }
Пример #2
0
        public MacroHelper(IAssemblySymbol macrosAssembly, List <Diagnostic> diagnostic,
                           IAssemblySymbol[] referencedAssemblies, ImmutableArray <SyntaxTree> trees, CSharpCompilation compilation)
        {
            this.macrosAssembly = macrosAssembly;
            this.diagnostic     = diagnostic;

            var typesToCheck = new List <INamedTypeSymbol>();

            {
                var typesWithMacroAttributesType = getTypeSymbol <TypesWithMacroAttributes>();

                foreach (var assembly in referencedAssemblies)
                {
                    collectTypesForMacros(assembly);
                }

                void collectTypesForMacros(IAssemblySymbol assembly)
                {
                    foreach (var attr in assembly.GetAttributes())
                    {
                        var c = attr.AttributeClass;
                        if (c != null && SymbolEqualityComparer.Default.Equals(c, typesWithMacroAttributesType))
                        {
                            var s = attr.ConstructorArguments[0].Values.Select(_ => {
                                var symbol = (INamedTypeSymbol?)_.Value;
                                if (symbol == null)
                                {
                                    throw new Exception();
                                }
                                return(symbol);
                            });
                            typesToCheck.AddRange(s);
                        }
                    }
                }
            }

            allMethods = typesToCheck
                         .SelectMany(_ => _.ConstructedFrom.GetMembers().OfType <IMethodSymbol>())
                         .Select(_ => _.OriginalDefinition)
                         .ToArray();

            {
                var bag = new ConcurrentBag <RootOperationsFinder>();
                trees.AsParallel().ForAll(tree => {
                    var root     = tree.GetCompilationUnitRoot();
                    var model    = compilation.GetSemanticModel(tree);
                    var opFinder = new RootOperationsFinder(model, tree);
                    opFinder.Visit(root);
                    bag.Add(opFinder);
                });
                operations = bag.ToImmutableArray();
            }
        }
        private bool ProcessSecCandles(IEnumerable <ICandle> secCandles)
        {
            foreach (var candle in secCandles)
            {
                if (_cts.IsCancellationRequested)
                {
                    return(true);
                }

                if (_healthService.CandlesToDispatchQueueLength > _settings.CandlesToDispatchLengthThrottlingThreshold)
                {
                    try
                    {
                        Task.Delay(_settings.ThrottlingDelay).GetAwaiter().GetResult();
                    }
                    catch (TaskCanceledException)
                    {
                        return(true);
                    }
                }

                _telemetryService.UpdateCurrentHistoryDate(candle.Timestamp, candle.PriceType);

                CheckCandleOrder(candle);

                _candlesPersistenceQueue.EnqueueCandle(candle);

                _intervalsToGenerate
                .AsParallel()
                .ForAll(interval =>
                {
                    var mergingResult = _candlesGenerator.Merge(
                        assetPair: candle.AssetPairId,
                        priceType: candle.PriceType,
                        timeInterval: interval,
                        timestamp: candle.Timestamp,
                        open: candle.Open,
                        close: candle.Close,
                        low: candle.Low,
                        high: candle.High);

                    if (mergingResult.WasChanged)
                    {
                        _candlesPersistenceQueue.EnqueueCandle(mergingResult.Candle);
                    }
                });
            }

            return(false);
        }
        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));
            }
        }