name: checks
on:
  push:
    branches:
    - develop
  pull_request:
    branches:
    - develop
permissions:
  contents: read
jobs:
  rake-ci:
    strategy:
      fail-fast: false
      matrix:
        os:
        - ubuntu-latest
        - macos-14
        ruby-version:
        - '2.4'
        - '2.5'
        - '2.6'
        - '2.7'
        - '3.0'
        - '3.1'
        - '3.2'
        - '3.3'
        - '3.4'
        exclude:
        # We use xapian-bindings 1.4.22 for Ruby < 3.1, but it does not link on MacOS 14:
        # https://github.com/Garaio-REM/xapian-ruby/issues/10
        - os: macos-14
          ruby-version: '2.4'
        - os: macos-14
          ruby-version: '2.5'
        - os: macos-14
          ruby-version: '2.6'
        - os: macos-14
          ruby-version: '2.7'
        - os: macos-14
          ruby-version: '3.0'
    runs-on: ${{ matrix.os }}
    env:
      nixfile: "contrib/nix/ruby${{ matrix.ruby-version }}-shell.nix"
    steps:
    - name: Check out source
      uses: actions/checkout@v3
      with:
        submodules: recursive
    - name: Cache nix store
      uses: actions/cache@v3
      id: nix-cache
      with:
        path: /tmp/nixcache
        key: nix-store-${{ matrix.os }}-ruby${{ matrix.ruby-version }}-${{ hashFiles('contrib/nix/**') }}
        restore-keys: |
          nix-store-${{ matrix.os }}-ruby${{ matrix.ruby-version }}-
          nix-store-
    - name: Install bash on MacOS
      run: brew install bash
      if: runner.os == 'macOS'
    - name: Install nix
      uses: nixbuild/nix-quick-install-action@v30
    - name: Import nix cache
      run: |
        if [ -f /tmp/nixcache ] ; then
          nix-store --import </tmp/nixcache
        fi
    - name: Run Rake ci task
      run: nix-shell "$nixfile" --run 'rake ci'
    - name: Export nix cache
      run: nix-store --export $(nix-store -qR $(nix-build --no-out-link "$nixfile" -A inputDerivation | tail -n1)) >/tmp/nixcache
