parameters:
- name: platform
  type: string
- name: python_env
  type: string
  default: default
  values:
  - default
  - noop
  - venv

steps:
- template: config-version.yml
  parameters:
    platform: ${{ parameters.platform }}
    python_env: ${{ parameters.python_env }}

- script: |
    set -e
    sudo apt-get install -y swig3.0
    sudo rm -f /usr/bin/swig
    sudo ln -s swig3.0 /usr/bin/swig
  displayName: (Ubuntu) Downgrade swig
  condition: and(succeeded(), contains('${{ parameters.platform }}', 'ubuntu'))

- script: |
    set -e
    brew install 'swig@3'
    rm /usr/local/bin/swig
    ln -s '/usr/local/opt/swig@3/bin/swig' /usr/local/bin/swig
  displayName: (macOS) Downgrade swig
  condition: and(succeeded(), contains('${{ parameters.platform }}', 'macos'))

- script: |
    set -e
    azcopy copy 'https://nni.blob.core.windows.net/cache/dependencies-${{ parameters.platform }}.zip' dependencies.zip
    python test/vso_tools/unpack_dependencies.py dependencies.zip
    rm dependencies.zip
  displayName: (POSIX) Download cache
  condition: and(succeeded(), not(contains('${{ parameters.platform }}', 'windows')))
  continueOnError: true

- powershell: |
    azcopy copy 'https://nni.blob.core.windows.net/cache/dependencies-${{ parameters.platform }}.zip' dependencies.zip
    python test/vso_tools/unpack_dependencies.py dependencies.zip
    Remove-Item dependencies.zip
  displayName: (Windows) Download cache
  condition: and(succeeded(), contains('${{ parameters.platform }}', 'windows'))
  continueOnError: true

- script: |
    mv dependencies/recommended_legacy.txt dependencies/recommended.txt
  displayName: (legacy) Activate legacy dependencies
  condition: and(succeeded(), contains('${{ parameters.platform }}', 'legacy'))

- ${{ if contains(parameters.platform, 'windows') }}:
    powershell:
      Move-Item -Force -Path dependencies/recommended_gpu.txt -Destination dependencies/recommended.txt
  ${{ else }}:
    script: |
      mv dependencies/recommended_gpu.txt dependencies/recommended.txt
  displayName: (GPU) Activate CUDA dependencies
  condition: and(succeeded(), contains('${{ parameters.platform }}', 'gpu'))

- script: |
    echo '===== develop ====='
    python -m pip install -r dependencies/develop.txt
    echo '===== required ====='
    python -m pip install -r dependencies/required.txt
    echo '===== required extra ====='
    python -m pip install -r dependencies/required_extra.txt
    echo '===== recommended ====='
    python -m pip install -r dependencies/recommended.txt
  displayName: Install Python dependencies

- script: |
    ls -al ${HOME}/.local/bin
    echo "##vso[task.setvariable variable=PATH]${HOME}/.local/bin:${PATH}"
  displayName: (POSIX) Add local bin to PATH
  condition: and(succeeded(), not(contains('${{ parameters.platform }}', 'windows')))

# TODO: Delete this after upgrading to PyTorch 1.11.
- script: |
    python test/vso_tools/interim_patch.py
  displayName: Torch utils tensorboard interim patch

- script: |
    python --version
    python -m pip --version
    python -m pip list
  displayName: List pip dependencies
