parameters:
  platform: none
  installer: none

steps:
- task: CacheBeta@0
  displayName: HDF5 cache
  inputs:
    # The key should specify enough to avoid restoring an incompatible build.
    # Increment the first number to clear the cache if you change something
    # not captured by the key, e.g. dependencies.
    ${{ if eq(parameters.platform, 'windows') }}:
      key: 0 | HDF5 | "$(Agent.OS)" | "$(Agent.OSArchitecture)" | "$(HDF5_VERSION)" | "$(HDF5_VSVERSION)" | "$(HDF5_MPI)" | ci/get_hdf5_win.py
    ${{ if ne(parameters.platform, 'windows') }}:
      key: 1 | HDF5 | "$(Agent.OS)" | "$(Agent.OSArchitecture)" | "$(HDF5_VERSION)" | "$(HDF5_MPI)" | ci/get_hdf5_if_needed.sh
    path: $(HDF5_CACHE_DIR)
  condition: and(succeeded(), ne(variables['HDF5_VERSION'], ''))

- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(python.version)'
    architecture: 'x64'
  displayName: 'Use Python $(python.version)'
  condition: and(succeeded(), ne(variables['python.version'], 'Pre'))

  #- task: stevedower.python.InstallPython.InstallPython@1
  #  displayName: 'Use prerelease Python'
  #  inputs:
  #    prerelease: true
  #  condition: and(succeeded(), eq(variables['python.version'], 'Pre'))

- ${{ if eq(parameters.installer, 'nuget') }}:
  - task: NuGetToolInstaller@0
    displayName: 'Use latest available Nuget'

  - script: |
      nuget install zlib-msvc-x64 -ExcludeVersion -OutputDirectory "$(build.BinariesDirectory)"
      echo ##vso[task.prependpath]$(build.BinariesDirectory)\zlib-msvc-x64\build\native\bin_release
      echo ##vso[task.setvariable variable=CL]/I$(build.BinariesDirectory)\zlib-msvc-x64\build\native\include
      echo ##vso[task.setvariable variable=LINK]/LIBPATH:$(build.BinariesDirectory)\zlib-msvc-x64\build\native\lib_release
      echo ##vso[task.setvariable variable=ZLIB_ROOT]$(build.BinariesDirectory)\zlib-msvc-x64\build\native
    displayName: 'Install nuget dependencies'
- ${{ if eq(parameters.installer, 'brew') }}:
  - script: |
      brew install pkg-config hdf5@1.10 ccache open-mpi
      brew link --force hdf5@1.10
    displayName: 'Install brew dependencies'
- ${{ if eq(parameters.installer, 'apt') }}:
  - script: |
      sudo apt-get update
      sudo apt-get install ccache openmpi-bin libopenmpi-dev
    displayName: 'Install apt dependencies'
    condition: and(succeeded(), ne(variables['HDF5_MPI'], ''))
- ${{ if eq(parameters.installer, 'apt') }}:
  - script: |
      sudo apt-get update
      sudo apt-get install libhdf5-serial-dev
    displayName: 'Install HDF5 via apt'
    # If HDF5_DIR is specified, HDF5 is built from source, so don't get it from apt
    condition: and(succeeded(), eq(variables['HDF5_DIR'], ''))

- script: |
    python -m pip install --upgrade pip
    pip install tox codecov
  displayName: 'Install pip dependencies'

- script: env
  displayName: 'print env'

- ${{ if eq(parameters.shell, 'unix') }}:
    - script: |
        ./ci/get_hdf5_if_needed.sh
      displayName: 'ensure HDF5'
- ${{ if eq(parameters.shell, 'cmd') }}:
    - script: |
        py -3.7 -m pip install requests
        py -3.7 ci\\get_hdf5_win.py
      displayName: 'ensure HDF5'

- script: env
  displayName: 'print env'

- script: |
    tox
  displayName: 'tox'

- task: PythonScript@0
  inputs:
    scriptSource: 'filePath'
    scriptPath: 'ci/upload_coverage.py'
    arguments: --codecov-token 813fb6da-087d-4b36-a185-5a530cab3455
  displayName: 'codecov'
