name: CI

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  ubuntu:
    name: Ubuntu
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
        ruby:
          - "3.4"
          - "3.3"
          - "3.2"
          - "3.1"
          - "3.0"
          - "2.7"
          - "2.6"
          - "2.5"
          - debug

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

      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby }}

      - run: rake build

      - name: Install ffi (if Ruby 2.7)
        if: |
          matrix.ruby == '2.7'
        run: |
          cat <<GEMFILE > Gemfile.irb
          source 'https://rubygems.org'
          gem 'ffi'
          GEMFILE
          BUNDLE_GEMFILE=Gemfile.irb bundle install --jobs 4 --retry 3

      - name: Install ffi 1.6.x (if Ruby 2.6)
        if: |
          matrix.ruby == '2.6'
        run: |
          cat <<GEMFILE > Gemfile.irb
          source 'https://rubygems.org'
          gem 'ffi', '~> 1.6.0'
          GEMFILE
          BUNDLE_GEMFILE=Gemfile.irb bundle install --jobs 4 --retry 3

      - name: Install ffi 1.6.x and irb < 1.4.3 (if Ruby 2.5)
        if: |
          matrix.ruby == '2.5'
        run: |
          cat <<GEMFILE > Gemfile.irb
          source 'https://rubygems.org'
          gem 'ffi', '~> 1.6.0'
          gem 'irb', '< 1.4.3'
          GEMFILE
          BUNDLE_GEMFILE=Gemfile.irb bundle install --jobs 4 --retry 3

      - run: gem install pkg/*.gem

      - run: ruby -r iruby -e "p IRuby::SessionAdapter.select_adapter_class"
        env:
          IRUBY_SESSION_ADAPTER: ffi-rzmq

      - name: Install requirements on ubuntu
        run: |
          sudo apt update
          sudo apt install -y --no-install-recommends \
                   libczmq-dev \
                   python3 \
                   python3-pip \
                   python3-setuptools
          sudo pip3 install wheel
          sudo pip3 install -r ci/requirements.txt

      - run: bundle install --jobs 4 --retry 3

      - name: Run tests
        env:
          PYTHON: python3
          ADAPTERS: cztop ffi-rzmq
        run: |
          for adapter in $ADAPTERS; do
            export IRUBY_TEST_SESSION_ADAPTER_NAME=$adapter
            bundle exec rake test TESTOPTS="-v"
          done

  windows:
    name: Windows
    runs-on: windows-latest

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

      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "ruby"

      - run: rake build

      - run: gem install pkg/*.gem

      - run: ruby -r iruby -e "p IRuby::SessionAdapter.select_adapter_class"
        env:
          IRUBY_SESSION_ADAPTER: ffi-rzmq

  macos:
    name: macOS
    runs-on: macos-latest

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

      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "ruby"

      - run: rake build

      - run: gem install pkg/*.gem

      - run: ruby -r iruby -e "p IRuby::SessionAdapter.select_adapter_class"
        env:
          IRUBY_SESSION_ADAPTER: ffi-rzmq
