version: 2.1

jobs:
  lint:
    docker:
      - image: cimg/python:3.10.16
    steps:
      - checkout
      - run:
          name: dependencies
          command: |
            source build_tools/shared.sh
            # Include pytest compatibility with mypy
            pip install pytest $(get_dep ruff min) $(get_dep mypy min) cython-lint
      - run:
          name: linting
          command: ./build_tools/linting.sh

  doc-min-dependencies:
    docker:
      - image: cimg/base:current-22.04
    environment:
      - MKL_NUM_THREADS: 2
      - OPENBLAS_NUM_THREADS: 2
      - CONDA_ENV_NAME: testenv
      - LOCK_FILE: build_tools/circle/doc_min_dependencies_linux-64_conda.lock
      # Do not fail if the documentation build generates warnings with minimum
      # dependencies as long as we can avoid raising warnings with more recent
      # versions of the same dependencies.
      - XLEARN_WARNINGS_AS_ERRORS: '0'
    steps:
      - checkout
      - run: ./build_tools/circle/checkout_merge_commit.sh
      - restore_cache:
          key: v1-doc-min-deps-datasets-{{ .Branch }}
      - restore_cache:
          keys:
            - doc-min-deps-ccache-{{ .Branch }}
            - doc-min-deps-ccache
      - run: ./build_tools/circle/build_doc.sh
      - save_cache:
          key: doc-min-deps-ccache-{{ .Branch }}-{{ .BuildNum }}
          paths:
            - ~/.ccache
            - ~/.cache/pip
      - save_cache:
          key: v1-doc-min-deps-datasets-{{ .Branch }}
          paths:
            - ~/scikit_learn_data
      - store_artifacts:
          path: doc/_build/html/stable
          destination: doc
      - store_artifacts:
          path: ~/log.txt
          destination: log.txt

  doc:
    docker:
      - image: cimg/base:current-22.04
    environment:
      - MKL_NUM_THREADS: 2
      - OPENBLAS_NUM_THREADS: 2
      - CONDA_ENV_NAME: testenv
      - LOCK_FILE: build_tools/circle/doc_linux-64_conda.lock
      # Make sure that we fail if the documentation build generates warnings with
      # recent versions of the dependencies.
      - XLEARN_WARNINGS_AS_ERRORS: '1'
    steps:
      - checkout
      - run: ./build_tools/circle/checkout_merge_commit.sh
      - restore_cache:
          key: v1-doc-datasets-{{ .Branch }}
      - restore_cache:
          keys:
            - doc-ccache-{{ .Branch }}
            - doc-ccache
      - run: ./build_tools/circle/build_doc.sh
      - save_cache:
          key: doc-ccache-{{ .Branch }}-{{ .BuildNum }}
          paths:
            - ~/.ccache
            - ~/.cache/pip
      - save_cache:
          key: v1-doc-datasets-{{ .Branch }}
          paths:
            - ~/scikit_learn_data
      - store_artifacts:
          path: doc/_build/html/stable
          destination: doc
      - store_artifacts:
          path: ~/log.txt
          destination: log.txt
      # Persists generated documentation so that it can be attached and deployed
      # in the 'deploy' step.
      - persist_to_workspace:
          root: doc/_build/html
          paths: .

  deploy:
    docker:
      - image: cimg/base:current-22.04
    steps:
      - checkout
      - run: ./build_tools/circle/checkout_merge_commit.sh
      # Attach documentation generated in the 'doc' step so that it can be
      # deployed.
      - attach_workspace:
          at: doc/_build/html
      - run: ls -ltrh doc/_build/html/stable
      - run:
          command: |
            if [[ "${CIRCLE_BRANCH}" =~ ^main$|^[0-9]+\.[0-9]+\.X$ ]]; then
              bash build_tools/circle/push_doc.sh doc/_build/html/stable
            fi

workflows:
  version: 2
  build-doc-and-deploy:
    jobs:
      - lint
      - doc:
          requires:
            - lint
      - doc-min-dependencies:
          requires:
            - lint
      - deploy:
          requires:
            - doc
