Skip to content

Commit 0838b3b

Browse files
Load NuGet.Versioning to a new ALC
1 parent fb2132a commit 0838b3b

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/dsc/psresourceget.ps1

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,40 @@ param(
1313
$stdinput
1414
)
1515

16+
$alcCode = @"
17+
using System;
18+
using System.IO;
19+
using System.Runtime.Loader;
20+
using System.Reflection;
21+
22+
public class IsolatedLoadContext : AssemblyLoadContext
23+
{
24+
private string _basePath;
25+
26+
public IsolatedLoadContext(string basePath) : base(isCollectible:true)
27+
{
28+
_basePath = basePath;
29+
}
30+
31+
protected override Assembly Load(AssemblyName assemblyName)
32+
{
33+
string depPath = Path.Combine(_basePath, assemblyName.Name + ".dll");
34+
35+
if (File.Exists(depPath))
36+
{
37+
return LoadFromAssemblyPath(depPath);
38+
}
39+
40+
return null;
41+
}
42+
}
43+
"@
44+
45+
Add-Type -TypeDefinition $alcCode
46+
47+
$assemblyFolder = "$PSScriptRoot/dependencies"
48+
$alc = [IsolatedLoadContext]::new($assemblyFolder)
49+
1650
enum Scope {
1751
CurrentUser
1852
AllUsers
@@ -228,29 +262,17 @@ function SatisfiesVersion {
228262
[string]$versionRange
229263
)
230264

265+
$asmPath = Join-Path $PSScriptRoot 'dependencies/NuGet.Versioning.dll'
266+
$pluginAssembly = $alc.LoadFromAssemblyPath($asmPath)
231267

232-
$loaded = [System.Runtime.Loader.AssemblyLoadContext]::All |
233-
ForEach-Object { $_.Assemblies } |
234-
Where-Object { $_.GetName().Name -eq 'NuGet.Versioning' }
235-
236-
Write-Verbose -Verbose "checking if NuGet.Versioning is already loaded: $loaded"
237-
238-
if (-not $loaded) {
239-
$nugetVersioningPath = Join-Path -Path $PSScriptRoot -ChildPath 'dependencies/NuGet.Versioning.dll'
240-
241-
if (-not (Test-Path -Path $nugetVersioningPath)) {
242-
Write-Trace -message "NuGet.Versioning.dll not found at expected path: $nugetVersioningPath" -level error
243-
return $false
244-
}
268+
try {
245269

246-
Write-Verbose -Verbose "Loading NuGet.Versioning assembly from $PSScriptRoot/dependencies/NuGet.Versioning.dll"
247-
Add-Type -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll"
270+
$versionRangeType = $pluginAssembly.GetType("NuGet.Versioning.VersionRange")
271+
$versionRangeObj = $versionRangeType.GetMethod("Parse", [Type[]]@([string])).Invoke($null, @($versionRange))
248272

249-
}
273+
$versionType = $pluginAssembly.GetType("NuGet.Versioning.NuGetVersion")
274+
$resourceVersion = $versionType.GetMethod("Parse", [Type[]]@([string])).Invoke($null, @($version))
250275

251-
try {
252-
$versionRangeObj = [NuGet.Versioning.VersionRange]::Parse($versionRange)
253-
$resourceVersion = [NuGet.Versioning.NuGetVersion]::Parse($version)
254276
return $versionRangeObj.Satisfies($resourceVersion)
255277
}
256278
catch {

0 commit comments

Comments
 (0)