Private/Invoke-ResolveDependency.ps1

<#
.SYNOPSIS
    Parse installed module based to get dependent module
.PARAMETER InstalledLocation
    Path to installed location of the module
.PARAMETER Credential
    Repository Credential
.PARAMETER Repository
    Name of the repository
.PARAMETER SkipImport
    When is set import-module will be skipped.
.PARAMETER Proxy
    Use it when is required to use proxy while accessing the repository
.PARAMETER ProxyCredential
    Use it when is required to use proxy with credential
 
#>

function Invoke-ResolveDependency {
    [CmdLetBinding()]
    [OutputType([PSCustomObject])]
    param (        
        [Parameter(Mandatory = $true, HelpMessage = "Path to installed location of the module")]
        [string] $InstalledLocation,

        [Parameter(Mandatory = $false, HelpMessage = "Repository Credential")]
        [PSCredential] $Credential = $null,

        [Parameter(Mandatory = $false, HelpMessage = "Repository name")]
        [string] $Repository = $null,

        [Parameter(Mandatory = $false, HelpMessage = "When is set import-module will be skipped.")]
        [switch] $SkipImport,

        [Parameter(Mandatory = $false, HelpMessage = "Use it when is required to use proxy while accessing the repository")]
        [Uri] $Proxy = $null,
    
        [Parameter(Mandatory = $false, HelpMessage = "Use it when is required to use proxy with credential")]
        [PSCredential] $ProxyCredential = $null
    
    )
    $ErrorActionPreference = 'Stop'    
    Write-Verbose '-- begin - Invoke-ResolveDependency --'
    Write-Verbose "InstalledLocation: $InstalledLocation"
    Write-Verbose "Proxy: $Proxy"
    if ($SkipImport.IsPresent) {
        Write-Verbose "SkipImport: True"
    }
    else {
        Write-Verbose "SkipImport: False"
    }

    $PSGalleryModules = @()
    $PSOriflameGalleryModules = @()
    [String[]] $Tags = @()

    $psdFiles = Get-ChildItem -Path $InstalledLocation -Filter *.psd1 -File
    foreach ($onePsdFile in $psdFiles) {
        $Manifest = Import-PowershellDataFile -LiteralPath $onePsdFile.FullName

        foreach ($onePrivateData in $Manifest.PrivateData) {
            foreach ($onePSData in $onePrivateData.PSData) {

                [Object[]] $ExternalModuleDependencies = $onePSData.ExternalModuleDependencies

                if (![string]::IsNullOrEmpty($onePSData.Tags)) {
                    [String[]] $Tags = $onePSData.Tags 
                }
                
                Write-Debug "Number of dependent modules:$($ExternalModuleDependencies.count)"
                if (![string]::IsNullOrEmpty($ExternalModuleDependencies)) {
                    foreach ($oneExternalModuleDependencies in $ExternalModuleDependencies) {

                        if (![string]::IsNullOrEmpty($oneExternalModuleDependencies)) {
                            [Hashtable] $PSGalleryInstallParam = Get-OriAzBopVersionParamHelper `
                                -Module $oneExternalModuleDependencies `
                                -Verbose:$VerbosePreference `
                                -Debug:$DebugPreference
         
                            Write-Debug "PSGAlleryInstallParam: $(ConvertTo-Json $PSGalleryInstallParam)"
                            Write-Debug "PSGAlleryInstallParam.Name: $($PSGalleryInstallParam.Name)"
                            Write-Debug "Tags: [$($Tags -join ',')]"
         
                            [Hashtable] $PSGalleryInstallParamBasedOnTags = Get-OriAzBopExternalDependenciesParamBasedOnTags `
                                -ModuleName $PSGAlleryInstallParam.Name `
                                -Tags $Tags `
                                -Verbose:$VerbosePreference `
                                -Debug:$DebugPreference
         
                            Write-Debug "Install prerequisity from PSGallery:"
                            Write-Debug (ConvertTo-Json -InputObject $PSGalleryInstallParamBasedOnTags)

                            Install-OriAzBopPrerequisite @PSGalleryInstallParamBasedOnTags `
                                -SkipImport:$SkipImport `
                                -AllowClobber:$True `
                                -SkipPublisherCheck:$True `
                                -Proxy $Proxy `
                                -ProxyCredential $ProxyCredential `
                                -Verbose:$VerbosePreference `
                                -Debug:$DebugPreference | Out-Null

                            $ModuleFilter = $PSGalleryInstallParamBasedOnTags.Clone()
                            $PSGalleryModules += @{
                                ModuleName   = $ModuleFilter.Name
                                ModuleFilter = $ModuleFilter
                            }

                        }
                    }
                }

            }
        }

        foreach ($oneModule in $Manifest.RequiredModules) {
            $InstallParamByRequiredModules = Get-OriAzBopVersionParamHelper -Module $oneModule `
                -Verbose:$VerbosePreference `
                -Debug:$DebugPreference

            Write-Debug "This Version is respecting only tags to determine the version of module"
            [HashTable] $InstallParam = Get-OriAzBopExternalDependenciesParamBasedOnTags `
                -ModuleName $InstallParamByRequiredModules.Name `
                -Tags $Tags `
                -TagPrefixTemplate 'InternalModuleDependencies_{0}_{1}' `
                -Verbose:$VerbosePreference `
                -Debug:$DebugPreference

            if (![string]::IsNullOrEmpty($Credential)) {
                $InstallParam += @{Credential = $Credential }
            }
            if (![string]::IsNullOrEmpty($Repository)) {
                $InstallParam += @{Repository = $Repository }
            }
                
            Write-Debug (ConvertTo-Json -InputObject $InstallParam)
            [PSCustomObject] $DependencyMap = Invoke-ModuleInstall @InstallParam `
                -SkipImport:$SkipImport `
                -Proxy $Proxy `
                -ProxyCredential $ProxyCredential `
                -Verbose:$VerbosePreference `
                -Debug:$DebugPreference

            $ModuleFilter = $InstallParam.Clone()
            if ($ModuleFilter.ContainsKey('Credential')) {
                $ModuleFilter.Remove('Credential')
            }

            $PSOriflameGalleryModules += @{
                ModuleName    = $ModuleFilter.Name
                ModuleFilter  = $ModuleFilter
                DependencyMap = $DependencyMap
            }
                    
        }  
    }

    [PSCustomObject] $ToReturn = @{
        PSGallery         = $PSGalleryModules
        PSOriflameGallery = $PSOriflameGalleryModules
    }
    Write-Verbose '-- end - Invoke-ResolveDependency --'
    return $ToReturn
}