functions/private/Find-SodiumLibrary.ps1
|
function Find-SodiumLibrary { $arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture $archStr = switch ($arch) { ([System.Runtime.InteropServices.Architecture]::X64) { 'x64' } ([System.Runtime.InteropServices.Architecture]::Arm64) { 'arm64' } ([System.Runtime.InteropServices.Architecture]::X86) { 'x86' } default { throw "Unsupported CPU architecture: $arch" } } $isWindows = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows) $isLinux = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Linux) $isMacOS = [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX) if ($isWindows) { $rid = "win-$archStr"; $libFile = 'libsodium.dll' } elseif ($isLinux) { $rid = "linux-$archStr"; $libFile = 'libsodium.so' } elseif ($isMacOS) { $rid = "osx-$archStr"; $libFile = 'libsodium.dylib' } else { throw 'Unsupported operating system.' } $nativeDir = Join-Path $script:moduleRoot 'native' $rid $cachedPath = Join-Path $nativeDir $libFile if (Test-Path $cachedPath) { return $cachedPath } $nupkgUrl = 'https://api.nuget.org/v3-flatcontainer/libsodium/1.0.18/libsodium.1.0.18.nupkg' $tmpFile = [System.IO.Path]::GetTempFileName() try { Write-Verbose "[SimpleSodiumPS] Downloading libsodium 1.0.18 from NuGet..." Invoke-WebRequest -Uri $nupkgUrl -OutFile $tmpFile -UseBasicParsing $null = New-Item -ItemType Directory -Path $nativeDir -Force Add-Type -AssemblyName System.IO.Compression.FileSystem $zip = [System.IO.Compression.ZipFile]::OpenRead($tmpFile) try { $entryPath = "runtimes/$rid/native/$libFile" $entry = $zip.Entries | Where-Object { $_.FullName -eq $entryPath } if (-not $entry) { throw "libsodium NuGet package does not contain '$entryPath'. Platform '$rid' may not be supported." } [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $cachedPath, $true) } finally { $zip.Dispose() } if (-not $isWindows) { & chmod +x $cachedPath } Write-Verbose "[SimpleSodiumPS] libsodium cached at: $cachedPath" return $cachedPath } finally { Remove-Item $tmpFile -ErrorAction SilentlyContinue } } |