| | | 1 | | using NexusLabs.Needlr.Injection.Reflection.Loaders; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 9 | | public 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 | | { |
| | 23 | 28 | | ArgumentNullException.ThrowIfNull(builder); |
| | 23 | 29 | | ArgumentNullException.ThrowIfNull(fileFilter); |
| | | 30 | | |
| | 23 | 31 | | return builder.MatchingFiles( |
| | 1453 | 32 | | 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 | | { |
| | 23 | 46 | | ArgumentNullException.ThrowIfNull(builder); |
| | 23 | 47 | | ArgumentNullException.ThrowIfNull(fileFilter); |
| | | 48 | | |
| | 23 | 49 | | return builder.MatchingFiles( |
| | 23 | 50 | | AppDomain.CurrentDomain.BaseDirectory, |
| | 23 | 51 | | 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 | | { |
| | 0 | 68 | | ArgumentNullException.ThrowIfNull(builder); |
| | 0 | 69 | | ArgumentNullException.ThrowIfNull(directory); |
| | 0 | 70 | | ArgumentNullException.ThrowIfNull(fileFilter); |
| | | 71 | | |
| | 0 | 72 | | return builder.MatchingFiles( |
| | 0 | 73 | | directory, |
| | 0 | 74 | | 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 | | { |
| | 23 | 90 | | ArgumentNullException.ThrowIfNull(builder); |
| | 23 | 91 | | ArgumentNullException.ThrowIfNull(directory); |
| | 23 | 92 | | ArgumentNullException.ThrowIfNull(fileFilter); |
| | | 93 | | |
| | 23 | 94 | | return builder.MatchingFiles( |
| | 23 | 95 | | [directory], |
| | 23 | 96 | | 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 | | { |
| | 0 | 113 | | ArgumentNullException.ThrowIfNull(builder); |
| | 0 | 114 | | ArgumentNullException.ThrowIfNull(directories); |
| | 0 | 115 | | ArgumentNullException.ThrowIfNull(fileFilter); |
| | | 116 | | |
| | 0 | 117 | | return builder.MatchingFiles( |
| | 0 | 118 | | directories, |
| | 0 | 119 | | 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 | | { |
| | 23 | 136 | | ArgumentNullException.ThrowIfNull(builder); |
| | 23 | 137 | | ArgumentNullException.ThrowIfNull(directories); |
| | 23 | 138 | | ArgumentNullException.ThrowIfNull(fileFilter); |
| | | 139 | | |
| | 23 | 140 | | return builder.UseLoader(new FileMatchAssemblyLoader( |
| | 23 | 141 | | directories, |
| | 23 | 142 | | 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) => |
| | 1430 | 151 | | filePath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || |
| | 1430 | 152 | | filePath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase); |
| | | 153 | | } |