1
1
$global :CurrentUserModulePath = " "
2
2
3
- function Update-PSModulePathForCI ()
4
- {
3
+ function Update-PSModulePathForCI () {
5
4
# Information on PSModulePath taken from docs
6
5
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath
7
6
@@ -11,7 +10,8 @@ function Update-PSModulePathForCI()
11
10
if ($IsWindows ) {
12
11
$hostedAgentModulePath = $env: SystemDrive + " \Modules"
13
12
$moduleSeperator = " ;"
14
- } else {
13
+ }
14
+ else {
15
15
$hostedAgentModulePath = " /usr/share"
16
16
$moduleSeperator = " :"
17
17
}
@@ -55,19 +55,30 @@ function Get-ModuleRepositories([string]$moduleName) {
55
55
56
56
$repoUrls = if ($packageFeedOverrides.Contains (" ${moduleName} " )) {
57
57
@ ($packageFeedOverrides [" ${moduleName} " ], $DefaultPSRepositoryUrl )
58
- } else {
58
+ }
59
+ else {
59
60
@ ($DefaultPSRepositoryUrl )
60
61
}
61
62
62
63
return $repoUrls
63
64
}
64
65
65
66
function moduleIsInstalled ([string ]$moduleName , [string ]$version ) {
66
- $modules = (Get-Module - ListAvailable $moduleName )
67
+ if (-not (Test-Path variable:script:InstalledModules)) {
68
+ $script :InstalledModules = @ {}
69
+ }
70
+
71
+ if ($script :InstalledModules.ContainsKey (" ${moduleName} " )) {
72
+ $modules = $script :InstalledModules [" ${moduleName} " ]
73
+ }
74
+ else {
75
+ $modules = (Get-Module - ListAvailable $moduleName )
76
+ $script :InstalledModules [" ${moduleName} " ] = $modules
77
+ }
78
+
67
79
if ($version -as [Version ]) {
68
80
$modules = $modules.Where ({ [Version ]$_.Version -ge [Version ]$version })
69
- if ($modules.Count -gt 0 )
70
- {
81
+ if ($modules.Count -gt 0 ) {
71
82
Write-Host " Using module $ ( $modules [0 ].Name) with version $ ( $modules [0 ].Version) ."
72
83
return $modules [0 ]
73
84
}
@@ -77,8 +88,7 @@ function moduleIsInstalled([string]$moduleName, [string]$version) {
77
88
78
89
function installModule ([string ]$moduleName , [string ]$version , $repoUrl ) {
79
90
$repo = (Get-PSRepository ).Where ({ $_.SourceLocation -eq $repoUrl })
80
- if ($repo.Count -eq 0 )
81
- {
91
+ if ($repo.Count -eq 0 ) {
82
92
Register-PSRepository - Name $repoUrl - SourceLocation $repoUrl - InstallationPolicy Trusted | Out-Null
83
93
$repo = (Get-PSRepository ).Where ({ $_.SourceLocation -eq $repoUrl })
84
94
if ($repo.Count -eq 0 ) {
@@ -102,17 +112,26 @@ function installModule([string]$moduleName, [string]$version, $repoUrl) {
102
112
throw " Failed to install module $moduleName with version $version "
103
113
}
104
114
115
+ $script :InstalledModules [" ${moduleName} " ] = $modules
116
+
105
117
# Unregister repository as it can cause overlap issues with `dotnet tool install`
106
118
# commands using the same devops feed
107
119
Unregister-PSRepository - Name $repoUrl | Out-Null
108
120
109
121
return $modules [0 ]
110
122
}
111
123
124
+ function InstallAndImport-ModuleIfNotInstalled ([string ]$module , [string ]$version ) {
125
+ if ($null -eq (moduleIsInstalled $module $version )) {
126
+ Install-ModuleIfNotInstalled - WhatIf:$false $module $version | Import-Module
127
+ } elseif (! (Get-Module - Name $module )) {
128
+ Import-Module $module
129
+ }
130
+ }
131
+
112
132
# Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1
113
133
# If we want to use another default repository other then PSGallery we can update the default parameters
114
- function Install-ModuleIfNotInstalled ()
115
- {
134
+ function Install-ModuleIfNotInstalled () {
116
135
[CmdletBinding (SupportsShouldProcess = $true )]
117
136
param (
118
137
[string ]$moduleName ,
@@ -137,12 +156,14 @@ function Install-ModuleIfNotInstalled()
137
156
foreach ($url in $repoUrls ) {
138
157
try {
139
158
$module = installModule - moduleName $moduleName - version $version - repoUrl $url
140
- } catch {
159
+ }
160
+ catch {
141
161
if ($url -ne $repoUrls [-1 ]) {
142
162
Write-Warning " Failed to install powershell module from '$url '. Retrying with fallback repository"
143
163
Write-Warning $_
144
164
continue
145
- } else {
165
+ }
166
+ else {
146
167
Write-Warning " Failed to install powershell module from $url "
147
168
throw
148
169
}
@@ -151,13 +172,14 @@ function Install-ModuleIfNotInstalled()
151
172
}
152
173
153
174
Write-Host " Using module '$ ( $module.Name ) ' with version '$ ( $module.Version ) '."
154
- } finally {
175
+ }
176
+ finally {
155
177
$mutex.ReleaseMutex ()
156
178
}
157
179
158
180
return $module
159
181
}
160
182
161
183
if ($null -ne $env: SYSTEM_TEAMPROJECTID ) {
162
- Update-PSModulePathForCI
184
+ Update-PSModulePathForCI
163
185
}
0 commit comments