| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.Injection.AssemblyOrdering; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Fluent builder for specifying assembly ordering rules. |
| | | 8 | | /// Assemblies are sorted into tiers based on the first matching rule. |
| | | 9 | | /// Unmatched assemblies are placed last. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class AssemblyOrderBuilder |
| | | 12 | | { |
| | 47 | 13 | | private readonly List<AssemblyOrderRule> _rules = new(); |
| | | 14 | | private int _currentTier = 0; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Adds the first ordering rule. Assemblies matching this expression go first. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="predicate">Expression that determines if an assembly matches this tier.</param> |
| | | 20 | | /// <returns>The builder for chaining.</returns> |
| | | 21 | | public AssemblyOrderBuilder By(Expression<Func<AssemblyInfo, bool>> predicate) |
| | | 22 | | { |
| | 44 | 23 | | ArgumentNullException.ThrowIfNull(predicate); |
| | 43 | 24 | | _rules.Add(new AssemblyOrderRule(predicate, _currentTier++)); |
| | 43 | 25 | | return this; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Adds a subsequent ordering rule. Assemblies matching this expression |
| | | 30 | | /// (and not matching any previous rules) go in the next tier. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="predicate">Expression that determines if an assembly matches this tier.</param> |
| | | 33 | | /// <returns>The builder for chaining.</returns> |
| | | 34 | | public AssemblyOrderBuilder ThenBy(Expression<Func<AssemblyInfo, bool>> predicate) |
| | | 35 | | { |
| | 29 | 36 | | if (_rules.Count == 0) |
| | | 37 | | { |
| | 1 | 38 | | throw new InvalidOperationException("ThenBy() must be called after By()."); |
| | | 39 | | } |
| | | 40 | | |
| | 28 | 41 | | ArgumentNullException.ThrowIfNull(predicate); |
| | 28 | 42 | | _rules.Add(new AssemblyOrderRule(predicate, _currentTier++)); |
| | 28 | 43 | | return this; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the ordering rules that have been configured. |
| | | 48 | | /// </summary> |
| | 1 | 49 | | public IReadOnlyList<AssemblyOrderRule> Rules => _rules; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Sorts assemblies according to the configured rules. |
| | | 53 | | /// Each assembly is placed in the first tier it matches. |
| | | 54 | | /// Unmatched assemblies are placed last. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="assemblies">The assemblies to sort.</param> |
| | | 57 | | /// <returns>The sorted assemblies.</returns> |
| | | 58 | | public IReadOnlyList<Assembly> Sort(IEnumerable<Assembly> assemblies) |
| | | 59 | | { |
| | 30 | 60 | | ArgumentNullException.ThrowIfNull(assemblies); |
| | | 61 | | |
| | 30 | 62 | | var assemblyList = assemblies.ToList(); |
| | 30 | 63 | | if (assemblyList.Count == 0 || _rules.Count == 0) |
| | | 64 | | { |
| | 0 | 65 | | return assemblyList; |
| | | 66 | | } |
| | | 67 | | |
| | 30 | 68 | | var unmatchedTier = _currentTier; // Unmatched assemblies go last |
| | | 69 | | |
| | 30 | 70 | | var tieredAssemblies = assemblyList |
| | 30 | 71 | | .Select(assembly => |
| | 30 | 72 | | { |
| | 153 | 73 | | var info = AssemblyInfo.FromAssembly(assembly); |
| | 153 | 74 | | var tier = GetTier(info, unmatchedTier); |
| | 153 | 75 | | return (Assembly: assembly, Tier: tier, Name: info.Name); |
| | 30 | 76 | | }) |
| | 153 | 77 | | .OrderBy(x => x.Tier) |
| | 153 | 78 | | .ThenBy(x => x.Name, StringComparer.OrdinalIgnoreCase) // Alphabetical within tier |
| | 153 | 79 | | .Select(x => x.Assembly) |
| | 30 | 80 | | .ToList(); |
| | | 81 | | |
| | 30 | 82 | | return tieredAssemblies; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Sorts assembly names according to the configured rules. |
| | | 87 | | /// Used for source-gen scenarios where only names are available. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="assemblyNames">The assembly names to sort.</param> |
| | | 90 | | /// <returns>The sorted assembly names.</returns> |
| | | 91 | | public IReadOnlyList<string> SortNames(IEnumerable<string> assemblyNames) |
| | | 92 | | { |
| | 10 | 93 | | ArgumentNullException.ThrowIfNull(assemblyNames); |
| | | 94 | | |
| | 9 | 95 | | var nameList = assemblyNames.ToList(); |
| | 9 | 96 | | if (nameList.Count == 0 || _rules.Count == 0) |
| | | 97 | | { |
| | 2 | 98 | | return nameList; |
| | | 99 | | } |
| | | 100 | | |
| | 7 | 101 | | var unmatchedTier = _currentTier; |
| | | 102 | | |
| | 7 | 103 | | var tieredNames = nameList |
| | 7 | 104 | | .Select(name => |
| | 7 | 105 | | { |
| | 27 | 106 | | var info = AssemblyInfo.FromStrings(name); |
| | 27 | 107 | | var tier = GetTier(info, unmatchedTier); |
| | 27 | 108 | | return (Name: name, Tier: tier); |
| | 7 | 109 | | }) |
| | 27 | 110 | | .OrderBy(x => x.Tier) |
| | 27 | 111 | | .ThenBy(x => x.Name, StringComparer.OrdinalIgnoreCase) |
| | 27 | 112 | | .Select(x => x.Name) |
| | 7 | 113 | | .ToList(); |
| | | 114 | | |
| | 7 | 115 | | return tieredNames; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private int GetTier(AssemblyInfo info, int unmatchedTier) |
| | | 119 | | { |
| | 680 | 120 | | foreach (var rule in _rules) |
| | | 121 | | { |
| | 218 | 122 | | if (rule.CompiledPredicate(info)) |
| | | 123 | | { |
| | 116 | 124 | | return rule.Tier; |
| | | 125 | | } |
| | | 126 | | } |
| | 64 | 127 | | return unmatchedTier; |
| | 116 | 128 | | } |
| | | 129 | | } |