-
Notifications
You must be signed in to change notification settings - Fork 1
/
Resolve-Dependency.ps1
289 lines (256 loc) · 10.4 KB
/
Resolve-Dependency.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
[CmdletBinding()]
param
(
[Parameter()]
[String]
$DependencyFile = 'RequiredModules.psd1',
[Parameter()]
[String]
# Path for PSDepend to be bootstrapped and save other dependencies.
# Can also be CurrentUser or AllUsers if you wish to install the modules in such scope
# Default to $PWD.Path/output/modules
$PSDependTarget = (Join-Path $PSScriptRoot './output/RequiredModules'),
[Parameter()]
[uri]
# URI to use for Proxy when attempting to Bootstrap PackageProvider & PowerShellGet
$Proxy,
[Parameter()]
# Credential to contact the Proxy when provided
[PSCredential]$ProxyCredential,
[Parameter()]
[ValidateSet('CurrentUser', 'AllUsers')]
[String]
# Scope to bootstrap the PackageProvider and PSGet if not available
$Scope = 'CurrentUser',
[Parameter()]
[String]
# Gallery to use when bootstrapping PackageProvider, PSGet and when calling PSDepend (can be overridden in Dependency files)
$Gallery = 'PSGallery',
[Parameter()]
[PSCredential]
# Credentials to use with the Gallery specified above
$GalleryCredential,
[Parameter()]
[switch]
# Allow you to use a locally installed version of PowerShellGet older than 1.6.0 (not recommended, default to $False)
$AllowOldPowerShellGetModule,
[Parameter()]
[String]
# Allow you to specify a minimum version fo PSDepend, if you're after specific features.
$MinimumPSDependVersion,
[Parameter()]
[Switch]
$AllowPrerelease,
[Parameter()]
[Switch]
$WithYAML
)
# Load Defaults for parameters values from Resolve-Dependency.psd1 if not provided as parameter
try
{
Write-Verbose -Message "Importing Bootstrap default parameters from '$PSScriptRoot/Resolve-Dependency.psd1'."
$ResolveDependencyDefaults = Import-PowerShellDataFile -Path (Join-Path $PSScriptRoot '.\Resolve-Dependency.psd1' -Resolve -ErrorAction Stop)
$ParameterToDefault = $MyInvocation.MyCommand.ParameterSets.Where{ $_.Name -eq $PSCmdlet.ParameterSetName }.Parameters.Keys
if ($ParameterToDefault.Count -eq 0)
{
$ParameterToDefault = $MyInvocation.MyCommand.Parameters.Keys
}
# Set the parameters available in the Parameter Set, or it's not possible to choose yet, so all parameters are an option
foreach ($ParamName in $ParameterToDefault)
{
if (-Not $PSBoundParameters.Keys.Contains($ParamName) -and $ResolveDependencyDefaults.ContainsKey($ParamName))
{
Write-Verbose -Message "Setting $ParamName with $($ResolveDependencyDefaults[$ParamName])"
try
{
$variableValue = $ResolveDependencyDefaults[$ParamName]
if ($variableValue -is [string])
{
$variableValue = $ExecutionContext.InvokeCommand.ExpandString($variableValue)
}
$PSBoundParameters.Add($ParamName, $variableValue)
Set-Variable -Name $ParamName -value $variableValue -Force -ErrorAction SilentlyContinue
}
catch
{
Write-Verbose -Message "Error adding default for $ParamName : $($_.Exception.Message)"
}
}
}
}
catch
{
Write-Warning -Message "Error attempting to import Bootstrap's default parameters from $(Join-Path $PSScriptRoot '.\Resolve-Dependency.psd1'): $($_.Exception.Message)."
}
Write-Progress -Activity "Bootstrap:" -PercentComplete 0 -CurrentOperation "NuGet Bootstrap"
if (!(Get-PackageProvider -Name NuGet -ForceBootstrap -ErrorAction SilentlyContinue))
{
$providerBootstrapParams = @{
Name = 'nuget'
force = $true
ForceBootstrap = $true
ErrorAction = 'Stop'
}
switch ($PSBoundParameters.Keys)
{
'Proxy'
{
$providerBootstrapParams.Add('Proxy', $Proxy)
}
'ProxyCredential'
{
$providerBootstrapParams.Add('ProxyCredential', $ProxyCredential)
}
'Scope'
{
$providerBootstrapParams.Add('Scope', $Scope)
}
}
if ($AllowPrerelease)
{
$providerBootstrapParams.Add('AllowPrerelease', $true)
}
Write-Information "Bootstrap: Installing NuGet Package Provider from the web (Make sure Microsoft addresses/ranges are allowed)"
$null = Install-PackageProvider @providerBootstrapParams
$latestNuGetVersion = (Get-PackageProvider -Name NuGet -ListAvailable | Select-Object -First 1).Version.ToString()
Write-Information "Bootstrap: Importing NuGet Package Provider version $latestNuGetVersion to current session."
$Null = Import-PackageProvider -Name NuGet -RequiredVersion $latestNuGetVersion -Force
}
Write-Progress -Activity "Bootstrap:" -PercentComplete 10 -CurrentOperation "Ensuring Gallery $Gallery is trusted"
# Fail if the given PSGallery is not Registered
$Policy = (Get-PSRepository $Gallery -ErrorAction Stop).InstallationPolicy
Set-PSRepository -Name $Gallery -InstallationPolicy Trusted -ErrorAction Ignore
try
{
Write-Progress -Activity "Bootstrap:" -PercentComplete 25 -CurrentOperation "Checking PowerShellGet"
# Ensure the module is loaded and retrieve the version you have
$PowerShellGetVersion = (Import-Module PowerShellGet -PassThru -ErrorAction SilentlyContinue).Version
Write-Verbose "Bootstrap: The PowerShellGet version is $PowerShellGetVersion"
# Versions below 1.6.0 are considered old, unreliable & not recommended
if (!$PowerShellGetVersion -or ($PowerShellGetVersion -lt [System.version]'1.6.0' -and !$AllowOldPowerShellGetModule))
{
Write-Progress -Activity "Bootstrap:" -PercentComplete 40 -CurrentOperation "Installing newer version of PowerShellGet"
$InstallPSGetParam = @{
Name = 'PowerShellGet'
Force = $True
SkipPublisherCheck = $true
AllowClobber = $true
Scope = $Scope
Repository = $Gallery
}
switch ($PSBoundParameters.Keys)
{
'Proxy'
{
$InstallPSGetParam.Add('Proxy', $Proxy)
}
'ProxyCredential'
{
$InstallPSGetParam.Add('ProxyCredential', $ProxyCredential)
}
'GalleryCredential'
{
$InstallPSGetParam.Add('Credential', $GalleryCredential)
}
}
Install-Module @InstallPSGetParam
Remove-Module PowerShellGet -force -ErrorAction SilentlyContinue
Import-Module PowerShellGet -Force
$NewLoadedVersion = (Get-Module PowerShellGet).Version.ToString()
Write-Information "Bootstrap: PowerShellGet version loaded is $NewLoadedVersion"
Write-Progress -Activity "Bootstrap:" -PercentComplete 60 -CurrentOperation "Installing newer version of PowerShellGet"
}
# Try to import the PSDepend module from the available modules
try
{
$ImportPSDependParam = @{
Name = 'PSDepend'
ErrorAction = 'Stop'
Force = $true
}
if ($MinimumPSDependVersion)
{
$ImportPSDependParam.add('MinimumVersion', $MinimumPSDependVersion)
}
$null = Import-Module @ImportPSDependParam
}
catch
{
# PSDepend module not found, installing or saving it
if ($PSDependTarget -in 'CurrentUser', 'AllUsers')
{
Write-Debug "PSDepend module not found. Attempting to install from Gallery $Gallery"
Write-Warning "Installing PSDepend in $PSDependTarget Scope"
$InstallPSDependParam = @{
Name = 'PSDepend'
Repository = $Gallery
Force = $true
Scope = $PSDependTarget
SkipPublisherCheck = $true
AllowClobber = $true
}
if ($MinimumPSDependVersion)
{
$InstallPSDependParam.add('MinimumVersion', $MinimumPSDependVersion)
}
Write-Progress -Activity "Bootstrap:" -PercentComplete 75 -CurrentOperation "Installing PSDepend from $Gallery"
Install-Module @InstallPSDependParam
}
else
{
Write-Debug "PSDepend module not found. Attempting to Save from Gallery $Gallery to $PSDependTarget"
$SaveModuleParam = @{
Name = 'PSDepend'
Repository = $Gallery
Path = $PSDependTarget
}
if ($MinimumPSDependVersion)
{
$SaveModuleParam.add('MinimumVersion', $MinimumPSDependVersion)
}
Write-Progress -Activity "Bootstrap:" -PercentComplete 75 -CurrentOperation "Saving & Importing PSDepend from $Gallery to $Scope"
Save-Module @SaveModuleParam
}
}
finally
{
Write-Progress -Activity "Bootstrap:" -PercentComplete 100 -CurrentOperation "Loading PSDepend"
# We should have successfully bootstrapped PSDepend. Fail if not available
Import-Module PSDepend -ErrorAction Stop
}
if ($WithYAML)
{
if (-Not (Get-Module -ListAvailable -Name 'PowerShell-Yaml'))
{
Write-Verbose "PowerShell-Yaml module not found. Attempting to Save from Gallery $Gallery to $PSDependTarget"
$SaveModuleParam = @{
Name = 'PowerShell-Yaml'
Repository = $Gallery
Path = $PSDependTarget
}
Save-Module @SaveModuleParam
Import-Module "PowerShell-Yaml" -ErrorAction Stop
}
else
{
Write-Verbose "PowerShell-Yaml is already available"
}
}
Write-Progress -Activity "PSDepend:" -PercentComplete 0 -CurrentOperation "Restoring Build Dependencies"
if (Test-Path $DependencyFile)
{
$PSDependParams = @{
Force = $true
Path = $DependencyFile
}
# TODO: Handle when the Dependency file is in YAML, and -WithYAML is specified
Invoke-PSDepend @PSDependParams
}
Write-Progress -Activity "PSDepend:" -PercentComplete 100 -CurrentOperation "Dependencies restored" -Completed
}
finally
{
# Reverting the Installation Policy for the given gallery
Set-PSRepository -Name $Gallery -InstallationPolicy $Policy
Write-Verbose "Project Bootstrapped, returning to Invoke-Build"
}