# This template runs a job for arbitrary cargo/cross operations.
parameters:
  # The identifier for the job
  identifier:
  # The display name for the job
  displayName:
  # The host target triple (e.g 'i686-apple-darwin') (required)
  target:
  # The rust channel ('stable', 'beta' or 'nightly')
  channel: stable
  # Whether cross should be used or not
  cross: false
  # Steps before any cargo operations
  preSteps: []
  # Steps after all cargo operations
  postSteps: []
  # List of cargo steps ($CARGO and $TARGET is injected via 'env')
  cargoSteps:
  - bash: $CARGO test --target $TARGET --all
    displayName: Cargo test

jobs:
- job: ${{ coalesce(parameters.identifier, format('rust_{0}_{1}', parameters.channel, replace(parameters.target, '-', '_'))) }}
  displayName: ${{ coalesce(parameters.displayName, format('{0}-{1}', parameters.channel, parameters.target)) }}
  variables:
    ${{ if eq(parameters.cross, 'true') }}:
      toolchain: ${{ parameters.channel }}
      cargo: cross
    ${{ if not(eq(parameters.cross, 'true')) }}:
      toolchain: ${{ format('{0}-{1}', parameters.channel, parameters.target) }}
      cargo: cargo
  pool:
    ${{ if contains(parameters.target, 'windows') }}:
      vmImage: 'vs2017-win2016'
    ${{ if contains(parameters.target, 'apple') }}:
      vmImage: 'macos-10.15'
    ${{ if contains(parameters.target, 'linux') }}:
      vmImage: 'ubuntu-16.04'
  steps:
  - ${{ if contains(parameters.target, 'windows') }}:
    - script: |
        curl -sSf -o rustup-init.exe https://win.rustup.rs
        rustup-init.exe -y --default-toolchain=none
        rustup default $(toolchain)
        echo "##vso[task.setvariable variable=PATH;]%PATH%;%USERPROFILE%\.cargo\bin"
      displayName: Install rust
  - ${{ if not(contains(parameters.target, 'windows')) }}:
    - script: |
        curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain=none
        PATH="$PATH:$HOME/.cargo/bin" rustup default $(toolchain)
        echo "##vso[task.setvariable variable=PATH;]$PATH:$HOME/.cargo/bin"
      displayName: Install rust
  - ${{ if eq(parameters.cross, 'true') }}:
    - script: |
        cargo install cross
      displayName: Install cross
  - ${{ parameters.preSteps }}
  - script: |
      rustup -V
      rustc -Vv
      cargo -V
    displayName: Cargo environment
  - ${{ each step in parameters.cargoSteps }}:
    - ${{ each pair in step }}:
        ${{ pair.key }}: ${{ pair.value }}
      env:
        TARGET: ${{ parameters.target }}
        CARGO: $(cargo)
  - ${{ parameters.postSteps }}
