Skip to content

Commit

Permalink
Merge pull request #141 from gabriel-samfira/use-load-file
Browse files Browse the repository at this point in the history
Use LoadFile() and resolve dependencies manually
  • Loading branch information
gabriel-samfira authored Aug 28, 2024
2 parents 8006556 + 0fd6c93 commit cc9cf5a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ dotnet build --configuration Release $here/src/
$destinations = @("netstandard2.1", "net47")

foreach ($item in $destinations) {
$src = Join-Path $here "src" "bin" "Release" $item "serializer.dll"
$dst = Join-Path $here "lib" $item "StringQuotingEmitter.dll"
$src = Join-Path $here "src" "bin" "Release" $item "PowerShellYamlSerializer.dll"
$dst = Join-Path $here "lib" $item "PowerShellYamlSerializer.dll"

Copy-Item -Force $src $dst
}
Binary file added lib/net47/PowerShellYamlSerializer.dll
Binary file not shown.
Binary file removed lib/net47/StringQuotingEmitter.dll
Binary file not shown.
Binary file not shown.
Binary file removed lib/netstandard2.1/StringQuotingEmitter.dll
Binary file not shown.
66 changes: 34 additions & 32 deletions powershell-yaml.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,56 @@ enum SerializationOptions {
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$infinityRegex = [regex]::new('^[-+]?(\.inf|\.Inf|\.INF)$', "Compiled, CultureInvariant");

function Invoke-LoadInContext {
param(
[string]$assemblyPath,
[string]$loadContextName
)

$loadContext = [System.Runtime.Loader.AssemblyLoadContext]::New($loadContextName, $true)
$assemblies = $loadContext.LoadFromAssemblyPath($assemblyPath)
$basePath = Split-Path -Parent $assemblyPath
$quoted = Join-Path $basePath "StringQuotingEmitter.dll"
$quotedAssembly = $loadContext.LoadFromAssemblyPath($quoted)

return @{ "yaml"= $assemblies; "quoted" = $quotedAssembly }
}

function Invoke-LoadInGlobalContext {
function Invoke-LoadFile {
param(
[string]$assemblyPath
)

$assemblies = [Reflection.Assembly]::LoadFrom($assemblyPath)
$basePath = Split-Path -Parent $assemblyPath
$quoted = Join-Path $basePath "StringQuotingEmitter.dll"
$quotedAssembly = [Reflection.Assembly]::LoadFrom($quoted)
$global:powershellYamlDotNetAssemblyPath = Join-Path $assemblyPath "YamlDotNet.dll"
$serializerAssemblyPath = Join-Path $assemblyPath "PowerShellYamlSerializer.dll"
$yamlAssembly = [Reflection.Assembly]::LoadFile($powershellYamlDotNetAssemblyPath)
$serializerAssembly = [Reflection.Assembly]::LoadFile($serializerAssemblyPath)

if ($PSVersionTable['PSEdition'] -eq 'Core') {
# Register the AssemblyResolve event to load dependencies manually. This seems to be needed only on
# PowerShell Core.
[System.AppDomain]::CurrentDomain.add_AssemblyResolve({
param ($sender, $e)
$pth = $powershellYamlDotNetAssemblyPath
# Load YamlDotNet if it's requested by PowerShellYamlSerializer. Ignore other requests as they might
# originate from other assemblies that are not part of this module and which might have different
# versions of the module that they need to load.
if ($e.Name -like "*YamlDotNet*" -and $e.RequestingAssembly -like "*PowerShellYamlSerializer*" ) {
return [System.Reflection.Assembly]::LoadFile($powershellYamlDotNetAssemblyPath)
}

return @{ "yaml"= $assemblies; "quoted" = $quotedAssembly }
return $null
})
# Load the StringQuotingEmitter from PowerShellYamlSerializer to force the resolver handler to fire once.
# This will load the YamlDotNet assembly and expand the global variable $powershellYamlDotNetAssemblyPath.
# We then remove it to avoid polluting the global scope.
# This is an ugly hack I am not happy with.
$serializerAssembly.GetType("StringQuotingEmitter") | Out-Null
}

Remove-Variable -Name powershellYamlDotNetAssemblyPath -Scope Global
return @{ "yaml"= $yamlAssembly; "quoted" = $serializerAssembly }
}

function Invoke-LoadAssembly {
$libDir = Join-Path $here "lib"
$assemblies = @{
"core" = Join-Path $libDir "netstandard2.1\YamlDotNet.dll";
"net47" = Join-Path $libDir "net47\YamlDotNet.dll";
"net35" = Join-Path $libDir "net35\YamlDotNet.dll";
"core" = Join-Path $libDir "netstandard2.1";
"net47" = Join-Path $libDir "net47";
}

if ($PSVersionTable.Keys -contains "PSEdition") {
if ($PSVersionTable.PSEdition -eq "Core") {
return (Invoke-LoadInContext -assemblyPath $assemblies["core"] -loadContextName "powershellyaml")
} elseif ($PSVersionTable.PSVersion.Major -gt 5.1) {
return (Invoke-LoadInContext -assemblyPath $assemblies["net47"] -loadContextName "powershellyaml")
} elseif ($PSVersionTable.PSVersion.Major -ge 4) {
return Invoke-LoadInGlobalContext $assemblies["net47"]
} else {
return Invoke-LoadInGlobalContext $assemblies["net45"]
return (Invoke-LoadFile -assemblyPath $assemblies["core"])
}
return (Invoke-LoadFile -assemblyPath $assemblies["net47"])
} else {
return Invoke-LoadInGlobalContext $assemblies["net45"]
return (Invoke-LoadFile -assemblyPath $assemblies["net47"])
}
}

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit cc9cf5a

Please sign in to comment.