|
1 | | -$DefaultPSRepositoryUrl = "https://www.powershellgallery.com/api/v2" |
2 | 1 | $global:CurrentUserModulePath = "" |
3 | 2 |
|
4 | 3 | function Update-PSModulePathForCI() |
@@ -47,83 +46,112 @@ function Update-PSModulePathForCI() |
47 | 46 | } |
48 | 47 | } |
49 | 48 |
|
50 | | -# Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1 |
51 | | -# If we want to use another default repository other then PSGallery we can update the default parameters |
52 | | -function Install-ModuleIfNotInstalled() |
53 | | -{ |
54 | | - [CmdletBinding(SupportsShouldProcess = $true)] |
55 | | - param( |
56 | | - [string]$moduleName, |
57 | | - [string]$version, |
58 | | - [string]$repositoryUrl |
59 | | - ) |
60 | | - |
| 49 | +function Get-ModuleRepositories([string]$moduleName) { |
| 50 | + $DefaultPSRepositoryUrl = "https://www.powershellgallery.com/api/v2" |
61 | 51 | # List of modules+versions we want to replace with internal feed sources for reliability, security, etc. |
62 | 52 | $packageFeedOverrides = @{ |
63 | 53 | 'powershell-yaml' = 'https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-tools/nuget/v2' |
64 | 54 | } |
65 | 55 |
|
66 | | - try { |
67 | | - $mutex = New-Object System.Threading.Mutex($false, "Install-ModuleIfNotInstalled") |
68 | | - $null = $mutex.WaitOne() |
| 56 | + $repoUrls = if ($packageFeedOverrides.Contains("${moduleName}")) { |
| 57 | + @($packageFeedOverrides["${moduleName}"], $DefaultPSRepositoryUrl) |
| 58 | + } else { |
| 59 | + @($DefaultPSRepositoryUrl) |
| 60 | + } |
69 | 61 |
|
70 | | - # Check installed modules again after acquiring lock |
71 | | - $modules = (Get-Module -ListAvailable $moduleName) |
72 | | - if ($version -as [Version]) { |
73 | | - $modules = $modules.Where({ [Version]$_.Version -ge [Version]$version }) |
74 | | - } |
| 62 | + return $repoUrls |
| 63 | +} |
75 | 64 |
|
| 65 | +function moduleIsInstalled([string]$moduleName, [string]$version) { |
| 66 | + $modules = (Get-Module -ListAvailable $moduleName) |
| 67 | + if ($version -as [Version]) { |
| 68 | + $modules = $modules.Where({ [Version]$_.Version -ge [Version]$version }) |
76 | 69 | if ($modules.Count -gt 0) |
77 | 70 | { |
78 | 71 | Write-Host "Using module $($modules[0].Name) with version $($modules[0].Version)." |
79 | 72 | return $modules[0] |
80 | 73 | } |
| 74 | + } |
| 75 | + return $null |
| 76 | +} |
81 | 77 |
|
82 | | - $repositoryUrl = if ($repositoryUrl) { |
83 | | - $repositoryUrl |
84 | | - } elseif ($mirrorFeedOverrides.Contains("${moduleName}")) { |
85 | | - $mirrorFeedOverrides["${moduleName}"] |
86 | | - } else { |
87 | | - $DefaultPSRepositoryUrl |
| 78 | +function installModule([string]$moduleName, [string]$version, $repoUrl) { |
| 79 | + $repo = (Get-PSRepository).Where({ $_.SourceLocation -eq $repoUrl }) |
| 80 | + if ($repo.Count -eq 0) |
| 81 | + { |
| 82 | + Register-PSRepository -Name $repoUrl -SourceLocation $repoUrl -InstallationPolicy Trusted |
| 83 | + $repo = (Get-PSRepository).Where({ $_.SourceLocation -eq $repoUrl }) |
| 84 | + if ($repo.Count -eq 0) { |
| 85 | + throw "Failed to register package repository $repoUrl." |
88 | 86 | } |
| 87 | + } |
89 | 88 |
|
90 | | - $repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl }) |
91 | | - if ($repositories.Count -eq 0) |
92 | | - { |
93 | | - Register-PSRepository -Name $repositoryUrl -SourceLocation $repositoryUrl -InstallationPolicy Trusted |
94 | | - $repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl }) |
95 | | - if ($repositories.Count -eq 0) { |
96 | | - Write-Error "Failed to register package repository $repositoryUrl." |
97 | | - return |
98 | | - } |
99 | | - } |
100 | | - $repository = $repositories[0] |
| 89 | + if ($repo.InstallationPolicy -ne "Trusted") { |
| 90 | + Set-PSRepository -Name $repo.Name -InstallationPolicy "Trusted" |
| 91 | + } |
101 | 92 |
|
102 | | - if ($repository.InstallationPolicy -ne "Trusted") { |
103 | | - Set-PSRepository -Name $repository.Name -InstallationPolicy "Trusted" |
104 | | - } |
| 93 | + Write-Host "Installing module $moduleName with min version $version from $repoUrl" |
| 94 | + # Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching |
| 95 | + Install-Module $moduleName -MinimumVersion $version -Repository $repo.Name -Scope CurrentUser -Force |
| 96 | + # Ensure module installed |
| 97 | + $modules = (Get-Module -ListAvailable $moduleName) |
| 98 | + if ($version -as [Version]) { |
| 99 | + $modules = $modules.Where({ [Version]$_.Version -ge [Version]$version }) |
| 100 | + } |
| 101 | + if ($modules.Count -eq 0) { |
| 102 | + throw "Failed to install module $moduleName with version $version" |
| 103 | + } |
105 | 104 |
|
106 | | - Write-Host "Installing module $moduleName with min version $version from $repositoryUrl" |
107 | | - # Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching |
108 | | - Install-Module $moduleName -MinimumVersion $version -Repository $repository.Name -Scope CurrentUser -Force |
| 105 | + return $modules[0] |
| 106 | +} |
109 | 107 |
|
110 | | - # Ensure module installed |
111 | | - $modules = (Get-Module -ListAvailable $moduleName) |
112 | | - if ($version -as [Version]) { |
113 | | - $modules = $modules.Where({ [Version]$_.Version -ge [Version]$version }) |
114 | | - } |
| 108 | +# Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1 |
| 109 | +# If we want to use another default repository other then PSGallery we can update the default parameters |
| 110 | +function Install-ModuleIfNotInstalled() |
| 111 | +{ |
| 112 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 113 | + param( |
| 114 | + [string]$moduleName, |
| 115 | + [string]$version, |
| 116 | + [string]$repositoryUrl |
| 117 | + ) |
| 118 | + |
| 119 | + # Check installed modules before after acquiring lock to avoid a big queue |
| 120 | + $module = moduleIsInstalled -moduleName $moduleName -version $version |
| 121 | + if ($module) { return $module } |
115 | 122 |
|
116 | | - if ($modules.Count -eq 0) { |
117 | | - Write-Error "Failed to install module $moduleName with version $version" |
118 | | - return |
| 123 | + try { |
| 124 | + $mutex = New-Object System.Threading.Mutex($false, "Install-ModuleIfNotInstalled") |
| 125 | + $null = $mutex.WaitOne() |
| 126 | + |
| 127 | + # Check installed modules again after acquiring lock, in case it has been installed |
| 128 | + $module = moduleIsInstalled -moduleName $moduleName -version $version |
| 129 | + if ($module) { return $module } |
| 130 | + |
| 131 | + $repoUrls = Get-ModuleRepositories $moduleName |
| 132 | + |
| 133 | + foreach ($url in $repoUrls) { |
| 134 | + try { |
| 135 | + $module = installModule -moduleName $moduleName -version $version -repoUrl $url |
| 136 | + } catch { |
| 137 | + if ($url -ne $repoUrls[-1]) { |
| 138 | + Write-Warning "Failed to install powershell module from '$url'. Retrying with fallback repository" |
| 139 | + Write-Warning $_ |
| 140 | + continue |
| 141 | + } else { |
| 142 | + Write-Warning "Failed to install powershell module from $url" |
| 143 | + throw |
| 144 | + } |
| 145 | + } |
| 146 | + break |
119 | 147 | } |
120 | 148 |
|
121 | | - Write-Host "Using module $($modules[0].Name) with version $($modules[0].Version)." |
| 149 | + Write-Host "Using module '$($module.Name)' with version '$($module.Version)'." |
122 | 150 | } finally { |
123 | 151 | $mutex.ReleaseMutex() |
124 | 152 | } |
125 | 153 |
|
126 | | - return $modules[0] |
| 154 | + return $module |
127 | 155 | } |
128 | 156 |
|
129 | 157 | if ($null -ne $env:SYSTEM_TEAMPROJECTID) { |
|
0 commit comments