From a7528cdd258142129c58eb7965c2badafaf3d39a Mon Sep 17 00:00:00 2001 From: Wojciech Sciesinski Date: Tue, 12 Feb 2019 21:22:22 +0100 Subject: [PATCH] Update the NuGet package provider when needed (#50759) * Update the NuGet package provider when needed * Update NuGet provider conditionally --- lib/ansible/modules/windows/win_psrepository.ps1 | 14 ++++++++++++-- .../targets/win_psrepository/tasks/main.yml | 12 ------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/ansible/modules/windows/win_psrepository.ps1 b/lib/ansible/modules/windows/win_psrepository.ps1 index 7aec08fb24..da637f02eb 100644 --- a/lib/ansible/modules/windows/win_psrepository.ps1 +++ b/lib/ansible/modules/windows/win_psrepository.ps1 @@ -1,7 +1,7 @@ #!powershell -# Copyright: (c) 2017, Daniele Lazzari # Copyright: (c) 2018, Wojciech Sciesinski +# Copyright: (c) 2017, Daniele Lazzari # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) #Requires -Module Ansible.ModuleUtils.Legacy @@ -18,13 +18,21 @@ $installationpolicy = Get-AnsibleParam -obj $params -name "installation_policy" $result = @{"changed" = $false} +Function Update-NuGetPackageProvider { + $PackageProvider = Get-PackageProvider -ListAvailable | Where-Object { ($_.name -eq 'Nuget') -and ($_.version -ge "2.8.5.201") } + if ($null -eq $PackageProvider) { + Find-PackageProvider -Name Nuget -ForceBootstrap -IncludeDependencies -Force | Out-Null + } +} + $Repo = Get-PSRepository -Name $name -ErrorAction Ignore if ($state -eq "present") { - if ($null -eq $Repo){ + if ($null -eq $Repo) { if ($null -eq $installationpolicy) { $installationpolicy = "trusted" } if (-not $check_mode) { + Update-NuGetPackageProvider Register-PSRepository -Name $name -SourceLocation $source -InstallationPolicy $installationpolicy } $result.changed = $true @@ -42,6 +50,7 @@ if ($state -eq "present") { if ($changed_properties.Count -gt 0) { if (-not $check_mode) { + Update-NuGetPackageProvider Set-PSRepository -Name $name @changed_properties } $result.changed = $true @@ -50,6 +59,7 @@ if ($state -eq "present") { } elseif ($state -eq "absent" -and $null -ne $Repo) { if (-not $check_mode) { + Update-NuGetPackageProvider Unregister-PSRepository -Name $name } $result.changed = $true diff --git a/test/integration/targets/win_psrepository/tasks/main.yml b/test/integration/targets/win_psrepository/tasks/main.yml index 4667bbc7fd..72226eec44 100644 --- a/test/integration/targets/win_psrepository/tasks/main.yml +++ b/test/integration/targets/win_psrepository/tasks/main.yml @@ -13,18 +13,6 @@ when: powershell_major_version.stdout | int >= 5 block: - - name: update NuGet version - win_shell: | - $nuget_exists = (Get-PackageProvider | Where-Object { $_.Name -eq 'Nuget' } | Measure-Object).Count -eq 1 - - if ( $nuget_exists ) { - $nuget_outdated = (Get-PackageProvider -Name NuGet -ErrorAction Ignore).Version -lt [Version]"2.8.5.201" - } - - if ( -not $nuget_exists -or $nuget_outdated ) { - Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force - } - - name: ensure test repository is deleted win_psrepository: name: '{{ repository_name }}'