|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using Microsoft.Extensions.DependencyModel; |
| 7 | +using Microsoft.Extensions.DependencyModel.Resolution; |
| 8 | + |
| 9 | +namespace Reqnroll.Plugins; |
| 10 | + |
| 11 | +public abstract class AssemblyResolverBase |
| 12 | +{ |
| 13 | + private readonly Lazy<Assembly> _assembly; |
| 14 | + |
| 15 | + public Assembly GetAssembly() => _assembly.Value; |
| 16 | + |
| 17 | + private ICompilationAssemblyResolver _assemblyResolver; |
| 18 | + private DependencyContext _dependencyContext; |
| 19 | + |
| 20 | + protected AssemblyResolverBase(string relativePath) |
| 21 | + { |
| 22 | + var path = Path.GetFullPath(relativePath); |
| 23 | + _assembly = new Lazy<Assembly>(() => Initialize(path)); |
| 24 | + } |
| 25 | + |
| 26 | + protected abstract Assembly Initialize(string path); |
| 27 | + |
| 28 | + protected void SetupDependencyContext(string path, Assembly assembly, bool throwOnError) |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + _dependencyContext = DependencyContext.Load(assembly); |
| 33 | + |
| 34 | + if (_dependencyContext is null) return; |
| 35 | + |
| 36 | + _assemblyResolver = new CompositeCompilationAssemblyResolver( |
| 37 | + [ |
| 38 | + new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(path)!), |
| 39 | + new ReferenceAssemblyPathResolver(), |
| 40 | + new PackageCompilationAssemblyResolver() |
| 41 | + ]); |
| 42 | + } |
| 43 | + catch (Exception) |
| 44 | + { |
| 45 | + if (throwOnError) |
| 46 | + throw; |
| 47 | + |
| 48 | + // We ignore if there was a problem with initializing context from .deps.json |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected abstract Assembly LoadAssemblyFromPath(string assemblyPath); |
| 53 | + |
| 54 | + protected Assembly TryResolveAssembly(AssemblyName name) |
| 55 | + { |
| 56 | + var library = _dependencyContext?.RuntimeLibraries.FirstOrDefault( |
| 57 | + runtimeLibrary => string.Equals(runtimeLibrary.Name, name.Name, StringComparison.OrdinalIgnoreCase)); |
| 58 | + |
| 59 | + if (library == null) |
| 60 | + return null; |
| 61 | + |
| 62 | + var wrapper = new CompilationLibrary( |
| 63 | + library.Type, |
| 64 | + library.Name, |
| 65 | + library.Version, |
| 66 | + library.Hash, |
| 67 | + library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), |
| 68 | + library.Dependencies, |
| 69 | + library.Serviceable, |
| 70 | + library.Path, |
| 71 | + library.HashPath); |
| 72 | + |
| 73 | + var assemblies = new List<string>(); |
| 74 | + _assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies); |
| 75 | + |
| 76 | + if (_assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies) && assemblies.Count > 0) |
| 77 | + { |
| 78 | + foreach (var assemblyPath in assemblies) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + return LoadAssemblyFromPath(assemblyPath); |
| 83 | + } |
| 84 | + catch |
| 85 | + { |
| 86 | + // Don't throw if we can't load the specified assembly (perhaps something is missing or misconfigured) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | + } |
| 93 | +} |
0 commit comments