PSDependScripts/PSGalleryModule.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
<#
    .SYNOPSIS
        Installs a module from a PowerShell repository like the PowerShell Gallery.

    .DESCRIPTION
        Installs a module from a PowerShell repository like the PowerShell Gallery.

        Relevant Dependency metadata:
            Name: The name for this module
            Version: Used to identify existing installs meeting this criteria, and as RequiredVersion for installation. Defaults to 'latest'
            Target: Used as 'Scope' for Install-Module. If this is a path, we use Save-Module with this path. Defaults to 'AllUsers'
            AddToPath: If target is used as a path, prepend that path to ENV:PSModulePath
            Credential: The username and password used to authenticate against the private repository

        If you don't have the Nuget package provider, we install it for you

    .PARAMETER Repository
        PSRepository to download from. Defaults to PSGallery

    .PARAMETER SkipPublisherCheck
        Bypass the catalog signing check. Defaults to $false

    .PARAMETER AllowClobber
        Allow installation of modules that clobber existing commands. Defaults to $True

    .PARAMETER AcceptLicense
        Accepts the license agreement during installation.
        
    .PARAMETER AllowPrerelease
        If specified, allow for prerelease.

        If specified along with version 'latest', a prerelease will be selected if it is the latest version

        Sorting assumes you name prereleases appropriately (i.e. alpha < beta < gamma)

    .PARAMETER Import
        If specified, import the module in the global scope

        Deprecated. Moving to PSDependAction

    .PARAMETER PSDependAction
        Test, Install, or Import the module. Defaults to Install

        Test: Return true or false on whether the dependency is in place
        Install: Install the dependency
        Import: Import the dependency

    .EXAMPLE
        @{
            BuildHelpers = 'latest'
            PSDeploy = ''
            InvokeBuild = '3.2.1'
        }

        # From the PSGallery repository (PowerShellGallery.com)...
            # Install the latest BuildHelpers and PSDeploy ('latest' and '' both evaluate to latest)
            # Install version 3.2.1

    .EXAMPLE
        @{
            BuildHelpers = @{
                Target = 'C:\Build'
            }
        }

        # Install the latest BuildHelpers module from PSGallery to C:\Build (i.e. C:\Build\BuildHelpers will be the module folder)
        # No version is specified - we assume latest in this case.

    .EXAMPLE
        @{
            BuildHelpers = @{
                Parameters @{
                    Repository = 'PSPrivateGallery'
                    SkipPublisherCheck = $true
                }
            }
        }

        # Install the latest BuildHelpers module from a custom gallery* that you registered, and bypass the catalog signing check
        # No version is specified - we assume latest in this case.

        # * Perhaps you use this https://github.com/PowerShell/PSPrivateGallery, or Artifactory, ProGet, etc.
    
    .EXAMPLE
        @{
            'vmware.powercli' = @{
                Parameters = @{
                    AllowPrerelease = $True
                }
            }
        }
        # Install the latest version of PowerCLI, allowing for prerelease
#>

[cmdletbinding()]
param(
    [PSTypeName('PSDepend.Dependency')]
    [psobject[]]$Dependency,

    [AllowNull()]
    [string]$Repository = 'PSGallery', # From Parameters...

    [bool]$SkipPublisherCheck, # From Parameters...

    [bool]$AllowClobber = $True,

    [bool]$AcceptLicense,

    [bool]$AllowPrerelease,

    [switch]$Import,

    [ValidateSet('Test', 'Install', 'Import')]
    [string[]]$PSDependAction = @('Install')
)

# Extract data from Dependency
    $DependencyName = $Dependency.DependencyName
    $Name = $Dependency.Name
    if(-not $Name)
    {
        $Name = $DependencyName
    }

    $Version = $Dependency.Version
    if(-not $Version)
    {
        $Version = 'latest'
    }

    # We use target as a proxy for Scope
    if(-not $Dependency.Target)
    {
        $Scope = 'AllUsers'
    }
    else
    {
        $Scope = $Dependency.Target
    }

    $Credential = $Dependency.Credential

    if('AllUsers', 'CurrentUser' -notcontains $Scope)
    {
        $command = 'save'
    }
    else
    {
        $command = 'install'
    }

if(-not (Get-PackageProvider -Name Nuget))
{
    # Grab nuget bits.
    $null = Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null
}

Write-Verbose -Message "Getting dependency [$name] from PowerShell repository [$Repository]"

# Validate that $target has been setup as a valid PowerShell repository,
# but allow to rely on all PS repos registered.
if($Repository) {
    $validRepo = Get-PSRepository -Name $Repository -Verbose:$false -ErrorAction SilentlyContinue
        if (-not $validRepo) {
            Write-Error "[$Repository] has not been setup as a valid PowerShell repository."
            return
        }
}

$params = @{
    Name               = $Name
    SkipPublisherCheck = $SkipPublisherCheck
    AllowClobber       = $AllowClobber
    Verbose            = $VerbosePreference
    Force              = $True
}

