name: Linux CI

on: [push, pull_request]

permissions: {}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

env:
  TRAVIS_OS_NAME: linux
  SPEC_SPLIT_DOTS: 160

jobs:
  # Test against latest release and the earliest supported Crystal version for forward compatibility.
  #
  # A more advanced test which runs against *each* supported version is available in `forward-compatibility.yml` and
  # runs regularly on a nightly schedule.
  test:
    env:
      ARCH: ${{ matrix.arch }}
      ARCH_CMD: linux64
    runs-on: ${{ case(startsWith(matrix.arch, 'aarch64'), 'ubuntu-24.04-arm', 'ubuntu-latest') }}
    strategy:
      fail-fast: false
      matrix:
        arch:
          - x86_64
          - x86_64-musl
          - aarch64
          - aarch64-musl
        env: [{}]
        include:
          - arch: x86_64
            env:
              CRYSTAL_BOOTSTRAP_VERSION: 1.0.0
              # libffi is only available starting from the 1.2.2 build images
              FLAGS: "-Dwithout_ffi"
              # pcre2 is only available starting from the 1.7.0 build images
              USE_PCRE1: true
    steps:
      - name: Download Crystal source
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Prepare System
        run: bin/ci prepare_system

      - name: Prepare Build
        run: bin/ci prepare_build

      - name: Test
        run: bin/ci build
        env: ${{ matrix.env }}

  test-std:
    env:
      ARCH: ${{ matrix.arch }}
      ARCH_CMD: linux64
    runs-on: ${{ case(startsWith(matrix.arch, 'aarch64'), 'ubuntu-24.04-arm', 'ubuntu-latest') }}
    strategy:
      fail-fast: false
      matrix:
        arch:
          - x86_64
          - aarch64
        opts:
          - flags: "-Dexecution_context -Dpreview_mt" # execution_context: concurrent
            workers: 1
          - flags: "-Dexecution_context -Dpreview_mt" # execution_context: parallel
            workers: 4
          - flags: "-Devloop=io_uring"
            workers: 1
          - flags: "-Dexecution_context -Dpreview_mt -Dio_uring"
            workers: 4
        include:
          - arch: x86_64
            opts:
              flags: "-Devloop=libevent"
    steps:
      - name: Download Crystal source
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Prepare System
        run: bin/ci prepare_system

      - name: Prepare Build
        run: |
          bin/ci prepare_build
          bin/ci with_build_env crystal version

      - name: Test
        run: bin/ci with_build_env 'CRYSTAL_WORKERS=${{ matrix.opts.workers || 1 }} make std_spec threads=1 FLAGS="${{ matrix.opts.flags }}"'

  check_format:
    env:
      ARCH: x86_64
      ARCH_CMD: linux64
    runs-on: ubuntu-latest
    steps:
      - name: Download Crystal source
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Prepare System
        run: bin/ci prepare_system

      - name: Prepare Build
        run: bin/ci prepare_build

      - name: Check Format
        run: bin/ci format

  lint_spellcheck:
    name: Spell Check with Typos
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Actions Repository
      uses: actions/checkout@v6
      with:
        persist-credentials: false

    - name: Spell Check Repo
      uses: crate-ci/typos@631208b7aac2daa8b707f55e7331f9112b0e062d # v1.44.0
      env:
        CLICOLOR: 1

  lint_ameba:
    runs-on: ubuntu-latest
    container: ghcr.io/crystal-ameba/ameba:sha-d861f4ed0f904e0ecdad11c26f98e968f7d95afa # 1.7.0-dev
    steps:
      - name: Download Crystal source
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - run: ameba
