version: 2.1

# heavily inspired by https://raw.githubusercontent.com/pinax/pinax-wiki/6bd2a99ab6f702e300d708532a6d1d9aa638b9f8/.circleci/config.yml

common: &common
  working_directory: ~/repo
  steps:
    - checkout
    - run:
        name: merge pull request base
        command: ./.circleci/merge_pr.sh
    - run:
        name: merge pull request base (2nd try)
        command: ./.circleci/merge_pr.sh
        when: on_fail
    - run:
        name: merge pull request base (3rd try)
        command: ./.circleci/merge_pr.sh
        when: on_fail
    - restore_cache:
        keys:
          - cache-v1-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}
    - run:
        name: install dependencies
        command: |
          python -m pip install --upgrade pip
          python -m pip install tox
    - run:
        name: run tox
        command: python -m tox run -r
    - save_cache:
        paths:
          - .hypothesis
          - .tox
          - ~/.cache/pip
          - ~/.local
        key: cache-v1-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}

orbs:
  win: circleci/windows@5.0.0

windows-wheel-steps:
  windows-wheel-setup: &windows-wheel-setup
    executor:
      name: win/default
      shell: bash.exe
    working_directory: C:\Users\circleci\project\eth-typing
    environment:
      TOXENV: windows-wheel
  restore-cache-step: &restore-cache-step
    restore_cache:
      keys:
        - cache-v1-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}
  install-pyenv-step: &install-pyenv-step
    run:
      name: install pyenv
      command: |
        pip install pyenv-win --target $HOME/.pyenv
        echo 'export PYENV="$HOME/.pyenv/pyenv-win/"' >> $BASH_ENV
        echo 'export PYENV_ROOT="$HOME/.pyenv/pyenv-win/"' >> $BASH_ENV
        echo 'export PYENV_USERPROFILE="$HOME/.pyenv/pyenv-win/"' >> $BASH_ENV
        echo 'export PATH="$PATH:$HOME/.pyenv/pyenv-win/bin"' >> $BASH_ENV
        echo 'export PATH="$PATH:$HOME/.pyenv/pyenv-win/shims"' >> $BASH_ENV
        source $BASH_ENV
        pyenv update
  install-latest-python-step: &install-latest-python-step
    run:
      name: install latest python version and tox
      command: |
        LATEST_VERSION=$(pyenv install --list | grep -E "${MINOR_VERSION}\.[0-9]+$" | tail -1)
        echo "installing python version $LATEST_VERSION"
        pyenv install $LATEST_VERSION
        pyenv global $LATEST_VERSION
        python3 -m pip install --upgrade pip
        python3 -m pip install tox
  run-tox-step: &run-tox-step
    run:
      name: run tox
      command: |
        echo 'running tox with' $(python3 --version)
        python3 -m tox run -r
  save-cache-step: &save-cache-step
    save_cache:
      paths:
        - .tox
      key: cache-v1-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}

docs: &docs
  working_directory: ~/repo
  steps:
    - checkout
    - restore_cache:
        keys:
          - cache-v1-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}
    - run:
        name: install dependencies
        command: |
          python -m pip install --upgrade pip
          python -m pip install tox
    - run:
        name: install latexpdf dependencies
        command: |
          sudo apt-get update
          sudo apt-get install latexmk tex-gyre texlive-fonts-extra texlive-xetex xindy
    - run:
        name: run tox
        command: python -m tox run -r
    - store_artifacts:
          path: /home/circleci/repo/docs/_build
    - save_cache:
        paths:
          - .tox
          - ~/.cache/pip
          - ~/.local
        key: cache-v1-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "setup.py" }}-{{ checksum "tox.ini" }}
  resource_class: xlarge

