trigger:
  - master

variables:
  PythonVersion36: '3.6'
  PythonVersion37: '3.7'
  PythonVersion38: '3.8'
  PythonVersion39: '3.9'
  PythonVersion310: '3.10.0'
  GetPip: 'https://bootstrap.pypa.io/get-pip.py'

jobs:
  - job: 'sdist'
    displayName: 'Source Distribution'

    pool:
      vmImage: 'ubuntu-18.04'

    steps:
      - template: /.azure-pipelines/use-python-version.yml
        parameters:
          versionSpec: '$(PythonVersion37)'

      - script: |
          echo "Prepending PATH environment variable with directory: $HOME/.local/bin"
          echo "##vso[task.prependpath]$HOME/.local/bin"
        displayName: 'Prepare PATH'

      - script: |
          python --version
          curl -sS $(GetPip) | python - --user
          python -m pip --version
          python -m pip install --user -r dev_requirements.txt
        displayName: 'Install dependencies'
      - script: python setup.py sdist
        displayName: 'Build uAMQP sdist'

      - script: ls ./dist
        displayName: 'Check output'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish sdist artifact'
        inputs:
          artifactName: uamqp-sdist
          pathToPublish: 'dist'

  - job: 'MacOS'

    pool:
      vmImage: 'macOS-10.15'

    strategy:
      matrix:
        Python 3.6:
          PythonBin: 'python3'
          PythonVersion: '$(PythonVersion36)'
        Python 3.7:
          PythonBin: 'python3'
          PythonVersion: '$(PythonVersion37)'
        Python 3.8:
          PythonBin: 'python3'
          PythonVersion: '$(PythonVersion38)'
        Python 3.9:
          PythonBin: 'python3'
          PythonVersion: '$(PythonVersion39)'
        Python 3.10:
          PythonBin: 'python3'
          PythonVersion: '$(PythonVersion310)'

    variables:
      MacOSXDeploymentTarget: '10.9'
      OpenSSLDir: $(Agent.BuildDirectory)/openssl-macosx$(MacOSXDeploymentTarget)
      PythonVersion36: '3.6.8'
      PythonVersion37: '3.7.6'
      PythonVersion38: '3.8.2'
      PythonVersion39: '3.9.0'
      PythonVersion310: '3.10.0'

    steps:
      - task: DownloadPipelineArtifact@1
        displayName: 'Download OpenSSL artifact'
        inputs:
          artifactName: openssl-macosx$(MacOSXDeploymentTarget)
          buildType: specific
          buildVersionToDownload: latest
          downloadPath: $(Agent.BuildDirectory)
          pipeline: 119 # azure-uamqp-python - openssl
          project: '29ec6040-b234-4e31-b139-33dc4287b756' # public

      - script: sudo xcode-select --switch /Applications/Xcode_11.3.1.app
        displayName: 'Select Xcode 11.3.1'

      - script: source ./install_python_osx.sh
        displayName: 'Install Official Python'

      - script: |
          echo "Prepending PATH environment variable with directory: /usr/local/bin"
          echo "##vso[task.prependpath]/usr/local/bin"
          export PYTHON_SHORT_VER=`echo $(PythonVersion) | cut -d . -f 1,2`
          echo "Prepending PATH environment variable with directory: $HOME/Library/Python/$PYTHON_SHORT_VER/bin"
          echo "##vso[task.prependpath]$HOME/Library/Python/$PYTHON_SHORT_VER/bin"
        displayName: 'Prepare PATH'

      - script: |
          $(PythonBin) --version
          curl -sS $(GetPip) | $(PythonBin) - --user
          $(PythonBin) -m pip --version
          $(PythonBin) -m pip install --user -r dev_requirements.txt
        displayName: 'Install dependencies'

      - script: $(PythonBin) setup.py bdist_wheel
        displayName: 'Build uAMQP Wheel'
        env:
          CFLAGS: -mmacosx-version-min=$(MacOSXDeploymentTarget) -I$(OpenSSLDir)/include
          CMAKE_OSX_ARCHITECTURES: 'x86_64'
          CMAKE_OSX_DEPLOYMENT_TARGET: $(MacOSXDeploymentTarget)
          LDFLAGS: -mmacosx-version-min=$(MacOSXDeploymentTarget) -L$(OpenSSLDir)/lib
          MACOSX_DEPLOYMENT_TARGET: $(MacOSXDeploymentTarget)
          OPENSSL_INCLUDE_DIR: $(OpenSSLDir)/include
          OPENSSL_ROOT_DIR: $(OpenSSLDir)
          UAMQP_REBUILD_PYX: 'True'
          UAMQP_USE_OPENSSL: 'True'

      - script: ls ./dist
        displayName: 'Check output'

      - script: |
          $(PythonBin) -m pip install --user --ignore-installed ./dist/*.whl
          pytest tests --doctest-modules --junitxml=junit/test-results-c.xml
          pytest samples --doctest-modules --junitxml=junit/test-results-live.xml
        displayName: 'Run tests'

      - task: PublishTestResults@2
        displayName: 'Publish test results'
        condition: succeededOrFailed()
        inputs:
          testResultsFiles: '**/test-results-*.xml'
          testResultsFormat: 'JUnit'
          testRunTitle: 'MacOS Python $(PythonVersion)'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish wheel artifact'
        inputs:
          artifactName: uamqp-macosx$(MacOSXDeploymentTarget)-$(PythonVersion)-whl
          pathToPublish: 'dist'

  - job: 'Windows'

    pool:
      vmImage: 'vs2017-win2016'

    strategy:
      matrix:
        x64 Python 3.6:
          PythonArchitecture: 'x64'
          PythonVersion: '$(PythonVersion36)'
        x64 Python 3.7:
          PythonArchitecture: 'x64'
          PythonVersion: '$(PythonVersion37)'
        x64 Python 3.8:
          PythonArchitecture: 'x64'
          PythonVersion: '$(PythonVersion38)'
        x64 Python 3.9:
          PythonArchitecture: 'x64'
          PythonVersion: '$(PythonVersion39)'
        x64 Python 3.10:
          PythonArchitecture: 'x64'
          PythonVersion: '$(PythonVersion310)'
        x86 Python 3.6:
          PythonArchitecture: 'x86'
          PythonVersion: '$(PythonVersion36)'
        x86 Python 3.7:
          PythonArchitecture: 'x86'
          PythonVersion: '$(PythonVersion37)'
        x86 Python 3.8:
          PythonArchitecture: 'x86'
          PythonVersion: '$(PythonVersion38)'
        x86 Python 3.9:
          PythonArchitecture: 'x86'
          PythonVersion: '$(PythonVersion39)'
        x86 Python 3.10:
          PythonArchitecture: 'x86'
          PythonVersion: '$(PythonVersion310)'

    steps:
      - template: /.azure-pipelines/use-python-version.yml
        parameters:
          architecture: '$(PythonArchitecture)'
          versionSpec: '$(PythonVersion)'

      - powershell: |
          python --version
          Invoke-WebRequest -UseBasicParsing -Uri $(GetPip) | Select-Object -ExpandProperty Content | python
          python -m pip --version
          python -m pip install -r dev_requirements.txt
        displayName: 'Install dependencies'
        env:
          PYTHONWARNINGS: ignore:DEPRECATION

      - script: python setup.py bdist_wheel
        displayName: 'Build uAMQP Wheel'
        env:
          UAMQP_REBUILD_PYX: 'True'

      - script: dir dist
        displayName: 'Check output'

      - powershell: |
          $whlfile = Get-ChildItem -Filter *.whl dist | Select-Object -First 1 -ExpandProperty Name
          python -m pip install --ignore-installed ./dist/$whlfile
          pytest tests --doctest-modules --junitxml=junit/test-results-c.xml
          pytest samples --doctest-modules --junitxml=junit/test-results-live.xml
        displayName: 'Run tests'

      - task: PublishTestResults@2
        displayName: 'Publish test results'
        condition: succeededOrFailed()
        inputs:
          testResultsFiles: '**/test-results-*.xml'
          testResultsFormat: 'JUnit'
          testRunTitle: 'Windows $(PythonArchitecture) Python $(PythonVersion)'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish wheel artifact'
        inputs:
          artifactName: uamqp-win$(PythonArchitecture)-$(PythonVersion)-whl
          pathToPublish: 'dist'

  - job: 'Linux'

    pool:
      vmImage: 'ubuntu-18.04'

    strategy:
      matrix:
        Python 3.6:
          PythonVersion: '$(PythonVersion36)'
        Python 3.7:
          PythonVersion: '$(PythonVersion37)'
        Python 3.8:
          PythonVersion: '$(PythonVersion38)'
        Python 3.9:
          PythonVersion: '$(PythonVersion39)'
        Python 3.10:
          PythonVersion: '$(PythonVersion310)'

    steps:
      - template: /.azure-pipelines/use-python-version.yml
        parameters:
          versionSpec: '$(PythonVersion)'

      - script: |
          echo "Prepending PATH environment variable with directory: $HOME/.local/bin"
          echo "##vso[task.prependpath]$HOME/.local/bin"
        displayName: 'Prepare PATH'

      - script: |
          python --version
          curl -sS $(GetPip) | python - --user
          python -m pip --version
          python -m pip install --user -r dev_requirements.txt
        displayName: 'Install dependencies'

      - script: python setup.py bdist_wheel
        displayName: 'Build uAMQP Wheel'
        env:
          UAMQP_REBUILD_PYX: 'True'

      - script: ls ./dist
        displayName: 'Check output'

      - script: |
          python -m pip install --user --ignore-installed ./dist/*.whl
          pytest tests --doctest-modules --junitxml=junit/test-results-c.xml
          pytest samples --doctest-modules --junitxml=junit/test-results-live.xml
        displayName: 'Run tests'

      - task: PublishTestResults@2
        displayName: 'Publish test results'
        condition: succeededOrFailed()
        inputs:
          testResultsFiles: '**/test-results-*.xml'
          testResultsFormat: 'JUnit'
          testRunTitle: 'Linux Python $(PythonVersion)'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish wheel artifact'
        inputs:
          artifactName: uamqp-linux-$(PythonVersion)-whl
          pathToPublish: 'dist'

  - job: 'ManyLinux'

    condition: ne(variables['System.TeamProject'], 'public')

    pool:
      vmImage: 'ubuntu-18.04'

    strategy:
      matrix:
        Python36m-manylinux1:
          ContainerImage: 'manylinux_crypto_x64'
          PythonVersion: 'cp36-cp36m'
          Platform: 'manylinux1_x86_64'
        Python37m-manylinux1:
          ContainerImage: 'manylinux_crypto_x64'
          PythonVersion: 'cp37-cp37m'
          Platform: 'manylinux1_x86_64'
        Python38-manylinux1:
          ContainerImage: 'manylinux_crypto_x64'
          PythonVersion: 'cp38-cp38'
          Platform: 'manylinux1_x86_64'
        Python39-manylinux1:
          ContainerImage: 'manylinux_crypto_x64'
          PythonVersion: 'cp39-cp39'
          Platform: 'manylinux1_x86_64'
        Python36m-manylinux2010:
          ContainerImage: 'manylinux2010_crypto_x64'
          PythonVersion: 'cp36-cp36m'
          Platform: 'manylinux2010_x86_64'
        Python37m-manylinux2010:
          ContainerImage: 'manylinux2010_crypto_x64'
          PythonVersion: 'cp37-cp37m'
          Platform: 'manylinux2010_x86_64'
        Python38-manylinux2010:
          ContainerImage: 'manylinux2010_crypto_x64'
          PythonVersion: 'cp38-cp38'
          Platform: 'manylinux2010_x86_64'
        Python39-manylinux2010:
          ContainerImage: 'manylinux2010_crypto_x64'
          PythonVersion: 'cp39-cp39'
          Platform: 'manylinux2010_x86_64'
        Python310-manylinux2010:
          ContainerImage: 'manylinux2010_crypto_x64'
          PythonVersion: 'cp310-cp310'
          Platform: 'manylinux2010_x86_64'

    steps:
      - task: Docker@1
        displayName: 'Build and test with Docker'
        inputs:
          azureContainerRegistry: azuresdkimages.azurecr.io
          azureSubscriptionEndpoint: 'Azure SDK ACR'
          command: 'Run an image'
          containerCommand: '/data/build_many_linux.sh'
          envVars: |
            PLATFORM=$(Platform)
            PYTHON_VERSION=$(PythonVersion)
            UAMQP_REBUILD_PYX=True
          imageName: '$(ContainerImage)'
          qualifyImageName: true
          runInBackground: false
          volumes: '$(System.DefaultWorkingDirectory):/data'

      - script: ls ./wheelhouse
        displayName: 'Check output'

      - task: PublishBuildArtifacts@1
        displayName: 'Publish wheel artifacts'
        inputs:
          artifactName: uamqp-$(Platform)-$(PythonVersion)-x64-whl
          pathToPublish: 'wheelhouse'
