-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |