name: CI

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

env:
  CARGO_TERM_COLOR: always
  RELEASE_BIN: editorconfiger

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - build: linux-x64
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-musl
          - build: linux-aarch64
            os: ubuntu-latest
            rust: stable
            target: aarch64-unknown-linux-musl
          - build: macos-x64
            os: macos-latest
            rust: stable
            target: x86_64-apple-darwin
          - build: macos-arm64
            os: macos-latest
            rust: stable
            target: aarch64-apple-darwin
          - build: windows
            os: windows-latest
            rust: stable
            target: x86_64-pc-windows-msvc

    steps:
      - name: Update rust
        if: matrix.build == 'linux-x64'
        run: rustup update
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Build
        uses: houseabsolute/actions-rust-cross@v1
        with:
          command: "build"
          target: ${{ matrix.target }}
          toolchain: ${{ matrix.rust }}
          args: '--release --bin editorconfiger --features="build-binary"'
          strip: true
      - name: Build Debian package
        if: matrix.build == 'linux-x64' || matrix.build == 'linux-aarch64'
        run: |
          cargo install cargo-deb
          [[ -d ./target/release/ ]] || mkdir ./target/release/
          [[ -f ./target/release/${{ env.RELEASE_BIN }} ]] || cp ./target/${{ matrix.target }}/release/${{ env.RELEASE_BIN }} ./target/release/
          cargo deb --no-build --no-strip --target ${{ matrix.target }}
          ls -lah ./target/${{ matrix.target }}/debian/
      - name: Build RPM package
        if: matrix.build == 'linux-x64' || matrix.build == 'linux-aarch64'
        run: |
          cargo install cargo-generate-rpm
          cargo generate-rpm --target ${{ matrix.target }}
          ls -lah ./target/${{ matrix.target }}/generate-rpm/

      - name: Install cargo-llvm-cov
        if: matrix.build == 'linux-x64'
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Generate code coverage
        if: matrix.build == 'linux-x64'
        run: cargo llvm-cov --lcov --output-path lcov.info

      - name: Upload to Codecov
        if: matrix.build == 'linux-x64'
        uses: codecov/codecov-action@v6
        with:
          files: lcov.info
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Run tests
        if: matrix.build != 'linux-x64'
        uses: houseabsolute/actions-rust-cross@v1
        with:
          command: "test"
          target: ${{ matrix.target }}
          toolchain: ${{ matrix.rust }}
          args: "--release"
      - name: Run linter
        if: matrix.build == 'linux-x64'
        uses: auguwu/clippy-action@1.5.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          check-args: --all-features --bin editorconfiger --release
          args: -Dwarnings
      - name: Run audit
        if: matrix.build == 'linux-x64'
        uses: rustsec/audit-check@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
