< Summary

Information
Class: NexusLabs.Needlr.Injection.Reflection.Loaders.FileMatchAssemblyLoader
Assembly: NexusLabs.Needlr.Injection.Reflection
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.Reflection/Loaders/FileMatchAssemblyLoader.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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%11100%
LoadAssemblies(...)100%22100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.Reflection/Loaders/FileMatchAssemblyLoader.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Reflection;
 3
 4namespace NexusLabs.Needlr.Injection.Reflection.Loaders;
 5
 6/// <summary>
 7/// Assembly loader that loads assemblies from disk based on file name matching.
 8/// </summary>
 9/// <remarks>
 10/// This loader is not compatible with NativeAOT or trimming. For AOT scenarios,
 11/// use GeneratedAssemblyProvider from NexusLabs.Needlr.Injection.SourceGen instead.
 12/// </remarks>
 13[RequiresUnreferencedCode("FileMatchAssemblyLoader uses Assembly.LoadFrom which is not AOT-compatible. Use GeneratedAsse
 14public sealed class FileMatchAssemblyLoader : IAssemblyLoader
 15{
 16    private readonly IReadOnlyList<string> _directories;
 17    private readonly Predicate<string> _fileFilter;
 18
 39619    public FileMatchAssemblyLoader(
 39620        IReadOnlyList<string> directories,
 39621        Predicate<string>? fileFilter)
 22    {
 39623        ArgumentNullException.ThrowIfNull(directories);
 39524        ArgumentNullException.ThrowIfNull(fileFilter);
 25
 39426        _directories = directories;
 39427        _fileFilter = fileFilter;
 39428    }
 29
 30    /// <inheritdoc />
 31    public IReadOnlyList<Assembly> LoadAssemblies(
 32        bool continueOnAssemblyError)
 33    {
 31134        var sourceAssemblies = _directories
 31135            .SelectMany(Directory.GetFiles)
 31136            .Where(x =>
 31137            {
 1912438                var fileName = Path.GetFileName(x);
 1912439                return _fileFilter.Invoke(fileName);
 31140            })
 31141            .Select(p =>
 31142            {
 31143                try
 31144                {
 63645                    return Assembly.LoadFrom(p);
 31146                }
 347                catch (Exception ex)
 31148                {
 349                    if (continueOnAssemblyError)
 31150                    {
 251                        return null;
 31152                    }
 31153                    else
 31154                    {
 155                        throw new InvalidOperationException($"Failed to load assembly from path: {p}", ex);
 31156                    }
 31157                }
 63558            })
 63559            .Where(x => x != null)
 31160            .Cast<Assembly>()
 31161            .ToArray();
 30962        return sourceAssemblies;
 63    }
 64}