trigger:
  branches:
    include:
    - '*'
  tags:
    include:
    - '*'

stages:
- stage: static
  displayName: Static Analysis
  jobs:
  - job: checks
    displayName: static code analysis
    pool:
      vmImage: ubuntu-latest
    steps:
      # Run syntax validation using oldest and latest Python
      - task: UsePythonVersion@0
        displayName: Set up python
        inputs:
          versionSpec: 3.6

      - bash: python .azure-pipelines/syntax-validation.py
        displayName: Syntax validation (3.6)

      - task: UsePythonVersion@0
        displayName: Set up python
        inputs:
          versionSpec: 3.10

      - bash: python .azure-pipelines/syntax-validation.py
        displayName: Syntax validation (3.10)

      # Run flake8 validation
      - bash: |
          pip install --disable-pip-version-check flake8 && \
          python .azure-pipelines/flake8-validation.py
        displayName: Flake8 validation

- stage: tests
  displayName: Run unit tests
  jobs:
  - job: linux
    pool:
      vmImage: ubuntu-latest
    strategy:
      matrix:
        python36:
          PYTHON_VERSION: 3.6
        python37:
          PYTHON_VERSION: 3.7
        python38:
          PYTHON_VERSION: 3.8
        python39:
          PYTHON_VERSION: 3.9
        python310:
          PYTHON_VERSION: 3.10
    steps:
    - template: .azure-pipelines/ci.yml

  - job: macOS
    pool:
      vmImage: macOS-latest
    strategy:
      matrix:
        python37:
          PYTHON_VERSION: 3.7
        python38:
          PYTHON_VERSION: 3.8
        python39:
          PYTHON_VERSION: 3.9
        python310:
          PYTHON_VERSION: 3.10
    steps:
    - template: .azure-pipelines/ci.yml

  - job: windows
    pool:
      vmImage: windows-latest
    strategy:
      matrix:
        python36:
          PYTHON_VERSION: 3.6
        python37:
          PYTHON_VERSION: 3.7
        python38:
          PYTHON_VERSION: 3.8
        python39:
          PYTHON_VERSION: 3.9
        python310:
          PYTHON_VERSION: 3.10
    steps:
    - template: .azure-pipelines/ci.yml

- stage: deploy
  displayName: Publish release
  dependsOn:
  - tests
  - static
  condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
  jobs:
  - job: pypi
    displayName: Publish pypi release
    pool:
      vmImage: ubuntu-latest
    steps:
      - task: UsePythonVersion@0
        displayName: Set up python
        inputs:
          versionSpec: 3.9

      - bash: |
          python -m pip install -r requirements_dev.txt
        displayName: Install dependencies

      - bash: |
          python setup.py sdist bdist_wheel
          ls -la dist
        displayName: Build python package

      - task: PublishBuildArtifacts@1
        inputs:
          pathToPublish: dist/
          artifactName: python-release

      - task: TwineAuthenticate@1
        displayName: Set up credentials
        inputs:
          pythonUploadServiceConnection: pypi-procrunner

      - bash: |
          python -m twine upload -r pypi-procrunner --config-file $(PYPIRC_PATH) dist/*.tar.gz dist/*.whl
        displayName: Publish package
