1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
3
4
using System . Linq ;
4
5
using System . Reflection ;
5
6
using System . Runtime . InteropServices ;
@@ -10,6 +11,9 @@ namespace KY.Core
10
11
{
11
12
public static class NugetPackageDependencyLoader
12
13
{
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" ) ;
13
17
private static bool isActivated ;
14
18
private static bool isRuntimeLocationsAdded ;
15
19
private static IAssemblyCache cache ;
@@ -26,18 +30,19 @@ static NugetPackageDependencyLoader()
26
30
} ;
27
31
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
28
32
{
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 ( ) ) ;
32
36
}
33
37
else
34
38
{
35
- Locations . Add ( new SearchLocation ( FileSystem . Combine ( Environment . GetEnvironmentVariable ( "HOME" ) , ".nuget" , "packages" ) ) . SearchOnlyByVersion ( ) ) ;
39
+ Locations . Add ( new SearchLocation ( LinuxNugetCachePath ) . SearchOnlyByVersion ( ) ) ;
36
40
}
37
41
}
38
42
39
- public static void Activate ( IAssemblyCache assemblyCache )
43
+ public static void Activate ( IAssemblyCache assemblyCache = null )
40
44
{
45
+ cache = assemblyCache ?? cache ;
41
46
if ( isActivated )
42
47
{
43
48
return ;
@@ -55,17 +60,21 @@ public static void Deactivate()
55
60
56
61
private static Assembly Resolve ( object sender , ResolveEventArgs args )
57
62
{
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 ) )
59
65
{
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 ;
61
72
}
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 ;
69
78
}
70
79
71
80
public static NugetAssemblyLocator CreateLocator ( )
0 commit comments