Private/Invoke-ResolveDependency.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.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.
#>

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
    
    )
    $ErrorActionPreference = 'Stop'    
    Write-Verbose '-- begin - Invoke-ResolveDependency --'
    Write-Verbose "InstalledLocation: $InstalledLocation"
    if ($SkipImport.IsPresent) {
        Write-Verbose "SkipImport: True"
    }
    else {
        Write-Verbose "SkipImport: False"
    }

    $PSGalleryModules = @()
    $PSOriflameGalleryModules = @()

    $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)) {
                    [Object[]] $Tags = @()
                }
                else {
                    [Object[]] $Tags = $onePSData.Tags 
                }
                
                Write-Verbose "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-Verbose "PSGAlleryInstallParam: $(ConvertTo-Json $PSGalleryInstallParam)"
                            Write-Verbose "PSGAlleryInstallParam.Name: $($PSGalleryInstallParam.Name)"
                            Write-Verbose "Tags: [$($Tags -join ',')]"
         
                            [Hashtable] $PSGalleryInstallParamBasedOnTags = Get-OriAzBopExternalDependenciesParamBasedOnTags `
                                -ModuleName $PSGAlleryInstallParam.Name `
                                -Tags $Tags `
                                -Verbose:$VerbosePreference `
                                -Debug:$DebugPreference
         
                            Write-Verbose "Install prerequisity from PSGallery:"
                            Write-Debug (ConvertTo-Json -InputObject $PSGalleryInstallParamBasedOnTags)
                            Install-OriAzBopPrerequisity @PSGalleryInstallParamBasedOnTags `
                                -SkipImport:$SkipImport `
                                -AllowClobber:$True `
                                -SkipPublisherCheck:$True `
                                -Verbose:$VerbosePreference `
                                -Debug:$DebugPreference | Out-Null

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

                        }
                    }
                }

            }
        }

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

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

            $ModuleFilter = $InstallParam.Clone()
            if ($ModuleFilter.ContainsKey('Credential')) {
                $ModuleFilter.Remove('Credential')           
            }            
            $PSOriflameGalleryModules += @{
                ModuleName    = $ModuleFilter.Name
                ModuleFilter  = $ModuleFilter
                DependencyMap = $DependencyMap
            }
                    
        }  
    }
    Write-Verbose '-- end - Invoke-ResolveDependency --'

    [PSCustomObject] $ToReturn = @{
        PSGallery         = $PSGalleryModules
        PSOriflameGallery = $PSOriflameGalleryModules
    }
    return $ToReturn
}