Skip to content

Commit ce141bb

Browse files
committed
feat: cache installed runtime
feat: nuget package loader more logging feat: nuget package loader external access to paths
1 parent 1cb8b97 commit ce141bb

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

Core.Common.Standard/Helpers/InstalledRuntime.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace KY.Core
88
{
99
public class InstalledRuntime
1010
{
11+
private static InstalledRuntime[] cache;
1112
public string Type { get; }
1213
public Version Version { get; }
1314
public string Path { get; }
@@ -29,7 +30,7 @@ public static InstalledRuntime Parse(string value)
2930

3031
public static InstalledRuntime[] Get()
3132
{
32-
Process process = new Process();
33+
Process process = new();
3334
process.StartInfo.FileName = "dotnet";
3435
process.StartInfo.Arguments = "--list-runtimes";
3536
process.StartInfo.UseShellExecute = false;
@@ -44,7 +45,11 @@ public static InstalledRuntime[] Get()
4445

4546
public static InstalledRuntime[] GetCurrent()
4647
{
47-
return Get().Where(x => x.Version == Environment.Version).ToArray();
48+
if (cache == null)
49+
{
50+
cache = Get().Where(x => x.Version == Environment.Version).ToArray();
51+
}
52+
return cache;
4853
}
4954
}
50-
}
55+
}

Core.Common.Standard/Nuget/AssemblyCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace KY.Core;
44

55
public interface IAssemblyCache
66
{
7-
Dictionary<string, string> Global { get; set; }
8-
Dictionary<string, string> Local { get; set; }
7+
void Add(string name, string location);
8+
string Resolve(string name);
99
}

Core.Common.Standard/Nuget/NugetAssemblyLocator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Reflection;
@@ -209,6 +210,8 @@ private Assembly TryFind(AssemblyInfo info, params string[] chunks)
209210
if (FileSystem.FileExists(file))
210211
{
211212
Logger.Trace($"Assembly found in: {file}");
213+
Stopwatch stopwatch = new();
214+
stopwatch.Start();
212215
try
213216
{
214217
return AssemblyLoadContext.Default?.LoadFromAssemblyPath(file);
@@ -226,6 +229,11 @@ private Assembly TryFind(AssemblyInfo info, params string[] chunks)
226229
AssemblyLoadContext.GetLoadContext(assembly)?.Unload();
227230
return AssemblyLoadContext.Default?.LoadFromAssemblyPath(file);
228231
}
232+
finally
233+
{
234+
stopwatch.Stop();
235+
Logger.Trace($"Assembly {info.Name} loaded in {(stopwatch.ElapsedMilliseconds >= 1 ? stopwatch.ElapsedMilliseconds.ToString() : "<1")} ms");
236+
}
229237
}
230238
else
231239
{

Core.Common.Standard/Nuget/NugetPackageDependencyLoader.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Reflection;
56
using System.Runtime.InteropServices;
@@ -10,6 +11,9 @@ namespace KY.Core
1011
{
1112
public static class NugetPackageDependencyLoader
1213
{
14+
public static readonly string WindowsNugetCachePath = FileSystem.Combine(Environment.ExpandEnvironmentVariables("%USERPROFILE%"), ".nuget", "packages");
15+
public static readonly string WindowsNugetFallbackPath = FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet", "sdk", "NuGetFallbackFolder");
16+
public static readonly string LinuxNugetCachePath = FileSystem.Combine(Environment.GetEnvironmentVariable("HOME"), ".nuget", "packages");
1317
private static bool isActivated;
1418
private static bool isRuntimeLocationsAdded;
1519
private static IAssemblyCache cache;
@@ -26,18 +30,19 @@ static NugetPackageDependencyLoader()
2630
};
2731
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2832
{
29-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%USERPROFILE%"), ".nuget", "packages")).SearchOnlyByVersion());
30-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet", "sdk", "NuGetFallbackFolder")).SearchOnlyByVersion());
31-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet", "sdk", "NuGetFallbackFolder")).SearchOnlyLocal());
33+
Locations.Add(new SearchLocation(WindowsNugetCachePath).SearchOnlyByVersion());
34+
Locations.Add(new SearchLocation(WindowsNugetFallbackPath).SearchOnlyByVersion());
35+
Locations.Add(new SearchLocation(WindowsNugetFallbackPath).SearchOnlyLocal());
3236
}
3337
else
3438
{
35-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.GetEnvironmentVariable("HOME"), ".nuget", "packages")).SearchOnlyByVersion());
39+
Locations.Add(new SearchLocation(LinuxNugetCachePath).SearchOnlyByVersion());
3640
}
3741
}
3842

39-
public static void Activate(IAssemblyCache assemblyCache)
43+
public static void Activate(IAssemblyCache assemblyCache = null)
4044
{
45+
cache = assemblyCache ?? cache;
4146
if (isActivated)
4247
{
4348
return;
@@ -55,17 +60,21 @@ public static void Deactivate()
5560

5661
private static Assembly Resolve(object sender, ResolveEventArgs args)
5762
{
58-
if (cache?.Local != null && cache.Local.TryGetValue(args.Name, out string localPath) && FileSystem.FileExists(localPath))
63+
string assemblyPath = cache?.Resolve(args.Name);
64+
if (assemblyPath != null && FileSystem.FileExists(assemblyPath))
5965
{
60-
return Assembly.Load(localPath);
66+
Stopwatch stopwatch = new();
67+
stopwatch.Start();
68+
Assembly cachedAssembly = AssemblyLoadContext.Default?.LoadFromAssemblyPath(assemblyPath);
69+
stopwatch.Stop();
70+
Logger.Trace($"Assembly {args.Name.Split(',').FirstOrDefault()} loaded in {(stopwatch.ElapsedMilliseconds >= 1 ? stopwatch.ElapsedMilliseconds.ToString() : "<1")} ms");
71+
return cachedAssembly;
6172
}
62-
if (cache?.Global != null && cache.Global.TryGetValue(args.Name, out string globalPath) && FileSystem.FileExists(globalPath))
63-
{
64-
return Assembly.Load(globalPath);
65-
}
66-
return CreateLocator()
67-
.AddLocation(0, args.RequestingAssembly)
68-
.Locate(args.Name, null, false, true);
73+
Assembly assembly = CreateLocator()
74+
.AddLocation(0, args.RequestingAssembly)
75+
.Locate(args.Name, null, false, true);
76+
cache?.Add(args.Name, assembly.Location);
77+
return assembly;
6978
}
7079

7180
public static NugetAssemblyLocator CreateLocator()

0 commit comments

Comments
 (0)