internal/functions/Initialize-ExternalLibrary.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Initialize-ExternalLibrary {
    #Load external libraries for a specific RDBMS
    Param (
        [Parameter(Mandatory)]
        [DBOps.ConnectionType]$Type
    )
    # try looking up already loaded assemblies
    $libs = [System.AppDomain]::CurrentDomain.GetAssemblies().GetName()
    $dependencies = Get-ExternalLibrary -Type $Type
    $isLoaded = $true
    foreach ($dPackage in $dependencies) {
        if ($lib = $libs | Where-Object Name -eq $dPackage.Name) {
            if ($minVersion = $dPackage.MinimumVersion -as [version]) {
                if ($minVersion -gt $lib.Version) {
                    $isLoaded = $false; break
                }
            }
            if ($maxVersion = $dPackage.MaximumVersion -as [version]) {
                if ($maxVersion -lt $lib.Version) {
                    $isLoaded = $false; break
                }
            }
            if ($reqVersion = $dPackage.RequiredVersion -as [version]) {
                if ($reqVersion -eq $lib.Version) {
                    $isLoaded = $false; break
                }
            }
            Write-PSFMessage -Level Verbose -Message "$($lib.Name) $($lib.Version) was found among the loaded libraries, assuming that the library is fully loaded"
        }
        else {
            $isLoaded = $false; break
        }
    }
    if ($isLoaded) {
        Write-PSFMessage -Level Debug -Message "All libraries for $Type were found among the loaded libraries"
        return
    }
    # get package from the local system
    if (-Not (Test-DBOSupportedSystem -Type $Type)) {
        Write-PSFMessage -Level Warning -Message "Installing dependent libraries for $Type connections"
        # Install dependencies into the current user scope
        $null = Install-DBOSupportLibrary -Type $Type -Scope CurrentUser
        # test again
        if (-Not (Test-DBOSupportedSystem -Type $Type)) {
            Write-PSFMessage -Level Warning -Message "Dependent libraries for $Type were not found. Run Install-DBOSupportLibrary -Type $Type"
            Stop-PSFFunction -EnableException $true -Message "$Type is not supported on this system - some of the external dependencies are missing."
            return
        }
    }
    $dependencies = Get-ExternalLibrary -Type $Type
    foreach ($package in $dependencies) {
        $packageSplat = @{
            Name            = $package.Name
            MinimumVersion  = $package.MinimumVersion
            MaximumVersion  = $package.MaximumVersion
            RequiredVersion = $package.RequiredVersion
            ProviderName    = "nuget"
        }
        $localPackage = Get-Package @packageSplat
        foreach ($dPath in $package.Path) {
            Write-PSFMessage -Level Debug -Message "Loading library $dPath from $($localPackage.Source)"
            try {
                $null = Add-Type -Path (Join-PSFPath -Normalize (Split-Path $localPackage.Source -Parent) $dPath) -ErrorAction SilentlyContinue
            }
            catch {
                Stop-PSFFunction -EnableException $true -Message "Could not load $dPath from $localPackage" -ErrorRecord $_
            }
        }
    }
}