diff --git a/src/Chisel/Chisel.cs b/src/Chisel/Chisel.cs index d5d78d6..2dd0ab8 100644 --- a/src/Chisel/Chisel.cs +++ b/src/Chisel/Chisel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; @@ -119,6 +119,12 @@ public class Chisel : Task [Output] public long? BytesSaved { get; private set; } + static Chisel() + { + NuGetAssemblyResolver.LogToFile("static Chisel()"); + AppDomain.CurrentDomain.AssemblyResolve += NuGetAssemblyResolver.ResolveAssembly; + } + /// public override bool Execute() { diff --git a/src/Chisel/NuGetAssemblyResolver.cs b/src/Chisel/NuGetAssemblyResolver.cs new file mode 100644 index 0000000..a077e60 --- /dev/null +++ b/src/Chisel/NuGetAssemblyResolver.cs @@ -0,0 +1,86 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Text.RegularExpressions; + +namespace Chisel; + +internal static class NuGetAssemblyResolver +{ + private static readonly Lazy DotnetSdkDirectory = new(GetDotnetSdkDirectory); + + private static DirectoryInfo? GetDotnetSdkDirectory() + { + var dotnet = new Process + { + StartInfo = new ProcessStartInfo("dotnet") + { + Arguments = "--list-sdks", + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + }, + }; + dotnet.Start(); + + using var reader = new StringReader(dotnet.StandardOutput.ReadToEnd()); + DirectoryInfo? dotnetSdkDirectory = null; + while (reader.ReadLine() is {} line) + { + var match = Regex.Match(line, @"(.*) \[(.*)\]"); + if (match.Success) + { + + } + } + + dotnet.WaitForExit(2000); + + return dotnetSdkDirectory; + } + + public static Assembly? ResolveAssembly(object sender, ResolveEventArgs args) + { + try + { + var dotnetSdkDirectory = DotnetSdkDirectory.Value; + if (dotnetSdkDirectory == null) + { + return null; + } + + var appDomain = sender as AppDomain; + LogToFile($"AppDomain BaseDirectory: {appDomain?.BaseDirectory}"); + LogToFile($"AppDomain DynamicDirectory: {appDomain?.DynamicDirectory}"); + LogToFile($"AppDomain RelativeSearchPath: {appDomain?.RelativeSearchPath}"); + LogToFile($"AppDomain FriendlyName: {appDomain?.FriendlyName}"); + + LogToFile($"ResolveAssembly sender <{sender.GetType()}> RequestingAssembly <{args.RequestingAssembly}> Name <{args.Name}>"); + var assemblyName = new AssemblyName(args.Name); + var assemblyFile = Path.Combine(dotnetSdkDirectory.FullName, $"{assemblyName.Name}.dll"); + if (File.Exists(assemblyFile)) + { + LogToFile($"Loading {assemblyFile}"); + var result = Assembly.LoadFrom(assemblyFile); + LogToFile($"Loaded {result}"); + return result; + } + + LogToFile($"Assembly not found: {assemblyFile}"); + return null; + } + catch (Exception exception) + { + Trace.WriteLine(exception); + return null; + } + } + + public static void LogToFile(string message) + { + using var stream = new FileStream("Chisel.log", FileMode.Append); + using var writer = new StreamWriter(stream); + writer.WriteLine($"[{DateTime.Now:O}] {message}"); + } +} \ No newline at end of file