Public/Invoke-PSDepend.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
Function Invoke-PSDepend {
    <#
    .SYNOPSIS
        Invoke PSDepend

    .DESCRIPTION
        Invoke PSDepend

        Searches for and runs *.depend.psd1 and requirements.psd1 files in the current and nested paths

        See Get-Help about_PSDepend for more information.

    .PARAMETER Path
        Path to a specific depend.psd1 file, or to a folder that we recursively search for *.depend.psd1 files

        Defaults to the current path

    .PARAMETER Recurse
        If path is a folder, whether to recursively search for *.depend.psd1 and requirements.psd1 files under that folder

        Defaults to $True

    .PARAMETER InputObject
        If specified instead of Path, treat this hashtable as the contents of a dependency file.

        For example:

            -InputObject @{
                BuildHelpers = 'latest'
                PSDeploy = 'latest'
                InvokeBuild = 'latest'
            }

    .PARAMETER Tags
        Only invoke dependencies that are tagged with all of the specified Tags (-and, not -or)

    .PARAMETER PSDependTypePath
        Specify a PSDependMap.psd1 file that maps DependencyTypes to their scripts.

        This defaults to the PSDependMap.psd1 in the PSDepend module folder

    .PARAMETER Force
        Force dependency, skipping prompts and confirmation

    .PARAMETER Test
        Run tests for dependencies we find.

        Appends a 'DependencyExists' property indicating whether the dependency exists by default
        Specify Quiet to simply return $true or $false depending on whether all dependencies exist

    .PARAMETER Quiet
        If specified along with Test, we return $true, or $false depending on whether all dependencies were met

    .PARAMETER Install
        Run the install for a dependency

        Default behavior

    .PARAMETER Target
        If specified, override the target in the PSDependOptions or Dependency.

    .PARAMETER Import
        If the dependency supports it, import it

    .PARAMETER Credentials
        Specifies a hashtable of PSCredentials to use for each dependency that is served from a private feed. The key of the hashtable must match the Credential property value in the dependency.

        For example:

        @{
            dependency_name = @{
                ...
                Credential = 'PrivatePackage'
                ...
            }
        }

        -Credentials @{
                PrivatePackage = $privateCredentials
                AnotherPrivatePackage = $morePrivateCredenials
        }

    .EXAMPLE
        Invoke-PSDepend

        # Search for and run *.deploy.psd1 and requirements.psd1 files under the current path

    .EXAMPLE
        Invoke-PSDepend -Path C:\Path\To\require.psd1

        # Install dependencies from require.psd1

    .EXAMPLE
        Invoke-PSDepend -Path C:\Requirements -Recurse $False

        # Find and run *.depend.psd1 and requirements.psd1 files under C\Requirements (but not subfolders)

    .LINK
        about_PSDepend

    .LINK
        about_PSDepend_Definitions

    .LINK
        Get-Dependency

    .LINK
        Install-Dependency

    .LINK
        https://github.com/RamblingCookieMonster/PSDepend
    #>

    [cmdletbinding( DefaultParameterSetName = 'installimport-file',
                    SupportsShouldProcess = $True,
                    ConfirmImpact='High' )]
    Param(
        [validatescript({Test-Path -Path $_ -ErrorAction Stop})]
        [parameter( ParameterSetName = 'installimport-file',
                    Position = 0,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True)]
        [parameter( ParameterSetName = 'test-file',
                    Position = 0,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True)]
        [string[]]$Path = '.',

        [parameter( ParameterSetName = 'installimport-hashtable',
                    Position = 0,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True)]
        [parameter( ParameterSetName = 'test-hashtable',
                    Position = 0,
                    ValueFromPipeline = $True,
                    ValueFromPipelineByPropertyName = $True)]
        [hashtable[]]$InputObject,

        [validatescript({Test-Path -Path $_ -PathType Leaf -ErrorAction Stop})]
        [string]$PSDependTypePath = $(Join-Path $ModuleRoot PSDependMap.psd1),

        [string[]]$Tags,

        [parameter(ParameterSetName = 'installimport-file')]
        [parameter(ParameterSetName = 'test-file')]
        [bool]$Recurse = $True,

        [parameter(ParameterSetName = 'test-file')]
        [parameter(ParameterSetName = 'test-hashtable')]
        [switch]$Test,

        [parameter(ParameterSetName = 'test-file')]
        [parameter(ParameterSetName = 'test-hashtable')]
        [switch]$Quiet,

        [parameter(ParameterSetName = 'installimport-file')]
        [parameter(ParameterSetName = 'installimport-hashtable')]
        [switch]$Import,

        [parameter(ParameterSetName = 'installimport-file')]
        [parameter(ParameterSetName = 'installimport-hashtable')]
        [switch]$Install,

        [switch]$Force,

        [String]$Target,

        [parameter(ParameterSetName = 'installimport-file')]
        [parameter(ParameterSetName = 'installimport-hashtable')]
        [hashtable]$Credentials
    )
    Begin
    {
        # Build parameters
        $InvokeParams = @{
            PSDependAction = @()
            PSDependTypePath = $PSDependTypePath
        }
        $DoInstall = $PSCmdlet.ParameterSetName -like 'installimport-*' -and $Install
        $DoImport = $PSCmdlet.ParameterSetName -like 'installimport-*' -and $Import
        $DoTest = $PSCmdlet.ParameterSetName -like 'test-*' -and $Test
        if($DoInstall){$InvokeParams.PSDependAction += 'Install'}
        if($DoImport){$InvokeParams.PSDependAction += 'Import'}
        if($DoTest){$InvokeParams.PSDependAction += 'Test'}
        if($InvokeParams.PSDependAction.count -like 0)
        {
            $InvokeParams.PSDependAction += 'Install'
        }
        Write-Verbose "Running Invoke-PSDepend with ParameterSetName '$($PSCmdlet.ParameterSetName)', PSDependAction $($InvokeParams.PSDependAction), and params: $($PSBoundParameters | Out-String)"

        $DependencyFiles = New-Object System.Collections.ArrayList
        $PSDependTypes = Get-PSDependType -Path $PSDependTypePath -SkipHelp
    }
    Process
    {
        $GetPSDependParams = @{}

        if($PSCmdlet.ParameterSetName -like '*-file')
        {
            foreach( $PathItem in $Path )
            {
                # Create a map for dependencies
                [void]$DependencyFiles.AddRange( @( Resolve-DependScripts -Path $PathItem -Recurse $Recurse ) )
                if ($DependencyFiles.count -gt 0)
                {
                    Write-Verbose "Working with [$($DependencyFiles.Count)] dependency files from [$PathItem]:`n$($DependencyFiles | Out-String)"
                }
                else
                {
                    Write-Warning "No *.depend.ps1 files found under [$PathItem]"
                }
            }
            $GetPSDependParams.add('Path',$DependencyFiles)
        }
        elseif($PSCmdlet.ParameterSetName -like '*-hashtable')
        {
            $GetPSDependParams.add('InputObject',$InputObject)
        }

        # Parse
        if($PSBoundParameters.ContainsKey('Tags'))
        {
            $GetPSDependParams.Add('Tags',$Tags)
        }

        if ($null -ne $Credentials) {
            $GetPSDependParams.Add('Credentials', $Credentials)
        }

        # Handle Dependencies
        $Dependencies = Get-Dependency @GetPSDependParams
        $Unsupported = ( $PSDependTypes | Where-Object {-not $_.Supported} ).DependencyType
        $Dependencies = foreach($Dependency in $Dependencies)
        {
            if($Unsupported -contains $Dependency.DependencyType)
            {
                $Supports = $PSDependTypes | Where-Object {$_.DependencyType -eq $Dependency.DependencyType} | Select -ExpandProperty Supports
                Write-Warning "Skipping unsupported dependency:`n$( $Dependency | Out-String)`nSupported platforms:`n$($Supports | Out-String)"
            }
            else
            {
                $Dependency
            }
        }

        if($DoTest -and $Quiet)
        {
            $TestResult = [System.Collections.ArrayList]@()
        }

        #TODO: Add ShouldProcess here if install is specified...
        foreach($Dependency in $Dependencies)
        {
            if($PSBoundParameters.ContainsKey('Target'))
            {
                Write-Verbose "Overriding Dependency target [$($Dependency.Target)] with target parameter value [$Target]"
                $Dependency.Target = $Target
            }

            if( ($Force -and -not $WhatIf) -or
                ($DoTest) -or
                $PSCmdlet.ShouldProcess( "Processed the dependency '$($Dependency.DependencyName -join ", ")'",
                                        "Process the dependency '$($Dependency.DependencyName -join ", ")'?",
                                        "Processing dependency" ))
            {
                $PreScriptSuccess = $True #anti pattern! Best I could come up with to handle both prescript fail and dependencies
                if($DoInstall -and $Dependency.PreScripts.Count -gt 0)
                {
                    $ExistingEA = $ErrorActionPreference
                    $ErrorActionPreference = 'Stop'
                    foreach($script in $Dependency.PreScripts)
                    {
                        Try
                        {
                            Write-Verbose "Invoking pre script: [$script]"
                            . $script
                        }
                        Catch
                        {
                            $PreScriptSuccess = $False
                            "Skipping installation due to failed pre script: [$script]"
                            Write-Error $_
                        }
                    }
                    $ErrorActionPreference = $ExistingEA
                }

                if($PreScriptSuccess)
                {
                    if($DoTest -and $Quiet)
                    {
                        $null = $TestResult.Add( (Invoke-DependencyScript @InvokeParams -Dependency $Dependency -Quiet ) )
                    }
                    else
                    {
                        Invoke-DependencyScript @InvokeParams -Dependency $Dependency
                    }
                }

                if($DoInstall -and $Dependency.PostScripts.Count -gt 0)
                {
                    foreach($script in $Dependency.PostScripts)
                    {
                        Write-Verbose "Invoking post script: $($script)"
                        . $script
                    }
                }
            }
        }
        if($DoTest -and $Quiet)
        {
            if($TestResult -contains $false)
            {
                $false
            }
            else
            {
                $true
            }
        }
    }
}