Skip to content

Commit 165c291

Browse files
Add ensureloaded function
1 parent 3cb5008 commit 165c291

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

src/dsc/psresourceget.ps1

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,42 @@ function Write-Trace {
222222
}
223223
}
224224

225+
function EnsureAssemblyLoadedByName {
226+
param(
227+
[Parameter(Mandatory)]
228+
[string] $SimpleName,
229+
230+
[string] $Path # optional fallback
231+
)
232+
233+
$loaded = [AppDomain]::CurrentDomain.GetAssemblies() |
234+
Where-Object { $_.GetName().Name -eq $SimpleName } |
235+
Select-Object -First 1
236+
237+
if ($loaded) {
238+
# Already loaded — do nothing and return the loaded assembly
239+
return $loaded
240+
}
241+
242+
if ($Path) {
243+
# Not loaded — load from path
244+
Add-Type -Path $Path -ErrorAction Stop | Out-Null
245+
return [AppDomain]::CurrentDomain.GetAssemblies() |
246+
Where-Object { $_.GetName().Name -eq $SimpleName } |
247+
Select-Object -First 1
248+
}
249+
250+
throw "Assembly '$SimpleName' is not loaded and no -Path was provided."
251+
}
252+
253+
225254
function SatisfiesVersion {
226255
param(
227256
[string]$version,
228257
[string]$versionRange
229258
)
230259

231-
Add-Type -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll"
260+
EnsureAssemblyLoadedByName -SimpleName "NuGet.Versioning" -Path "$PSScriptRoot/dependencies/NuGet.Versioning.dll" | Out-Null
232261

233262
try {
234263
$versionRangeObj = [NuGet.Versioning.VersionRange]::Parse($versionRange)

0 commit comments

Comments
 (0)