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

pr:
  branches:
    include:
    - master

jobs:
- job:
  displayName: linux
  pool:
    vmImage: 'ubuntu-latest'
  strategy:
    matrix:
      Python39:
        python.version: '3.9'
      Python310:
        python.version: '3.10'
      Python311:
        python.version: '3.11'

  steps:
  - bash: echo "##vso[task.prependpath]$CONDA/bin"
    displayName: Add conda to PATH

  - bash: |
      conda config --add channels conda-forge
      conda config --set channel_priority strict
      conda create --yes --quiet --name build python=$PYTHON_VERSION conda conda-build
    displayName: Create Anaconda build environment

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate build
      conda build -m "conda_package/ci/linux_python${PYTHON_VERSION}.yaml" "conda_package/recipe"
    displayName: Build MPAS-Tools

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate build
      conda create --yes --quiet --name test -c ${CONDA_PREFIX}/conda-bld/ \
          python=$PYTHON_VERSION mpas_tools pytest
    displayName: Create Anaconda test environment

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate test
      pytest --pyargs mpas_tools
    displayName: pytest

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate build
      conda create --yes --quiet --name docs -c ${CONDA_PREFIX}/conda-bld/ \
          python=$PYTHON_VERSION mpas_tools sphinx mock sphinx_rtd_theme m2r
    condition: eq(variables['python.version'], '3.9')
    displayName: Create Anaconda docs environment

  - bash: |

      # error out when something goes wrong
      set -e

      eval "$(conda shell.bash hook)"
      conda activate docs

      echo "source branch: $(Build.SourceBranch)"
      echo "target branch: $(System.PullRequest.TargetBranch)"
      echo "repository: $(Build.Repository.Name)"

      tag=$(git describe --tags $(git rev-list --tags --max-count=1))
      echo "tag: $tag"

      version=`python -c "import mpas_tools; print(mpas_tools.__version__)"`
      echo "version: $version"

      REPO_PATH=$PWD

      if [[ "$(Build.SourceBranch)" == "refs/heads/master" ]]; then
        export DOCS_VERSION="stable"
        deploy=True
      elif [[ "$(Build.SourceBranch)" == refs/tags/* ]]; then
        # this is a tag build
        export DOCS_VERSION="$tag"
        deploy=True
      else
        DOCS_VERSION="$version"
        export DOCS_VERSION
        deploy=False
      fi
      echo "Docs version: $DOCS_VERSION"
      echo "Deploy to gh-pages? $deploy"
      cd conda_package/docs || exit 1
      make html

      cd "$REPO_PATH" || exit 1

      if [[ "$deploy" == "True" ]]; then
        PUBLICATION_BRANCH=gh-pages
        # Checkout the branch
        pushd $HOME || exit 1
        git clone --branch=$PUBLICATION_BRANCH https://$(GitHubToken)@github.com/$(Build.Repository.Name) publish
        cd publish || exit 1

        # Update pages
        if [[ -d "$DOCS_VERSION" ]]; then
          git rm -rf "$DOCS_VERSION" > /dev/null
        fi
        mkdir "$DOCS_VERSION"
        cp -r "$REPO_PATH"/conda_package/docs/_build/html/* "$DOCS_VERSION"
        # Commit and push latest version
        git add .
        if git diff-index --quiet HEAD; then
          echo "No changes in the docs."
        else
          git config --local user.name "Azure Pipelines"
          git config --local user.email "azuredevops@microsoft.com"
          git commit -m "[skip ci] Update $DOCS_VERSION"
          git push -fq origin $PUBLICATION_BRANCH
        fi
        popd || exit 1
      fi
    condition: eq(variables['python.version'], '3.9')
    displayName: build and deploy docs

- job:
  displayName: osx
  pool:
    vmImage: 'macOS-latest'
  strategy:
    matrix:
      Python39:
        python.version: '3.9'
      Python310:
        python.version: '3.10'
      Python311:
        python.version: '3.11'

  steps:
  - bash: echo "##vso[task.prependpath]$CONDA/bin"
    displayName: Add conda to PATH

  - bash: sudo chown -R 501:20 /usr/local/miniconda/pkgs
    displayName: Fix permissions

  - bash: |
      conda config --add channels conda-forge
      conda config --set channel_priority strict
      conda create --yes --quiet --name build python=$PYTHON_VERSION conda conda-build
    displayName: Create Anaconda build environment

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate build
      conda build -m "conda_package/ci/osx_python${PYTHON_VERSION}.yaml" "conda_package/recipe"
    displayName: Build MPAS-Tools

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate build
      conda create --yes --quiet --name test -c ${CONDA_PREFIX}/conda-bld/ \
          python=$PYTHON_VERSION mpas_tools pytest
    displayName: Create Anaconda test environment

  - bash: |
      eval "$(conda shell.bash hook)"
      conda activate test
      pytest --pyargs mpas_tools
    displayName: pytest

