@@ -47,6 +47,7 @@ function Update-PSModulePathForCI()
47
47
}
48
48
}
49
49
50
+ # Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1
50
51
# If we want to use another default repository other then PSGallery we can update the default parameters
51
52
function Install-ModuleIfNotInstalled ()
52
53
{
@@ -65,35 +66,53 @@ function Install-ModuleIfNotInstalled()
65
66
66
67
if ($modules.Count -eq 0 )
67
68
{
68
- $repositories = (Get-PSRepository ).Where ({ $_.SourceLocation -eq $repositoryUrl })
69
- if ($repositories.Count -eq 0 )
70
- {
71
- Register-PSRepository - Name $repositoryUrl - SourceLocation $repositoryUrl - InstallationPolicy Trusted
72
- $repositories = (Get-PSRepository ).Where ({ $_.SourceLocation -eq $repositoryUrl })
73
- if ($repositories.Count -eq 0 ) {
74
- Write-Error " Failed to registory package repository $repositoryUrl ."
75
- return
69
+ # Use double-checked locking to avoid locking when module is already installed
70
+ $mutex = New-Object System.Threading.Mutex($false , " Install-ModuleIfNotInstalled" )
71
+ $null = $mutex.WaitOne ()
72
+
73
+ try {
74
+ # Check installed modules again after acquiring lock
75
+ $modules = (Get-Module - ListAvailable $moduleName )
76
+ if ($version -as [Version ]) {
77
+ $modules = $modules.Where ({ [Version ]$_.Version -ge [Version ]$version })
76
78
}
77
- }
78
- $repository = $repositories [0 ]
79
-
80
- if ($repository.InstallationPolicy -ne " Trusted" ) {
81
- Set-PSRepository - Name $repository.Name - InstallationPolicy " Trusted"
82
- }
83
79
84
- Write-Host " Installing module $moduleName with min version $version from $repositoryUrl "
85
- # Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching
86
- Install-Module $moduleName - MinimumVersion $version - Repository $repository.Name - Scope CurrentUser - Force
87
-
88
- # Ensure module installed
89
- $modules = (Get-Module - ListAvailable $moduleName )
90
- if ($version -as [Version ]) {
91
- $modules = $modules.Where ({ [Version ]$_.Version -ge [Version ]$version })
80
+ if ($modules.Count -eq 0 )
81
+ {
82
+ $repositories = (Get-PSRepository ).Where ({ $_.SourceLocation -eq $repositoryUrl })
83
+ if ($repositories.Count -eq 0 )
84
+ {
85
+ Register-PSRepository - Name $repositoryUrl - SourceLocation $repositoryUrl - InstallationPolicy Trusted
86
+ $repositories = (Get-PSRepository ).Where ({ $_.SourceLocation -eq $repositoryUrl })
87
+ if ($repositories.Count -eq 0 ) {
88
+ Write-Error " Failed to register package repository $repositoryUrl ."
89
+ return
90
+ }
91
+ }
92
+ $repository = $repositories [0 ]
93
+
94
+ if ($repository.InstallationPolicy -ne " Trusted" ) {
95
+ Set-PSRepository - Name $repository.Name - InstallationPolicy " Trusted"
96
+ }
97
+
98
+ Write-Host " Installing module $moduleName with min version $version from $repositoryUrl "
99
+ # Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching
100
+ Install-Module $moduleName - MinimumVersion $version - Repository $repository.Name - Scope CurrentUser - Force
101
+
102
+ # Ensure module installed
103
+ $modules = (Get-Module - ListAvailable $moduleName )
104
+ if ($version -as [Version ]) {
105
+ $modules = $modules.Where ({ [Version ]$_.Version -ge [Version ]$version })
106
+ }
107
+
108
+ if ($modules.Count -eq 0 ) {
109
+ Write-Error " Failed to install module $moduleName with version $version "
110
+ return
111
+ }
112
+ }
92
113
}
93
-
94
- if ($modules.Count -eq 0 ) {
95
- Write-Error " Failed to install module $moduleName with version $version "
96
- return
114
+ finally {
115
+ $mutex.ReleaseMutex ()
97
116
}
98
117
}
99
118
0 commit comments