src/_Install-CciModuleFromBlob.ps1

function _Install-CciModuleFromBlob {
<#
.SYNOPSIS
    Installs a module (and its RequiredModules, recursively) from the tenant
    distribution store.
.DESCRIPTION
    The feed path gets dependency resolution for free from PSResourceGet; the
    store path resolves it explicitly, by reading RequiredModules from the
    installed manifest and fetching each dependency that is not already
    available. $Seen guards against cycles and repeat work.
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$Name,
        [Parameter(Mandatory)]$Feed,
        [Parameter(Mandatory)]$Context,
        [Parameter(Mandatory)]$Index,
        [string]$Version,
        [ValidateSet('CurrentUser','AllUsers')][string]$Scope = 'CurrentUser',
        [switch]$Reinstall,
        [System.Collections.Generic.HashSet[string]]$Seen
    )

    if (-not $Seen) { $Seen = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase) }
    if (-not $Seen.Add($Name)) { return }

    $entry = $Index.modules.PSObject.Properties[$Name]
    if (-not $entry) {
        throw "cciget: '$Name' is not present in the tenant distribution store index."
    }
    if (-not $Version) { $Version = $entry.Value.latest }

    $root   = _Get-CciModulesRoot -Scope $Scope
    $target = Join-Path (Join-Path $root $Name) $Version
    if ((Test-Path $target) -and -not $Reinstall) {
        Write-Host "cciget: $Name $Version already installed."
    }
    else {
        New-Item -ItemType Directory -Path $target -Force | Out-Null
        $prefix = "$($Feed.blobPrefix)/$Name/$Version/"
        $blobs  = @(_Invoke-CciBlobRequest -Context $Context -Operation List `
                        -Container $Feed.blobContainer -Prefix $prefix)
        if (-not $blobs) { throw "cciget: no content under '$prefix' in the distribution store." }
        Write-Host "cciget: downloading $Name $Version ($($blobs.Count) files)..."
        # A partial install is worse than no install - it looks importable but
        # is missing source files. Any failure removes the target so the next
        # attempt is not short-circuited by the "already installed" check.
        try {
            $expected = 0
            foreach ($b in $blobs) {
                $rel = $b.Substring($prefix.Length)
                if (-not $rel) { continue }
                $dest = Join-Path $target ($rel -replace '/', '\')
                $null = _Invoke-CciBlobRequest -Context $Context -Operation Download `
                            -Container $Feed.blobContainer -Blob $b -Path $dest
                $expected++
            }
            $actual = @(Get-ChildItem $target -Recurse -File).Count
            if ($actual -ne $expected) {
                throw "cciget: incomplete install of $Name $Version - expected $expected files, found $actual."
            }
        }
        catch {
            Remove-Item $target -Recurse -Force -ErrorAction SilentlyContinue
            throw
        }
        Write-Host "cciget: installed $Name $Version -> $target"
    }

    $psd1 = Get-ChildItem $target -Filter '*.psd1' | Select-Object -First 1
    if ($psd1) {
        $manifest = Import-PowerShellDataFile $psd1.FullName
        $required = if ($manifest.ContainsKey('RequiredModules')) { @($manifest.RequiredModules) } else { @() }
        foreach ($dep in $required) {
            if (-not $dep) { continue }
            $depName = if ($dep -is [hashtable]) { $dep.ModuleName } else { "$dep" }
            if (-not $depName) { continue }
            # A dependency that is merely PRESENT is not necessarily CURRENT.
            # Skipping on presence alone pinned a machine to whatever version it
            # happened to have: a build kept loading framework 0.2.1.12 after
            # 0.2.1.13 shipped, so a fix that had been published, mirrored and
            # verified never reached the machine it was for - and the failure
            # looked identical to the one already fixed.
            $depEntry   = $Index.modules.PSObject.Properties[$depName]
            $depVersion = if ($depEntry) { "$($depEntry.Value.latest)" } else { $null }
            $installed  = @(Get-Module -ListAvailable -Name $depName)
            if ($installed.Count -gt 0) {
                if (-not $depVersion) {
                    Write-Verbose "cciget: dependency $depName is available and not in the store index."
                    continue
                }
                $haveTarget = $false
                foreach ($m in $installed) {
                    if ("$($m.Version)" -eq $depVersion) { $haveTarget = $true }
                }
                if ($haveTarget) {
                    Write-Verbose "cciget: dependency $depName $depVersion already available."
                    continue
                }
                $present = (($installed | ForEach-Object { "$($_.Version)" }) -join ', ')
                Write-Host "cciget: dependency $depName is at $present; the store has $depVersion."
            }
            Write-Host "cciget: resolving dependency $depName..."
            _Install-CciModuleFromBlob -Name $depName -Feed $Feed -Context $Context -Index $Index `
                -Scope $Scope -Reinstall:$Reinstall -Seen $Seen
        }
    }
}