|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Loads Newtonsoft.Json and Newtonsoft.Json.Schema assemblies from NuGet. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +This script loads the Newtonsoft.Json and Newtonsoft.Json.Schema assemblies from NuGet. |
| 7 | +It checks if the assemblies are already loaded, installs them if not, and then loads them. |
| 8 | +
|
| 9 | +.PARAMETER Path |
| 10 | +The path where the assemblies will be installed or loaded from. If not specified, it defaults to the user's profile directory. |
| 11 | +
|
| 12 | +.EXAMPLE |
| 13 | +.\Load-NewtonsoftDlls.ps1 -Path "C:\Path\To\Your\Modules" |
| 14 | +#> |
| 15 | + |
| 16 | +[CmdletBinding()] |
| 17 | +param ( |
| 18 | + [Parameter(Mandatory = $false, HelpMessage = "Path to the directory where the assemblies will be installed or loaded from.")] |
| 19 | + [string]$Path |
| 20 | +) |
| 21 | + |
| 22 | +function Get-DllFile { |
| 23 | + [CmdletBinding()] |
| 24 | + [OutputType([String])] |
| 25 | + param( |
| 26 | + [Parameter(Mandatory = $true)] |
| 27 | + [string]$Path, |
| 28 | + |
| 29 | + [Parameter(Mandatory = $true)] |
| 30 | + [string]$PackageName |
| 31 | + ) |
| 32 | + |
| 33 | + $dllPath = Get-ChildItem -Path "$Path" -Filter "$PackageName.dll" -File -Recurse -ErrorAction SilentlyContinue | ` |
| 34 | + Select-Object -First 1 -ExpandProperty FullName |
| 35 | + |
| 36 | + return $dllPath |
| 37 | +} |
| 38 | + |
| 39 | +function Import-NewtonsoftDll { |
| 40 | + [CmdletBinding()] |
| 41 | + param( |
| 42 | + [Parameter(Mandatory = $true)] |
| 43 | + [ValidateSet("Newtonsoft.Json", "Newtonsoft.Json.Schema")] |
| 44 | + [string]$PackageName, |
| 45 | + |
| 46 | + [Parameter(Mandatory = $false)] |
| 47 | + [string]$Path |
| 48 | + ) |
| 49 | + |
| 50 | + Write-Host "Loading $PackageName assembly..." -ForegroundColor Cyan |
| 51 | + |
| 52 | + # Check if the assembly is already loaded |
| 53 | + $loadedAssemblies = [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { |
| 54 | + $_.GetName().Name -eq "$PackageName" |
| 55 | + } |
| 56 | + if ($loadedAssemblies) { |
| 57 | + Write-Warning "$PackageName already loaded.`n`tLocation: '$($loadedAssemblies.Location)'" |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + # Set the default path if not provided |
| 62 | + if (-not $Path) { |
| 63 | + $profileDir = Split-Path -Parent "$($PROFILE.CurrentUserCurrentHost)" |
| 64 | + $Path = Join-Path -Path "$profileDir" -ChildPath "Modules\newtonsoft.json" |
| 65 | + } |
| 66 | + Write-Debug "Path: $Path" |
| 67 | + if (-not (Test-Path -Path $Path)) { |
| 68 | + New-Item -Path $Path -ItemType Directory -Force | Out-Null |
| 69 | + } |
| 70 | + |
| 71 | + $framework = if ($PSVersionTable.PSEdition -eq "Core") { "netstandard2.0" } else { "net45" } |
| 72 | + $assembliesDir = Join-Path -Path $Path -ChildPath "$PackageName\lib\$framework" |
| 73 | + Write-Debug "Package directory: $assembliesDir" |
| 74 | + |
| 75 | + # Install the package if it doesn't exist from nuget |
| 76 | + $dllPath = Get-DllFile -Path $assembliesDir -PackageName $PackageName |
| 77 | + |
| 78 | + if (-not $dllPath) { |
| 79 | + # Check if nuget.exe is available |
| 80 | + if (-not (Get-Command nuget.exe -ErrorAction SilentlyContinue)) { |
| 81 | + Write-Error "'nuget.exe' not found in PATH. Please install or add it to PATH." |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + Write-Host "Installing $PackageName from NuGet." -ForegroundColor Cyan |
| 86 | + nuget.exe install $PackageName -NoHttpCache -ExcludeVersion -NonInteractive -Force ` |
| 87 | + -OutputDirectory $Path ` |
| 88 | + -Framework $framework |
| 89 | + |
| 90 | + $dllPath = Get-DllFile -Path $assembliesDir -PackageName $PackageName |
| 91 | + if (-not $dllPath) { |
| 92 | + Write-Error "The $PackageName.dll file could not be found in the provided directory after install." |
| 93 | + return |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + # Load the assembly from the path |
| 98 | + try { |
| 99 | + Write-Host "Loading assembly from '$dllPath'." -ForegroundColor Magenta |
| 100 | + $asm = [Reflection.Assembly]::LoadFile($dllPath) |
| 101 | + Write-Debug $asm |
| 102 | + } |
| 103 | + catch { |
| 104 | + Write-Error "Failed to load assembly from '$dllPath'. $_" |
| 105 | + return |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +Import-NewtonsoftDll -Path $Path -PackageName "Newtonsoft.Json.Schema" |
| 110 | +Import-NewtonsoftDll -Path $Path -PackageName "Newtonsoft.Json" |
0 commit comments