name: libfyaml

on:
  push:
  pull_request:
  schedule:
    - cron: '33 11 * * 0'
  workflow_dispatch:

# Exercises the experimental, opt-in libfyaml backend (--enable-libfyaml).
# libfyaml is not supported on Windows, so this workflow runs on Linux and
# macOS only. The default libyaml backend is covered by test.yml/libyaml.yml.
jobs:
  ruby-versions:
    uses: ruby/actions/.github/workflows/ruby_versions.yml@master
    with:
      engine: cruby
      min_version: 3.0

  build:
    needs: ruby-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
        os: [ ubuntu-latest, macos-latest ]

    env:
      LIBFYAML_VERSION: "0.9.6"

    steps:
      - uses: actions/checkout@v7.0.1
      - name: Set up Ruby and libfyaml
        uses: ruby/setup-ruby-pkgs@v1
        with:
          ruby-version: ${{ matrix.ruby }}
          apt-get: "pkg-config"
          brew: "libfyaml pkg-config"
      - name: Build libfyaml from source
        # The libfyaml-dev package on Ubuntu is 0.8, which crashes the emitter,
        # so build a known-good release. macOS uses the Homebrew build above.
        if: runner.os == 'Linux'
        run: |
          curl -fsSL "https://github.com/pantoniou/libfyaml/releases/download/v${LIBFYAML_VERSION}/libfyaml-${LIBFYAML_VERSION}.tar.gz" | tar xz
          cd "libfyaml-${LIBFYAML_VERSION}"
          ./configure --prefix=/usr/local
          make -j"$(nproc)"
          sudo make install
          sudo ldconfig
      - name: Install dependencies
        run: bundle install --jobs 3
      - name: Compile with the libfyaml backend
        run: rake compile -- --enable-libfyaml
      - name: Verify the libfyaml backend is active
        run: |
          ruby -Ilib -rpsych -e 'abort "expected the libfyaml backend, got #{Psych::BACKEND}" unless Psych::BACKEND == "libfyaml"'
          ruby -Ilib -rpsych -e 'puts "libfyaml #{Psych.libfyaml_version}"'
      - name: Run test
        run: rake test
