name: Continuous Integration

on:
  pull_request:
  push:
    branches: main
  workflow_dispatch:

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
      - run: cargo check

  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          components: rustfmt
      - run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          components: clippy
      - run: cargo clippy -- -D warnings

  spellcheck:
    name: Spellcheck
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: codespell-project/actions-codespell@v1
        with:
          ignore_words_file: .codespellignore

  test:
    name: Test Suite
    runs-on: ${{ matrix.os }}
    needs:
      - check
      - fmt
      - clippy
      - spellcheck
    strategy:
      matrix:
        build:
          - macos
          - macos-aarch64
          - linux
          - linux-musl
          - linux-aarch64
          - linux-arm
        rust: [stable]
        include:
          # See the list: https://github.com/cross-rs/cross

          - build: macos
            os: macos-latest
            target: x86_64-apple-darwin

          - build: macos-aarch64
            os: macos-latest
            target: aarch64-apple-darwin

          - build: linux
            os: ubuntu-latest
            target: x86_64-unknown-linux-gnu

          - build: linux-musl
            os: ubuntu-latest
            target: x86_64-unknown-linux-musl

          - build: linux-aarch64
            os: ubuntu-latest
            target: aarch64-unknown-linux-gnu

          - build: linux-arm
            os: ubuntu-latest
            target: arm-unknown-linux-gnueabihf

    env:
      RUST_BACKTRACE: full
    steps:
      - uses: actions/checkout@v3

      - name: Installing Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}
          target: ${{ matrix.target }}

      - name: Installing needed macOS dependencies
        if: matrix.os == 'macos-latest'
        run: brew install openssl@1.1

      - name: Installing needed Ubuntu dependencies
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update --fix-missing
          sudo apt-get install -y --no-install-recommends liblua5.1-0-dev libluajit-5.1-dev gcc pkg-config curl git make ca-certificates

      - if: matrix.build == 'linux-musl'
        run: sudo apt-get install -y musl-tools

      - if: matrix.build == 'linux-aarch64'
        run: sudo apt-get install -y gcc-aarch64-linux-gnu

      - if: matrix.build == 'linux-arm'
        run: |
          sudo apt-get install -y gcc-multilib
          sudo apt-get install -y gcc-arm-linux-gnueabihf
          sudo ln -s /usr/include/asm-generic/ /usr/include/asm

      - run: cargo build --target ${{ matrix.target }}

      - if: matrix.build == 'macos' || matrix.build == 'linux'
        run: cargo test --target ${{ matrix.target }}

  # bench:
  #   name: Benchmarks
  #   runs-on: ubuntu-latest
  #   steps:
  #     - uses: actions/checkout@v3
  #     - uses: dtolnay/rust-toolchain@stable
  #       with:
  #         toolchain: stable
  #     # These dependencies are required for `clipboard`
  #     - run: sudo apt-get install -y -qq libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev
  #     - run: cargo bench
