# SPDX-License-Identifier: EUPL-1.2
name: Cargo manifest lints

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
  RUSTFLAGS: "-D warnings -W unreachable-pub -W rust-2021-compatibility -C debuginfo=0"
  RUSTUP_MAX_RETRIES: 10
  RUST_BACKTRACE: short

on:
  workflow_dispatch:
  pull_request:
    paths:
      - '.gitea/**'
      - 'melib/Cargo.toml'
      - 'meli/Cargo.toml'
      - 'fuzz/Cargo.toml'
      - 'tool/Cargo.toml'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - '.cargo/config.toml'

jobs:
  manifest_lint:
    name: Lint Cargo manifests on ${{ matrix.build }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        build: [linux-amd64, ]
        include:
          - build: linux-amd64
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-gnu
    steps:
      - uses: actions/checkout@v3
      - id: os-deps
        name: install OS dependencies
        run: |
          apt-get update
          apt-get install -y mandoc
      - name: Find meli MSRV from meli/Cargo.toml.
        run: echo MELI_MSRV=$(grep -m1 rust-version meli/Cargo.toml | head -n1 | cut -d'"' -f 2 | head -n1) >> $GITHUB_ENV
      - id: rustup-setup
        name: Install Rustup and toolchains
        shell: bash
        run: |
          if ! command -v rustup &>/dev/null; then
            curl --proto '=https' --tlsv1.2 --retry 10 --retry-connrefused --location --silent --show-error --fail "https://sh.rustup.rs" | sh -s -- --default-toolchain none -y
            source "${HOME}/.cargo/env"
            echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH
            echo "CARGO_HOME=${CARGO_HOME:-$HOME/.cargo}" >> $GITHUB_ENV
            rustup toolchain install --profile minimal --component "rustfmt" --target "${{ matrix.target }}" -- "${{ env.MELI_MSRV }}"
            rustup component add rustfmt --toolchain ${{ env.MELI_MSRV }}-${{ matrix.target }}
            rustup toolchain install --profile minimal --component "rustfmt" --target "${{ matrix.target }}" -- "${{ matrix.rust }}"
            rustup component add rustfmt --toolchain ${{ matrix.rust }}-${{ matrix.target }}
            rustup default ${{ matrix.rust }}
          fi
      - name: Setup Rust target
        run: |
          mkdir -p "${{ env.CARGO_HOME }}"
          cat << EOF > "${{ env.CARGO_HOME }}"/config.toml
          [build]
          target = "${{ matrix.target }}"
          EOF
      - if: ${{ steps.cache-cargo.outputs.cache-hit != 'true' }} && matrix.target
        name: Add manifest lint dependencies
        run: |
          source "${HOME}/.cargo/env"
          cargo install --quiet --version 1.0.9 --target "${{ matrix.target }}" cargo-sort
          cargo install --quiet --version 0.15.1 --target "${{ matrix.target }}" cargo-msrv
      - name: cargo-msrv verify melib MSRV
        if: success() || failure()
        run: |
          source "${HOME}/.cargo/env"
          cargo-msrv --output-format json --log-level trace --log-target stdout --path meli verify
          cargo-msrv --output-format json --log-level trace --log-target stdout --path melib verify
      - name: cargo-sort
        if: success() || failure()
        run: |
          source "${HOME}/.cargo/env"
          cargo-sort --check --check-format --grouped --order package,bin,lib,dependencies,features,build-dependencies,dev-dependencies,workspace fuzz
          cargo-sort --check --check-format --grouped --order package,bin,lib,dependencies,features,build-dependencies,dev-dependencies,workspace tools
          cargo-sort --check --check-format --grouped --order package,bin,lib,dependencies,features,build-dependencies,dev-dependencies,workspace --workspace
      - name: Check debian/changelog is up-to-date.
        if: success() || failure()
        run: |
          ./scripts/check_debian_changelog.sh
