From 893ae1f63f926a66784a2046b51cdc17f0849f30 Mon Sep 17 00:00:00 2001 From: oxalica Date: Sun, 1 Jan 2023 05:36:52 +0800 Subject: [PATCH] Use pre-commit from Nix shell for CI style check --- .github/workflows/ci.yml | 32 ++++++-------------------------- flake.nix | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ddfad98..a66627b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,38 +7,18 @@ env: RUST_BACKTRACE: full jobs: - format: - name: Format + style: + name: Code style check runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: nightly - components: rustfmt - - name: Check format - run: cargo fmt --all -- --check - - clippy: - name: Clippy - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - # For builtins. - name: Install Nix uses: cachix/install-nix-action@v17 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - components: clippy - - name: Check clippy - run: cargo clippy --all -- -D warnings + - name: Prepare devShell + run: nix develop --command true + - name: Run pre-commit + run: nix develop --command pre-commit test: name: Test diff --git a/flake.nix b/flake.nix index edb3d32..1ae7132 100644 --- a/flake.nix +++ b/flake.nix @@ -38,15 +38,22 @@ pkgs = nixpkgs.legacyPackages.${system}; rustPkgs = rust-overlay.packages.${system}; + clippyFlags = lib.concatStringsSep " " [ + "-D" "warnings" + "-D" "clippy::dbg_macro" + ]; + pre-commit = pkgs.writeShellScriptBin "pre-commit" '' set -e die() { echo "$*" >&2; exit 1; } - cd "$(git rev-parse --show-toplevel)" - rg --fixed-strings 'dbg!' --glob '*.rs' \ - && die 'Found dbg!()' - cargo fmt --quiet --check >/dev/null \ + if git_dir="$(git rev-parse --show-toplevel)"; then + cd "$git_dir" + fi + cargo fmt --all --check \ || die 'Format failed' + cargo clippy --all --all-targets -- ${clippyFlags} \ + || die 'Clippy failed' ''; nil = pkgs.callPackage mkNil { }; @@ -79,6 +86,7 @@ RUST_BACKTRACE = "short"; NIXPKGS = nixpkgs; + CLIPPY_FLAGS = clippyFlags; # bash shellHook = ''