name: CI

on:
  push:
    branches: [main, master]
  pull_request:

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo test

  lint:
    name: Clippy + fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy -- -D warnings
      - run: cargo fmt --check

  build-musl:
    name: Release (musl)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-unknown-linux-musl
      - uses: Swatinem/rust-cache@v2
      - run: sudo apt-get install -y musl-tools
      - run: cargo build --release --target x86_64-unknown-linux-musl
      - run: file target/x86_64-unknown-linux-musl/release/mftp
      - uses: actions/upload-artifact@v7
        with:
          name: mftp-x86_64-linux-musl
          path: target/x86_64-unknown-linux-musl/release/mftp

  build-macos:
    name: Release (macOS)
    runs-on: macos-latest
    strategy:
      matrix:
        target:
          - x86_64-apple-darwin
          - aarch64-apple-darwin
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target ${{ matrix.target }}
      - run: file target/${{ matrix.target }}/release/mftp
      - uses: actions/upload-artifact@v7
        with:
          name: mftp-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/mftp

  build-windows:
    name: Release (Windows)
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target x86_64-pc-windows-msvc
      - uses: actions/upload-artifact@v7
        with:
          name: mftp-x86_64-pc-windows-msvc
          path: target/x86_64-pc-windows-msvc/release/mftp.exe
