Skip to content

Commit

Permalink
[WIP] NuGetAssemblyResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Mar 25, 2024
1 parent baaf2ca commit f1d1d3d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Chisel/Chisel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -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;
}

/// <inheritdoc />
public override bool Execute()
{
Expand Down
86 changes: 86 additions & 0 deletions src/Chisel/NuGetAssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -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<DirectoryInfo?> 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}");
}
}

0 comments on commit f1d1d3d

Please sign in to comment.