name: CI

on:
  push:
    branches: [main]
    paths:
      - "crates/broomva-cli/**"
  pull_request:
    branches: [main]
    paths:
      - "crates/broomva-cli/**"

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

defaults:
  run:
    working-directory: crates/broomva-cli

jobs:
  check:
    name: Check & Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: "1.88"
          components: clippy, rustfmt
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: crates/broomva-cli
      - run: cargo check
      - run: cargo clippy -- -D warnings
      - run: cargo fmt --all -- --check

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: "1.88"
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: crates/broomva-cli
      - run: cargo test

  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: broomva-linux-amd64
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact: broomva-macos-amd64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: broomva-macos-arm64
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: "1.88"
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: crates/broomva-cli
      - run: cargo build --release --target ${{ matrix.target }}
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: crates/broomva-cli/target/${{ matrix.target }}/release/broomva
