< Summary

Information
Class: NexusLabs.Needlr.Injection.SourceGen.Loaders.GeneratedAssemblyProvider
Assembly: NexusLabs.Needlr.Injection.SourceGen
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.SourceGen/Loaders/GeneratedAssemblyProvider.cs
Line coverage
94%
Covered lines: 33
Uncovered lines: 2
Coverable lines: 35
Total lines: 75
Line coverage: 94.2%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%4494.11%
GetCandidateAssemblies()100%11100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.SourceGen/Loaders/GeneratedAssemblyProvider.cs

#LineLine coverage
 1using System.Reflection;
 2
 3using NexusLabs.Needlr.Generators;
 4
 5namespace NexusLabs.Needlr.Injection.SourceGen.Loaders;
 6
 7/// <summary>
 8/// An assembly provider that derives assemblies from the generated TypeRegistry.
 9/// </summary>
 10/// <remarks>
 11/// <para>
 12/// When using source generation, the TypeRegistry contains all injectable types
 13/// and plugins discovered at compile time. This provider extracts the unique
 14/// assemblies from those types, enabling cross-assembly plugin discovery without
 15/// runtime assembly scanning.
 16/// </para>
 17/// <para>
 18/// This provider should be used with <see cref="PluginFactories.GeneratedPluginFactory"/>
 19/// to ensure that all assemblies containing generated types are included in plugin discovery.
 20/// </para>
 21/// <para>
 22/// For assembly ordering, use <c>SyringeExtensions.OrderAssemblies</c> after configuring the Syringe.
 23/// </para>
 24/// </remarks>
 25[DoNotAutoRegister]
 26public sealed class GeneratedAssemblyProvider : IAssemblyProvider
 27{
 28    private readonly Lazy<IReadOnlyList<Assembly>> _lazyAssemblies;
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="GeneratedAssemblyProvider"/> class.
 32    /// </summary>
 33    /// <param name="injectableTypesProvider">A function that returns the injectable types.</param>
 34    /// <param name="pluginTypesProvider">A function that returns the plugin types.</param>
 30635    public GeneratedAssemblyProvider(
 30636        Func<IReadOnlyList<InjectableTypeInfo>> injectableTypesProvider,
 30637        Func<IReadOnlyList<PluginTypeInfo>> pluginTypesProvider)
 38    {
 30639        ArgumentNullException.ThrowIfNull(injectableTypesProvider);
 30640        ArgumentNullException.ThrowIfNull(pluginTypesProvider);
 41
 30642        _lazyAssemblies = new(() =>
 30643        {
 30644            // In NativeAOT with reflection disabled, Type.Assembly can throw.
 30645            // Candidate assemblies are only a hint for reflection-based discovery.
 30646            // For source generation, the plugin/type registries already define the universe.
 30647            try
 30648            {
 30049                var assemblies = new HashSet<Assembly>();
 30650
 30651                // Extract assemblies from injectable types
 9152852                foreach (var info in injectableTypesProvider())
 30653                {
 4546454                    assemblies.Add(info.Type.Assembly);
 30655                }
 30656
 30657                // Extract assemblies from plugin types
 6910858                foreach (var info in pluginTypesProvider())
 30659                {
 3425460                    assemblies.Add(info.PluginType.Assembly);
 30661                }
 30662
 30063                return assemblies.ToList();
 30664            }
 065            catch (NotSupportedException)
 30666            {
 067                return Array.Empty<Assembly>();
 30668            }
 60669        });
 30670    }
 71
 72    /// <inheritdoc />
 73    public IReadOnlyList<Assembly> GetCandidateAssemblies() =>
 58474        _lazyAssemblies.Value;
 75}