version: 2.1

# Aliases to reuse
_imageconfig: &imageconfig
  docker:
    - image: cimg/base:2022.10-22.04

commands:
  bash_env:
    steps:
      - run:
          name: Set BASH_ENV
          command: |
            ./.circleci/setup_bash.sh

jobs:
  setup_env:
    <<: *imageconfig
    steps:
      # Get our data and merge with upstream
      - checkout
      - run:
          name: Merge with upstream
          command: |
            echo $(git log -1 --pretty=%B) | tee gitlog.txt
            echo ${CI_PULL_REQUEST//*pull\//} | tee merge.txt
            if [[ $(cat merge.txt) != "" ]] && [[ $(cat gitlog.txt) != *"[circle nomerge]"*  ]]; then
              echo "Merging $(cat merge.txt)";
              git pull --ff-only origin "refs/pull/$(cat merge.txt)/merge";
            fi
      - bash_env
      - restore_cache:
          keys:
            - cache-pip-1
      - run:
          name: Pip
          # for VTK: https://gitlab.kitware.com/vtk/vtk/-/packages/102
          command: |
            pip install --upgrade --only-binary ":all:" pip setuptools
            pip install --upgrade --only-binary ":all:" \
                numpy matplotlib seaborn statsmodels pillow joblib \
                "sphinx!=7.3.2,!=7.3.3,!=7.3.4,!=7.3.5,!=7.3.6" pytest \
                traits memory_profiler "ipython!=8.7.0" plotly graphviz \
                "pyvista>=0.44.0" "docutils>=0.18" imageio pydata-sphinx-theme \
                "jupyterlite-sphinx>=0.8.0,<0.9.0" "jupyterlite-pyodide-kernel<0.1.0" \
                libarchive-c "sphinxcontrib-video>=0.2.1rc0" intersphinx_registry
            pip uninstall -yq vtk  # pyvista installs vtk above
            pip install --upgrade --only-binary ":all" --extra-index-url https://wheels.vtk.org vtk-osmesa
      - save_cache:
          key: cache-pip-1
          paths:
            - ~/.cache/pip
      - run:
          name: Test libs
          command: |
            python -c "import pyvista;print(pyvista.Report())"
      - run:
          name: Install
          command: |
            pip install -e .
      - persist_to_workspace:
          root: ~/
          paths:
            - project
            - python_env

  build_docs:
    <<: *imageconfig
    steps:
      - attach_workspace:
          at: ~/
      - bash_env
      - run: sphinx-build doc doc/_build/html -nW --keep-going -b html 2>&1 | tee sphinx_log.txt
      - run:
          name: Check sphinx log for warnings (which are treated as errors)
          when: always
          command: |
            ! grep "^.* WARNING: .*$" sphinx_log.txt
      - store_artifacts:
          path: doc/_build/html/
          destination: html
      - store_test_results:
          path: doc/_build/html/
      - persist_to_workspace:
          root: doc/_build/html
          paths: .

  build_tinyhtml:
    <<: *imageconfig
    steps:
      - attach_workspace:
          at: ~/
      - bash_env
      - run: make -C sphinx_gallery/tests/tinybuild/doc clean html
      - store_artifacts:
          path: sphinx_gallery/tests/tinybuild/doc/_build/html
          destination: tiny_html

  build_latexpdf:
    <<: *imageconfig
    steps:
      - attach_workspace:
          at: ~/
      - bash_env
      - run:
          name: Get LaTeX tools
          command: |
            sudo apt install texlive texlive-latex-extra latexmk tex-gyre
      - run:
          name: latexpdf
          command: |
            make -C sphinx_gallery/tests/tinybuild/doc clean latexpdf
      - store_artifacts:
          path: sphinx_gallery/tests/tinybuild/doc/_build/latex/
          destination: latex

  deploy_dev:
    docker:
      - image: cimg/base:current
    steps:
      - checkout
      - add_ssh_keys:
          fingerprints:
            - "87:18:18:25:10:8c:29:0f:25:bd:28:b1:4f:cd:af:96"
      - attach_workspace:
          at: html
      - run: ./.circleci/docs_deploy.sh html dev

  deploy_stable:
    docker:
      - image: cimg/base:current
    steps:
      - checkout
      - add_ssh_keys:
          fingerprints:
            - "87:18:18:25:10:8c:29:0f:25:bd:28:b1:4f:cd:af:96"
      - attach_workspace:
          at: html
      - run: ./.circleci/docs_deploy.sh html stable


workflows:
  version: 2
  default:
    jobs:
      # https://circleci.com/docs/2.0/workflows/#executing-workflows-for-a-git-tag
      # Run for all branches and tags
      - setup_env: &filter_tags
          filters:
            tags:
              only: /.*/
      - build_docs:
          requires:
            - setup_env
          <<: *filter_tags
      - build_tinyhtml:
          requires:
            - setup_env
          <<: *filter_tags
      - build_latexpdf:
          requires:
            - setup_env
          <<: *filter_tags
      # Run for master branch
      - deploy_dev:
          requires:
            - build_docs
          filters:
            branches:
              only: master
      # Run for tags named vX.Y.Z
      - deploy_stable:
          requires:
            - build_docs
          filters:
            branches:
              ignore: /.*/
            tags:
              only: /^v\d+\.\d+\.\d+$/
