< Summary

Information
Class: NexusLabs.Needlr.Injection.Reflection.IAssemblyProviderBuilderExtensions
Assembly: NexusLabs.Needlr.Injection.Reflection
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Injection.Reflection/IAssemblyProviderBuilderExtensions.cs
Line coverage
65%
Covered lines: 23
Uncovered lines: 12
Coverable lines: 35
Total lines: 153
Line coverage: 65.7%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MatchingAssemblies(...)100%22100%
MatchingFiles(...)100%11100%
MatchingAssemblies(...)0%620%
MatchingFiles(...)100%11100%
MatchingAssemblies(...)0%620%
MatchingFiles(...)100%11100%
IsAssemblyFilePath(...)100%22100%

File(s)

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

#LineLine coverage
 1using NexusLabs.Needlr.Injection.Reflection.Loaders;
 2
 3namespace NexusLabs.Needlr.Injection.Reflection;
 4
 5/// <summary>
 6/// Extension methods for <see cref="IAssemblyProviderBuilder"/> providing fluent configuration of assembly loading.
 7/// For assembly ordering, use <c>SyringeExtensions.OrderAssemblies</c> instead.
 8/// </summary>
 9public static class IAssemblyProviderBuilderExtensions
 10{
 11    /// <summary>
 12    /// Configures the builder to load assemblies from the application's base directory that match the specified filter 
 13    /// Only files with .dll or .exe extensions will be considered.
 14    /// </summary>
 15    /// <param name="builder">The assembly provider builder to configure.</param>
 16    /// <param name="fileFilter">A predicate that determines which assembly files to include based on their file path.</
 17    /// <returns>The configured assembly provider builder.</returns>
 18    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="fileFilter"/> 
 19    /// <example>
 20    /// <code>
 21    /// builder.MatchingAssemblies(path => path.Contains("MyProject"));
 22    /// </code>
 23    /// </example>
 24    public static IAssemblyProviderBuilder MatchingAssemblies(
 25        this IAssemblyProviderBuilder builder,
 26        Predicate<string> fileFilter)
 27    {
 2328        ArgumentNullException.ThrowIfNull(builder);
 2329        ArgumentNullException.ThrowIfNull(fileFilter);
 30
 2331        return builder.MatchingFiles(
 145332            x => IsAssemblyFilePath(x) && fileFilter(x));
 33    }
 34
 35    /// <summary>
 36    /// Configures the builder to load files from the application's base directory that match the specified filter crite
 37    /// </summary>
 38    /// <param name="builder">The assembly provider builder to configure.</param>
 39    /// <param name="fileFilter">A predicate that determines which files to include based on their file path.</param>
 40    /// <returns>The configured assembly provider builder.</returns>
 41    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="fileFilter"/> 
 42    public static IAssemblyProviderBuilder MatchingFiles(
 43        this IAssemblyProviderBuilder builder,
 44        Predicate<string> fileFilter)
 45    {
 2346        ArgumentNullException.ThrowIfNull(builder);
 2347        ArgumentNullException.ThrowIfNull(fileFilter);
 48
 2349        return builder.MatchingFiles(
 2350            AppDomain.CurrentDomain.BaseDirectory,
 2351            fileFilter);
 52    }
 53
 54    /// <summary>
 55    /// Configures the builder to load assemblies from the specified directory that match the filter criteria.
 56    /// Only files with .dll or .exe extensions will be considered.
 57    /// </summary>
 58    /// <param name="builder">The assembly provider builder to configure.</param>
 59    /// <param name="directory">The directory path to scan for assembly files.</param>
 60    /// <param name="fileFilter">A predicate that determines which assembly files to include based on their file path.</
 61    /// <returns>The configured assembly provider builder.</returns>
 62    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/>, <paramref name="directory"/>, or
 63    public static IAssemblyProviderBuilder MatchingAssemblies(
 64        this IAssemblyProviderBuilder builder,
 65        string directory,
 66        Predicate<string> fileFilter)
 67    {
 068        ArgumentNullException.ThrowIfNull(builder);
 069        ArgumentNullException.ThrowIfNull(directory);
 070        ArgumentNullException.ThrowIfNull(fileFilter);
 71
 072        return builder.MatchingFiles(
 073            directory,
 074            x => IsAssemblyFilePath(x) && fileFilter(x));
 75    }
 76
 77    /// <summary>
 78    /// Configures the builder to load files from the specified directory that match the filter criteria.
 79    /// </summary>
 80    /// <param name="builder">The assembly provider builder to configure.</param>
 81    /// <param name="directory">The directory path to scan for files.</param>
 82    /// <param name="fileFilter">A predicate that determines which files to include based on their file path.</param>
 83    /// <returns>The configured assembly provider builder.</returns>
 84    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/>, <paramref name="directory"/>, or
 85    public static IAssemblyProviderBuilder MatchingFiles(
 86        this IAssemblyProviderBuilder builder,
 87        string directory,
 88        Predicate<string> fileFilter)
 89    {
 2390        ArgumentNullException.ThrowIfNull(builder);
 2391        ArgumentNullException.ThrowIfNull(directory);
 2392        ArgumentNullException.ThrowIfNull(fileFilter);
 93
 2394        return builder.MatchingFiles(
 2395            [directory],
 2396            fileFilter);
 97    }
 98
 99    /// <summary>
 100    /// Configures the builder to load assemblies from the specified directories that match the filter criteria.
 101    /// Only files with .dll or .exe extensions will be considered.
 102    /// </summary>
 103    /// <param name="builder">The assembly provider builder to configure.</param>
 104    /// <param name="directories">The collection of directory paths to scan for assembly files.</param>
 105    /// <param name="fileFilter">A predicate that determines which assembly files to include based on their file path.</
 106    /// <returns>The configured assembly provider builder.</returns>
 107    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/>, <paramref name="directories"/>, 
 108    public static IAssemblyProviderBuilder MatchingAssemblies(
 109        this IAssemblyProviderBuilder builder,
 110        IReadOnlyList<string> directories,
 111        Predicate<string> fileFilter)
 112    {
 0113        ArgumentNullException.ThrowIfNull(builder);
 0114        ArgumentNullException.ThrowIfNull(directories);
 0115        ArgumentNullException.ThrowIfNull(fileFilter);
 116
 0117        return builder.MatchingFiles(
 0118            directories,
 0119            x => IsAssemblyFilePath(x) && fileFilter(x));
 120    }
 121
 122    /// <summary>
 123    /// Configures the builder to load files from the specified directories that match the filter criteria.
 124    /// This is the core method that sets up the FileMatchAssemblyLoader.
 125    /// </summary>
 126    /// <param name="builder">The assembly provider builder to configure.</param>
 127    /// <param name="directories">The collection of directory paths to scan for files.</param>
 128    /// <param name="fileFilter">A predicate that determines which files to include based on their file path.</param>
 129    /// <returns>The configured assembly provider builder.</returns>
 130    /// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/>, <paramref name="directories"/>, 
 131    public static IAssemblyProviderBuilder MatchingFiles(
 132        this IAssemblyProviderBuilder builder,
 133        IReadOnlyList<string> directories,
 134        Predicate<string> fileFilter)
 135    {
 23136        ArgumentNullException.ThrowIfNull(builder);
 23137        ArgumentNullException.ThrowIfNull(directories);
 23138        ArgumentNullException.ThrowIfNull(fileFilter);
 139
 23140        return builder.UseLoader(new FileMatchAssemblyLoader(
 23141            directories,
 23142            fileFilter));
 143    }
 144
 145    /// <summary>
 146    /// Determines whether the specified file path represents an assembly file (.dll or .exe).
 147    /// </summary>
 148    /// <param name="filePath">The file path to check.</param>
 149    /// <returns>True if the file path ends with .dll or .exe (case-insensitive); otherwise, false.</returns>
 150    private static bool IsAssemblyFilePath(string filePath) =>
 1430151        filePath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ||
 1430152        filePath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase);
 153}