public void Execute(GeneratorExecutionContext context)
        {
            try
            {
                var registrationSymbols = RegistrationSymbols.FromCompilation(context.Compilation);
                var containerClasses    = context.LocateContainerSymbols(registrationSymbols.ContainerSymbol);
                var generator           = new ContainerClassContentGenerator(context, registrationSymbols);

                foreach (var containerClass in containerClasses)
                {
                    var hintName = $"Generated.{containerClass.FullyQualifiedName}";
                    var content  = generator.GenerateClassString(containerClass);

                    WriteOutDebugFile(hintName, content, context);
                    context.AddSource(hintName, SourceText.From(content, Encoding.UTF8));
                }
            }
            catch (DiagnosticException ex)
            {
                context.ReportDiagnostic(ex.Diagnostic);
            }
            catch (Exception ex)
            {
                var descriptor = new DiagnosticDescriptor(DiagnosticConstants.UnknownExceptionId, "Unexpected error", $"Unknown error during generation: {ex.GetType()} {ex.Message}", DiagnosticConstants.Category, DiagnosticSeverity.Error, true);
                context.ReportDiagnostic(Diagnostic.Create(descriptor, Location.None));
            }
        }
        private static bool TryCreateFactoryRegistration(this RegistrationSymbols registrationSymbols, INamedTypeSymbol typeSymbol, out Registration registration)
        {
            if (SymbolEqualityComparer.Default.Equals(registrationSymbols.FactorySymbol, typeSymbol.OriginalDefinition))
            {
                registration = CreateFactoryRegistration((INamedTypeSymbol)typeSymbol.TypeArguments.First());
                return(true);
            }

            registration = default;
            return(false);
        }
        private static bool TryCreateSingletonRegistration(this RegistrationSymbols registrationSymbols, INamedTypeSymbol typeSymbol, out Registration registration)
        {
            if (registrationSymbols.SingletonSymbols.Any(s => SymbolEqualityComparer.Default.Equals(s, typeSymbol.OriginalDefinition)))
            {
                registration = CreateSingletonRegistration((INamedTypeSymbol)typeSymbol.TypeArguments.First(), (INamedTypeSymbol)typeSymbol.TypeArguments.Last());
                return(true);
            }

            registration = default;
            return(false);
        }
        private static bool TryCreateDelegateRegistration(this RegistrationSymbols registrationSymbols, INamedTypeSymbol typeSymbol, out Registration registration)
        {
            var delegateMatch = registrationSymbols.DelegateSymbols.FirstOrDefault(s => SymbolEqualityComparer.Default.Equals(s, typeSymbol.OriginalDefinition));

            if (delegateMatch != null)
            {
                registration = CreateDelegateRegistration(delegateMatch, (INamedTypeSymbol)typeSymbol.TypeArguments.First(), typeSymbol.TypeArguments.Skip(1).OfType <INamedTypeSymbol>().ToArray());
                return(true);
            }

            registration = default;
            return(false);
        }
예제 #5
0
 public ContainerClassContentGenerator(SourceGeneratorContext context, RegistrationSymbols registrationSymbols)
 {
     _context             = context;
     _registrationSymbols = registrationSymbols;
 }
 public static Registration[] GetRegistrationsFromInterfaces(RegistrationSymbols registrationSymbols, INamedTypeSymbol[] interfaces)
 => interfaces.Select(i =>
        public static INamedTypeSymbol[] GetTypesRequiringConstructorsFromContainerAttributes(RegistrationSymbols registrationSymbols, AttributeData[] attributes)
        {
            var constructorForAttributes = attributes.Where(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, registrationSymbols.ConstructorForAttributeSymbol)).ToArray();

            return(constructorForAttributes.Select(attr => attr.GetAttributeConstructorValueByParameterName <INamedTypeSymbol>("constructType")).ToArray());
        }
예제 #8
0
 public ContainerClassContentGenerator(GeneratorExecutionContext context, RegistrationSymbols registrationSymbols)
 {
     _context             = context;
     _registrationSymbols = registrationSymbols;
 }