name: CI

on:
  push:
    branches:
    - master
    - "check/ci/**"
    - "check/unix/**"
  pull_request:
    types:
    - opened
    - synchronize
    - reopened

jobs:
  test:
    name: ${{ matrix.venv }}${{ matrix.os }}/${{ matrix.ruby }}/${{ matrix.python }}-${{ matrix.python_architecture }}
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os:
        - ubuntu-latest
        - ubuntu-20.04
        - macos-latest
        ruby:
        - "3.3"
        - "3.2"
        - "3.1"
        - "3.0"
        - "2.7"
        python:
        - "3.x"
        python_architecture:
        - x64
        venv:
        - ""
        include:
        # Old ruby and the latest Python 3
        - { os: ubuntu-20.04   , ruby: 2.6   , python: "3.x"  , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.5   , python: "3.x"  , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.4   , python: "3.x"  , python_architecture: x64 , venv: "" }

        # Ruby 2.7 with Each Python 3.x
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.12" , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.11" , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.10" , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.9"  , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.8"  , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.7"  , python_architecture: x64 , venv: "" }
        - { os: ubuntu-20.04   , ruby: 2.7   , python: "3.6"  , python_architecture: x64 , venv: "" }

        # Ruby-debug with the latest Python 3
        - { os: ubuntu-20.04   , ruby: debug , python: "3.x"  , python_architecture: x64 , venv: "" }

        # The latest stable Ruby with the latest Python 3 with venv
        - { os: ubuntu-20.04   , ruby: "3.3" , python: "3.x"  , python_architecture: x64 , venv: "venv:"  }

        # macOS with venv
        - { os: macos-latest   , ruby: "3.3" , python: "3.x"  , python_architecture: x64 , venv: "venv:"  }

        #- { os: macos-latest   , ruby: debug , python: "3.x" , python_architecture: x64 , venv: "" }

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 1

    - uses: ruby/setup-ruby@v1
      if: matrix.ruby_version != 'master-nightly'
      with:
        ruby-version: ${{ matrix.ruby }}

    - uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python }}
        architecture: ${{ matrix.python_architecture }}

    - run: pip install --user numpy

    - run: bundle install

    - run: rake compile

    - run: python lib/pycall/python/investigator.py

    - name: venv examination
      run: |
        python -m venv ~/test-venv
        source ~/test-venv/bin/activate
        ruby -Ilib -Iext/pycall -rpycall -ePyCall.builtins
      env:
        PYCALL_DEBUG_FIND_LIBPYTHON: 1
      if: ${{ matrix.venv != '' }}

    - run: rake
      env:
        PYTHON: python

  conda:
    name: conda:${{ matrix.os }}/${{ matrix. ruby }}/${{ matrix.python }}
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os:
        - ubuntu-20.04
        #- macos-latest
        ruby:
        - "3.1"
        python:
        - "3.10"

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 1

    - uses: conda-incubator/setup-miniconda@v2
      with:
        activate-environment: test
        python-version: ${{ matrix.python }}

    - uses: ruby/setup-ruby@v1
      if: matrix.ruby_version != 'master-nightly'
      with:
        ruby-version: ${{ matrix.ruby }}

    - name: Add Ruby path
      run: |
        echo >> ~/.profile
        echo >> ~/.profile
        IFS=:
        for p in $PATH; do
          case $p in
          */Ruby/*)
            echo "export PATH=$p:\$PATH" >> ~/.profile
            ;;
          esac
        done

    - run: cat ~/.profile

    - run: bash -xe ~/.profile

    - run: conda install numpy
      shell: bash -l {0}

    - run: bundle install

    - run: rake compile

    - run: python lib/pycall/python/investigator.py
      shell: bash -l {0}

    - run: rake
      env:
        PYTHON: python
      shell: bash -l {0}