jobs:
  docs:
    <<: *docs
    docker:
      - image: cimg/python:3.10
        environment:
          TOXENV: docs

  py38-core:
    <<: *common
    docker:
      - image: cimg/python:3.8
        environment:
          TOXENV: py38-core
  py39-core:
    <<: *common
    docker:
      - image: cimg/python:3.9
        environment:
          TOXENV: py39-core
  py310-core:
    <<: *common
    docker:
      - image: cimg/python:3.10
        environment:
          TOXENV: py310-core
  py311-core:
    <<: *common
    docker:
      - image: cimg/python:3.11
        environment:
          TOXENV: py311-core
  py312-core:
    <<: *common
    docker:
      - image: cimg/python:3.12
        environment:
          TOXENV: py312-core
  py313-core:
    <<: *common
    docker:
      - image: cimg/python:3.13
        environment:
          TOXENV: py313-core

  py38-lint:
    <<: *common
    docker:
      - image: cimg/python:3.8
        environment:
          TOXENV: py38-lint
  py39-lint:
    <<: *common
    docker:
      - image: cimg/python:3.9
        environment:
          TOXENV: py39-lint
  py310-lint:
    <<: *common
    docker:
      - image: cimg/python:3.10
        environment:
          TOXENV: py310-lint
  py311-lint:
    <<: *common
    docker:
      - image: cimg/python:3.11
        environment:
          TOXENV: py311-lint
  py312-lint:
    <<: *common
    docker:
      - image: cimg/python:3.12
        environment:
          TOXENV: py312-lint
  py313-lint:
    <<: *common
    docker:
      - image: cimg/python:3.13
        environment:
          TOXENV: py313-lint

  py38-wheel:
    <<: *common
    docker:
      - image: cimg/python:3.8
        environment:
          TOXENV: py38-wheel
  py39-wheel:
    <<: *common
    docker:
      - image: cimg/python:3.9
        environment:
          TOXENV: py39-wheel
  py310-wheel:
    <<: *common
    docker:
      - image: cimg/python:3.10
        environment:
          TOXENV: py310-wheel
  py311-wheel:
    <<: *common
    docker:
      - image: cimg/python:3.11
        environment:
          TOXENV: py311-wheel
  py312-wheel:
    <<: *common
    docker:
      - image: cimg/python:3.12
        environment:
          TOXENV: py312-wheel
  py313-wheel:
    <<: *common
    docker:
      - image: cimg/python:3.13
        environment:
          TOXENV: py313-wheel

  py311-windows-wheel:
    <<: *windows-wheel-setup
    steps:
      - checkout
      - <<: *restore-cache-step
      - <<: *install-pyenv-step
      - run:
          name: set minor version
          command: echo "export MINOR_VERSION='3.11'" >> $BASH_ENV
      - <<: *install-latest-python-step
      - <<: *run-tox-step
      - <<: *save-cache-step

  py312-windows-wheel:
    <<: *windows-wheel-setup
    steps:
      - checkout
      - <<: *restore-cache-step
      - <<: *install-pyenv-step
      - run:
          name: set minor version
          command: echo "export MINOR_VERSION='3.12'" >> $BASH_ENV
      - <<: *install-latest-python-step
      - <<: *run-tox-step
      - <<: *save-cache-step

  py313-windows-wheel:
    <<: *windows-wheel-setup
    steps:
      - checkout
      - <<: *restore-cache-step
      - <<: *install-pyenv-step
      - run:
          name: set minor version
          command: echo "export MINOR_VERSION='3.13'" >> $BASH_ENV
      - <<: *install-latest-python-step
      - <<: *run-tox-step
      - <<: *save-cache-step

define: &all_jobs
  - docs
  - py38-core
  - py39-core
  - py310-core
  - py311-core
  - py312-core
  - py313-core
  - py38-lint
  - py39-lint
  - py310-lint
  - py311-lint
  - py312-lint
  - py313-lint
  - py38-wheel
  - py39-wheel
  - py310-wheel
  - py311-wheel
  - py312-wheel
  - py313-wheel
  - py311-windows-wheel
  - py312-windows-wheel
  - py313-windows-wheel

workflows:
  version: 2
  test:
    jobs: *all_jobs
  nightly:
    triggers:
      - schedule:
          # Weekdays 12:00p UTC
          cron: "0 12 * * 1,2,3,4,5"
          filters:
            branches:
              only:
                - main
    jobs: *all_jobs
