Skip to content

Commit 3b1ada9

Browse files
committed
clean up bootstrap code so it's only in one place
1 parent 434281d commit 3b1ada9

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

azure-pipelines.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ steps:
1010
- task: gittools.gitversion.gitversion-task.GitVersion@3
1111
displayName: GitVersion
1212

13-
- powershell: .\bootstrap.ps1
14-
displayName: 'Restore pre-requisites'
13+
## As long as we're calling build.ps1, we don't need to bootstrap separately
14+
# - powershell: .\bootstrap.ps1
15+
# displayName: 'Restore pre-requisites'
1516

1617
- powershell: .\build.ps1 -OutputDirectory $(Build.ArtifactStagingDirectory)\$(Build.DefinitionName) -Version $(GitVersion.AssemblySemVer) -Verbose
1718
displayName: 'Run build script'

bootstrap.ps1

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
[CmdletBinding()]
22
param(
3-
[ValidateSet("CurrentUser", "AllUsers")]
4-
$Scope="CurrentUser"
3+
#
4+
[ValidateSet("CurrentUser", "AllUsers", "LocalTools")]
5+
$Scope = "CurrentUser"
56
)
67

7-
Push-Location $PSScriptRoot
8+
Push-Location $PSScriptRoot -StackName bootstrap-stack
89

9-
# # This really does work, but using PSDepends is a lot smoother when there's more modules, or nuget packages
10+
# # This would probably work, if the default gallery was trusted ...
1011
# Install-Module Pester -RequiredVersion 4.4.0 -Scope CurrentUser -Repository PSGallery -SkipPublisherCheck
1112
# Install-Module Configuration -RequiredVersion 1.3.1 -Scope CurrentUser -Repository PSGallery -SkipPublisherCheck
13+
if (!(Get-Module PSDepends -ListAvailable)) {
14+
$Policy = (Get-PSRepository PSGallery).InstallationPolicy
15+
try {
16+
Set-PSRepository PSGallery -InstallationPolicy Trusted
1217

13-
# Here's one example of why PowerShellGet is so awkward: their default gallery is not be trusted by default!
14-
$Policy = (Get-PSRepository PSGallery).InstallationPolicy
15-
Set-PSRepository PSGallery -InstallationPolicy Trusted
16-
try {
17-
Install-Module PSDepend -RequiredVersion 0.3.0 -Scope:$Scope -Repository PSGallery -ErrorAction Stop
18-
} finally {
19-
# Make sure we didn't change anything permanently
20-
Set-PSRepository PSGallery -InstallationPolicy:$Policy
18+
if ($Scope -eq "LocalTools") {
19+
$Target = Join-Path $PSScriptRoot "Tools"
20+
New-Item -Path $Target -ItemType Directory -Force
21+
22+
$Env:PSModulePath += ';' + $Target
23+
$PSDefaultParameterValues["Invoke-PSDepend:Target"] = $Target
24+
Save-Module PSDepend -Repository PSGallery -ErrorAction Stop -Path $Target
25+
} else {
26+
$PSDefaultParameterValues["Invoke-PSDepend:Target"] = $Scope
27+
Install-Module PSDepend -Repository PSGallery -ErrorAction Stop -Scope:$Scope
28+
}
29+
} finally {
30+
# Make sure we didn't change anything permanently
31+
Set-PSRepository PSGallery -InstallationPolicy:$Policy
32+
Pop-Location -StackName bootstrap-stack
33+
}
2134
}
22-
Import-Module PSDepend -RequiredVersion 0.3.0
35+
36+
try {
37+
Write-Verbose "Updating dependencies"
38+
Import-Module PSDepend -ErrorAction Stop
39+
Invoke-PSDepend -Import -Force -ErrorAction Stop
40+
} catch {
41+
Write-Warning "Unable to restore dependencies. Please review errors:"
42+
throw
43+
}

build.ps1

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ param(
88

99
$Repository = 'PSGallery',
1010

11-
[ValidateSet("User", "Machine", "Project")]
12-
$InstallToolScope = "User",
11+
[ValidateSet("CurrentUser", "AllUsers", "LocalTools")]
12+
$InstallToolScope = "CurrentUser",
1313

1414
[switch]$Test
1515
)
@@ -22,26 +22,7 @@ $null = $PSBoundParameters.Remove('InstallToolScope')
2222
$ErrorActionPreference = "Stop"
2323
Push-Location $PSScriptRoot -StackName BuildBuildModule
2424
try {
25-
26-
try {
27-
Write-Verbose "Updating dependencies"
28-
switch($InstallToolScope) {
29-
"Project" {
30-
$PSDefaultParameterValues["Invoke-PSDepend:Target"] = Join-Path $PSScriptRoot "Tools"
31-
}
32-
"Machine" {
33-
$PSDefaultParameterValues["Invoke-PSDepend:Target"] = Join-Path $PSScriptRoot "AllUsers"
34-
}
35-
default {
36-
$PSDefaultParameterValues["Invoke-PSDepend:Target"] = Join-Path $PSScriptRoot "CurrentUser"
37-
}
38-
}
39-
Invoke-PSDepend -Force -ErrorAction Stop
40-
Invoke-PSDepend -Import -Force -ErrorAction Stop
41-
} catch {
42-
Write-Warning "Unable to restore dependencies. Please review errors:"
43-
throw
44-
}
25+
& "$PSScriptRoot\bootstrap.ps1" -Scope $InstallToolScope
4526

4627
# Build ModuleBuilder with ModuleBuilder:
4728
Write-Verbose "Compiling ModuleBuilderBootstrap module"

0 commit comments

Comments
 (0)