# Installs PackageManagement and PowerShellGet to the required versions for testing
---
- name: check if PackageManagement has been installed
  win_shell: if (Get-Command -Name Install-Module -ErrorAction SilentlyContinue) { $true } else { $false }
  changed_when: False
  register: module_installed

- name: install PackageManagement and PowerShellGet
  when: not module_installed.stdout | trim | bool
  block:
  - name: install PackageManagement
    win_package:
      path: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/PackageManagement_x64.msi
      product_id: '{57E5A8BB-41EB-4F09-B332-B535C5954A28}'
      state: present

  - name: create the required folder
    win_file:
      path: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
      state: directory

  - name: download nuget provider dll
    win_get_url:
      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll
      dest: C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll

- name: get version and install location of PackageManagement and PowerShellGet
  win_shell: |
    $info = @{}
    $modules = Get-Module -ListAvailable | Where-Object {
        ($_.Name -eq "PackageManagement" -and $_.Version -lt "1.1.7") -or ($_.Name -eq "PowerShellGet" -and $_.Version -lt "1.6.0")
    } | ForEach-Object {
        $module_info = @{}
        if ([System.IO.Path]::GetFileName($_.ModuleBase) -eq $_.Name) {
            $module_info.remove_path = $_.ModuleBase
            $module_info.install_path = $_.ModuleBase
        } else {
            $module_version = switch($_.Name) {
                PackageManagement { "1.1.7.0" }
                PowerShellGet { "1.6.0" }
            }
            $module_info.remove_path = ""
            $module_info.install_path = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.ModuleBase), $module_version))
        }
        $info.($_.Name) = $module_info
    }

    ConvertTo-Json -InputObject $info -Compress
  changed_when: False
  register: installed_modules

- name: register installed_modules info
  set_fact:
    installed_modules: '{{ installed_modules.stdout | trim | from_json }}'

- name: update the PackageManagement and PowerShellGet versions
  when: installed_modules.keys() | list | length > 0
  block:
  - name: download newer PackageManagement and PowerShellGet nupkg
    win_get_url:
      url: '{{ item.url }}'
      dest: '{{ remote_tmp_dir }}\{{ item.name }}.zip'  # .zip is required for win_unzip
    when: item.name in installed_modules
    loop:
    - name: PackageManagement
      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/packagemanagement.1.1.7.nupkg
    - name: PowerShellGet
      url: https://ansible-ci-files.s3.amazonaws.com/test/integration/targets/setup_win_psget/powershellget.1.6.0.nupkg

  - name: remove the old versions of PackageManagement and PowerShellGet
    win_file:
      path: '{{ item.value.remove_path }}'
      state: absent
    # This isn't necessary on 2016+ as packages are installed in a version specific dir
    when: item.value.remove_path != ""
    with_dict: '{{ installed_modules }}'

  - name: extract new modules to correct location
    win_unzip:
      src: '{{ remote_tmp_dir }}\{{ item.name }}.zip'
      dest: '{{ item.path }}'
    when: item.path != ""
    loop:
    - name: PackageManagement
      path: '{{ installed_modules.PackageManagement.install_path | default("") }}'
    - name: PowerShellGet
      path: '{{ installed_modules.PowerShellGet.install_path | default("") }}'
