name: $(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.rr)
resources:
  repositories:
  - repository: tox
    type: github
    endpoint: github-gb
    name: tox-dev/azure-pipelines-template
    ref: master

trigger:
  batch: true
  branches:
    include:
    - master
    - rewrite
    - refs/tags/*

pr:
  branches:
    include:
    - '*'

schedules:
- cron: "12 0 * * *"
  displayName: Daily build
  branches:
    include: [ master, rewrite ]
  always: true

variables:
  PYTEST_ADDOPTS: "-vv --durations=10"
  CI_RUN: 'yes'
  UPGRADE_ADVISORY: 'yes'

jobs:
- template: run-tox-env.yml@tox
  parameters:
    jobs:
      py38:
        image: [linux, vs2017-win2016, macOs]
      py37:
        image: [linux, vs2017-win2016, macOs]
      py36:
        image: [linux, vs2017-win2016, macOs]
      py35:
        image: [linux, vs2017-win2016, macOs]
      py27:
        image: [linux, windows, macOs]
      pypy:
        image: [linux, windows, macOs]
      pypy3:
        image: [linux, windows, macOs]
      fix_lint:
        image: [linux, windows]
      zipapp:
        image: [linux, windows]
      docs:
        image: [linux, windows]
      readme:
        image: [linux, windows]
      upgrade:
        image: [linux, windows]
      dev: null
    before:
      - script: 'sudo apt-get update -y && sudo apt-get install fish csh'
        condition: and(succeeded(), eq(variables['image_name'], 'linux'), in(variables['TOXENV'], 'py38', 'py37', 'py36', 'py35', 'py27', 'pypy', 'pypy3'))
        displayName: install fish and csh via apt-get
      - script: 'brew update -vvv && brew install fish tcsh'
        condition: and(succeeded(), eq(variables['image_name'], 'macOs'), in(variables['TOXENV'], 'py38', 'py37', 'py36', 'py35', 'py27', 'pypy', 'pypy3'))
        displayName: install fish and csh via brew
      - task: UsePythonVersion@0
        condition: and(succeeded(), in(variables['TOXENV'], 'py38', 'py37', 'py36', 'py35'))
        displayName: provision cpython 2
        inputs:
          versionSpec: '2.7'
      - task: UsePythonVersion@0
        condition: and(succeeded(), in(variables['TOXENV'], 'py27'))
        displayName: provision cpython 3
        inputs:
          versionSpec: '3.8'
      - task: UsePythonVersion@0
        condition: and(succeeded(), in(variables['TOXENV'], 'pypy'))
        displayName: provision pypy 3
        inputs:
          versionSpec: 'pypy3'
      - task: UsePythonVersion@0
        condition: and(succeeded(), in(variables['TOXENV'], 'pypy3'))
        displayName: provision pypy 2
        inputs:
          versionSpec: 'pypy2'
      - script: choco install vcpython27 --yes -f
        condition: and(succeeded(), eq(variables['image_name'], 'windows'), in(variables['TOXENV'], 'py27', 'pypy'))
        displayName: Install Visual C++ for Python 2.7
    coverage:
      with_toxenv: 'coverage' # generate .tox/.coverage, .tox/coverage.xml after test run
      for_envs: [py38, py37, py36, py35, py27, pypy, pypy3]

- ${{ if startsWith(variables['Build.SourceBranch'], 'refs/tags/') }}:
  - template: publish-pypi.yml@tox
    parameters:
      external_feed: 'gb'
      pypi_remote: 'pypi-gb'
      dependsOn: [fix_lint, embed, docs, report_coverage, dev, readme]