if($PSBoundParameters.ContainsKey('AllowPrerelease')){
    $params.Add('AllowPrerelease', $AllowPrerelease)
}

if($PSBoundParameters.ContainsKey('AcceptLicense')){
    $params.Add('AcceptLicense', $AcceptLicense)
}

if($Repository) {
    $params.Add('Repository',$Repository)
}

if($Version -and $Version -ne 'latest')
{
    $Params.add('RequiredVersion', $Version)
}

if($Credential)
{
    $Params.add('Credential', $Credential)
}

# This code works for both install and save scenarios.
if($command -eq 'Save')
{
    $ModuleName =  Join-Path $Scope $Name
    $Params.Remove('AllowClobber')
    $Params.Remove('SkipPublisherCheck')
}
elseif ($Command -eq 'Install')
{
    $ModuleName = $Name
}

# Only use "SkipPublisherCheck" (and other) parameter if "Install-Module" supports it
$availableParameters = (Get-Command "Install-Module").Parameters
$tempParams = $Params.Clone()
foreach($thisParameter in $Params.Keys)
{
    if(-Not ($availableParameters.ContainsKey($thisParameter)))
    {
        Write-Verbose -Message "Removing parameter [$thisParameter] from [Install-Module] as it is not available"
        $tempParams.Remove($thisParameter)
    }
}
$Params = $tempParams.Clone()

Add-ToPsModulePathIfRequired -Dependency $Dependency -Action $PSDependAction

$Existing = $null
$Existing = Get-Module -ListAvailable -Name $ModuleName -ErrorAction SilentlyContinue

if($Existing)
{
    Write-Verbose "Found existing module [$Name]"
    # Thanks to Brandon Padgett!
    $ExistingVersion = $Existing | Measure-Object -Property Version -Maximum | Select-Object -ExpandProperty Maximum
    $FindModuleParams = @{Name = $Name}
    if($Repository) {
        $FindModuleParams.Add('Repository', $Repository)
    }
    if($Credential)
    {
        $FindModuleParams.Add('Credential', $Credential)
    }
    if($AllowPrerelease)
    {
        $FindModuleParams.Add('AllowPrerelease', $AllowPrerelease)
    }

    # Version string, and equal to current
    if($Version -and $Version -ne 'latest' -and $Version -eq $ExistingVersion)
    {
        Write-Verbose "You have the requested version [$Version] of [$Name]"
        # Conditional import
        Import-PSDependModule -Name $ModuleName -Action $PSDependAction -Version $ExistingVersion

        if($PSDependAction -contains 'Test')
        {
            return $true
        }
        return $null
    }

    $GalleryVersion = Find-Module @FindModuleParams | Measure-Object -Property Version -Maximum | Select-Object -ExpandProperty Maximum
    [System.Version]$parsedVersion = $null
    [System.Management.Automation.SemanticVersion]$parsedSemanticVersion = $null
    [System.Management.Automation.SemanticVersion]$parsedTempSemanticVersion = $null
    $isGalleryVersionLessEquals = if (
        [System.Management.Automation.SemanticVersion]::TryParse($ExistingVersion, [ref]$parsedSemanticVersion) -and
        [System.Management.Automation.SemanticVersion]::TryParse($GalleryVersion, [ref]$parsedTempSemanticVersion)
    ) {
        $GalleryVersion -le $parsedSemanticVersion
    }
    elseif ([System.Version]::TryParse($ExistingVersion, [ref]$parsedVersion)) {
        $GalleryVersion -le $parsedVersion
    }

    # latest, and we have latest
    if( $Version -and ($Version -eq 'latest' -or $Version -eq '') -and $isGalleryVersionLessEquals)
    {
        Write-Verbose "You have the latest version of [$Name], with installed version [$ExistingVersion] and PSGallery version [$GalleryVersion]"
        # Conditional import
        Import-PSDependModule -Name $ModuleName -Action $PSDependAction -Version $ExistingVersion

        if($PSDependAction -contains 'Test')
        {
            return $True
        }
        return $null
    }
    Write-Verbose "Continuing to install [$Name]: Requested version [$version], existing version [$ExistingVersion]"
}

#No dependency found, return false if we're testing alone...
if( $PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1)
{
    return $False
}

if($PSDependAction -contains 'Install')
{
    if('AllUsers', 'CurrentUser' -contains $Scope)
    {
        Write-Verbose "Installing [$Name] with scope [$Scope]"
        Install-Module @params -Scope $Scope
    }
    else
    {
        Write-Verbose "Saving [$Name] with path [$Scope]"
        Write-Verbose "Creating directory path to [$Scope]"
        if(-not (Test-Path $Scope -ErrorAction SilentlyContinue))
        {
            $Null = New-Item -ItemType Directory -Path $Scope -Force -ErrorAction SilentlyContinue
        }
        Save-Module @params -Path $Scope
    }
}

# Conditional import
$importVs = $params['RequiredVersion']
Import-PSDependModule -Name $ModuleName -Action $PSDependAction -Version $importVs