NCache.psm1

#
# Script module for module 'NCache'
#

# Summary: PackageManagement is supported on Windows PowerShell 3.0 or later, Nano Server and PowerShellCore

$isCore = ($PSVersionTable.Keys -contains "PSEdition") -and ($PSVersionTable.PSEdition -ne 'Desktop')
$binarySubPath = ''
if ($isCore)
{
    $binarySubPath = 'core'
} else {
    $binarySubPath = 'framework'
}

# Set up some helper variables to make it easier to work with the module
$script:PSModule = $ExecutionContext.SessionState.Module
$script:PSModuleRoot = $script:PSModule.ModuleBase

if ($isCore)
{
    $script:PSPkgMgmt = 'ncacheps.dll'
} else {
       $script:PSPkgMgmt = 'NCache.psd1'
}

$binaryModuleRoot = Join-Path -Path $script:PSModuleRoot -ChildPath $binarySubPath
$PSOneGetModulePath = Join-Path -Path $binaryModuleRoot -ChildPath $script:PSPkgMgmt


# ============================================================
# NCache System.Memory assembly resolver
# ============================================================

if (-not ("NCacheAssemblyResolver" -as [type]))
{
    Add-Type @"
using System;
using System.IO;
using System.Reflection;
 
public static class NCacheAssemblyResolver
{
    private static string _moduleRoot;
 
    public static void Install(string moduleRoot)
    {
        _moduleRoot = moduleRoot;
 
        AppDomain.CurrentDomain.AssemblyResolve -= Resolve;
        AppDomain.CurrentDomain.AssemblyResolve += Resolve;
    }
 
    private static Assembly Resolve(object sender, ResolveEventArgs args)
    {
        try
        {
            AssemblyName requested = new AssemblyName(args.Name);
 
           if (!requested.Name.Equals(
            "System.Memory",
            StringComparison.OrdinalIgnoreCase)
            &&
            !requested.Name.Equals(
                "System.IO.Pipelines",
                StringComparison.OrdinalIgnoreCase))
        {
            return null;
        }
 
            string path = Path.Combine(
            _moduleRoot,
            "framework",
            requested.Name + ".dll"
        );
 
            if (!File.Exists(path))
            {
                return null;
            }
 
          // If the requested assembly is already loaded, use it.
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (assembly.GetName().Name.Equals(
                    requested.Name,
                    StringComparison.OrdinalIgnoreCase))
                {
                    return assembly;
                }
            }
 
            // Requested version may be 4.0.1.1,
            // while the file we ship is 4.0.1.2.
            return Assembly.LoadFrom(path);
        }
        catch
        {
            return null;
        }
    }
}
"@

}

[NCacheAssemblyResolver]::Install($script:PSModuleRoot)
# ============================================================
# Load NCache binary module
# ============================================================


$PSOneGetModule = Import-Module -Name $PSOneGetModulePath -PassThru -FORCE

if($PSOneGetModule)
{
    $script:PSModule.OnRemove = {
        Remove-Module -ModuleInfo $PSOneGetModule
    }
}