< 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>
 26135    public GeneratedAssemblyProvider(
 26136        Func<IReadOnlyList<InjectableTypeInfo>> injectableTypesProvider,
 26137        Func<IReadOnlyList<PluginTypeInfo>> pluginTypesProvider)
 38    {
 26139        ArgumentNullException.ThrowIfNull(injectableTypesProvider);
 26140        ArgumentNullException.ThrowIfNull(pluginTypesProvider);
 41
 26142        _lazyAssemblies = new(() =>
 26143        {
 26144            // In NativeAOT with reflection disabled, Type.Assembly can throw.
 26145            // Candidate assemblies are only a hint for reflection-based discovery.
 26146            // For source generation, the plugin/type registries already define the universe.
 26147            try
 26148            {
 25549                var assemblies = new HashSet<Assembly>();
 26150
 26151                // Extract assemblies from injectable types
 7382852                foreach (var info in injectableTypesProvider())
 26153                {
 3665954                    assemblies.Add(info.Type.Assembly);
 26155                }
 26156
 26157                // Extract assemblies from plugin types
 4430258                foreach (var info in pluginTypesProvider())
 26159                {
 2189660                    assemblies.Add(info.PluginType.Assembly);
 26161                }
 26162
 25563                return assemblies.ToList();
 26164            }
 065            catch (NotSupportedException)
 26166            {
 067                return Array.Empty<Assembly>();
 26168            }
 51669        });
 26170    }
 71
 72    /// <inheritdoc />
 73    public IReadOnlyList<Assembly> GetCandidateAssemblies() =>
 49474        _lazyAssemblies.Value;
 75}