From d7bd9ef488959b3e080adff12cffc3b625ab5d3d Mon Sep 17 00:00:00 2001 From: jk Date: Tue, 13 Feb 2024 11:50:46 +0100 Subject: [PATCH] fix: run flakebox install again to fix generated `justfile`. Also few more generated git-hooks have been added. --- .config/flakebox/.gitignore | 1 + .config/flakebox/id | 2 +- .rustfmt.toml | 1 + flake.nix | 53 +++++++++------- justfile | 38 ++++++------ misc/git-hooks/commit-msg | 23 +++++++ misc/git-hooks/pre-commit | 116 ++++++++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+), 38 deletions(-) create mode 100644 .config/flakebox/.gitignore create mode 100755 misc/git-hooks/commit-msg create mode 100755 misc/git-hooks/pre-commit diff --git a/.config/flakebox/.gitignore b/.config/flakebox/.gitignore new file mode 100644 index 0000000..3fec32c --- /dev/null +++ b/.config/flakebox/.gitignore @@ -0,0 +1 @@ +tmp/ diff --git a/.config/flakebox/id b/.config/flakebox/id index 40312ed..33b0577 100644 --- a/.config/flakebox/id +++ b/.config/flakebox/id @@ -1 +1 @@ -7f694652756ea26511ac7b4509c7ba6b79c81856687e4d51fe93508f3c04e43bfe71f1eebebd3167395b4f9ed222b6dd93594998512e24720d6fd306386b128b +7d8d50aec3339e9569f6602d4d3295c251b3f7a10e974c86587f0f29737629015a8fdc7574dfd4f2c73206f98799a22fc2c06e0001930ea65d2dc102bb2b8ca5 diff --git a/.rustfmt.toml b/.rustfmt.toml index b32175c..35cd282 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -2,3 +2,4 @@ group_imports = "StdExternalCrate" wrap_comments = true format_code_in_doc_comments = true imports_granularity = "Module" +edition = "2021" diff --git a/flake.nix b/flake.nix index 7485db5..4ca9498 100644 --- a/flake.nix +++ b/flake.nix @@ -15,7 +15,19 @@ inherit system; config.allowUnfree = true; }; - flakeboxLib = flakebox.lib.${system} { }; + flakeboxLib = flakebox.lib.${system} { + config = { + # Avoid auto generation of flakebox' ci workflows + # @see https://github.com/rustshop/flakebox/blob/master/docs/nixos-options.md#configgithubciworkflows + github.ci.workflows = { }; + # don't check newlines + # @see https://github.com/rustshop/flakebox/blob/master/docs/nixos-options.md#configgitpre-committrailing_newline + git.pre-commit.trailing_newline = false; + # disable semgrep + # @see https://github.com/rustshop/flakebox/blob/master/docs/nixos-options.md#configsemgrepenable + semgrep.enable = false; + }; + }; rustSrc = flakeboxLib.filterSubPaths { root = builtins.path { name = "webimint"; @@ -41,28 +53,29 @@ pname = "flexbox-multibuild"; src = rustSrc; }); - in rec { + in + rec { workspaceDeps = craneLib.buildWorkspaceDepsOnly { }; workspaceBuild = craneLib.buildWorkspace { cargoArtifacts = workspaceDeps; }; webimint = craneLib.buildPackage { }; }); - in - { - legacyPackages = outputs; - devShells = flakeboxLib.mkShells { - toolchain = toolchainWasm; - packages = [ ]; - CC_wasm32_unknown_unknown = "${pkgs.llvmPackages_15.clang-unwrapped}/bin/clang-15"; - # -Wno-macro-redefined fixes ring building - CFLAGS_wasm32_unknown_unknown = "-I ${pkgs.llvmPackages_15.libclang.lib}/lib/clang/15.0.7/include/ -Wno-macro-redefined"; - nativeBuildInputs = with pkgs; [ - trunk - wasm-pack - wasm-bindgen-cli - nodejs - nodePackages.tailwindcss - ]; - }; - }); + in + { + legacyPackages = outputs; + devShells = flakeboxLib.mkShells { + toolchain = toolchainWasm; + packages = [ ]; + CC_wasm32_unknown_unknown = "${pkgs.llvmPackages_15.clang-unwrapped}/bin/clang-15"; + # -Wno-macro-redefined fixes ring building + CFLAGS_wasm32_unknown_unknown = "-I ${pkgs.llvmPackages_15.libclang.lib}/lib/clang/15.0.7/include/ -Wno-macro-redefined"; + nativeBuildInputs = with pkgs; [ + trunk + wasm-pack + wasm-bindgen-cli + nodejs + nodePackages.tailwindcss + ]; + }; + }); } diff --git a/justfile b/justfile index 7773dc8..ad4156c 100644 --- a/justfile +++ b/justfile @@ -1,5 +1,4 @@ # THIS FILE IS AUTOGENERATED FROM FLAKEBOX CONFIGURATION -!include justfile.fedimint.just alias b := build alias c := check @@ -12,23 +11,34 @@ default: # run `cargo build` on everything -build: +build *ARGS="--workspace --all-targets": #!/usr/bin/env bash set -euo pipefail if [ ! -f Cargo.toml ]; then cd {{invocation_directory()}} fi - cargo build --workspace --all-targets + cargo build {{ARGS}} # run `cargo check` on everything -check: +check *ARGS="--workspace --all-targets": #!/usr/bin/env bash set -euo pipefail if [ ! -f Cargo.toml ]; then cd {{invocation_directory()}} fi - cargo check --workspace --all-targets + cargo check {{ARGS}} + + +# run all checks recommended before opening a PR +final-check: lint clippy + #!/usr/bin/env bash + set -euo pipefail + if [ ! -f Cargo.toml ]; then + cd {{invocation_directory()}} + fi + cargo test --doc + just test # run code formatters @@ -60,28 +70,22 @@ test: build # run and restart on changes -watch: +watch *ARGS="-x run": #!/usr/bin/env bash set -euo pipefail if [ ! -f Cargo.toml ]; then cd {{invocation_directory()}} fi - env RUST_LOG=${RUST_LOG:-debug} cargo watch -x run + env RUST_LOG=${RUST_LOG:-debug} cargo watch {{ARGS}} # run `cargo clippy` on everything -clippy: - cargo clippy --locked --offline --workspace --all-targets -- --deny warnings --allow deprecated +clippy *ARGS="--locked --offline --workspace --all-targets": + cargo clippy {{ARGS}} -- --deny warnings --allow deprecated # run `cargo clippy --fix` on everything -clippy-fix: - cargo clippy --locked --offline --workspace --all-targets --fix - - -# run `semgrep` -semgrep: - env SEMGREP_ENABLE_VERSION_CHECK=0 \ - semgrep --error --no-rewrite-rule-ids --config .config/semgrep.yaml +clippy-fix *ARGS="--locked --offline --workspace --all-targets": + cargo clippy {{ARGS}} --fix # check typos diff --git a/misc/git-hooks/commit-msg b/misc/git-hooks/commit-msg new file mode 100755 index 0000000..b4fe09a --- /dev/null +++ b/misc/git-hooks/commit-msg @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +# Sanitize file first, by removing leading lines that are empty or start with a hash, +# as `convco` currently does not do it automatically (but git will) +# TODO: next release of convco should be able to do it automatically +MESSAGE="$( + while read -r line ; do + # skip any initial comments (possibly from previous run) + if [ -z "${body_detected:-}" ] && { [[ "$line" =~ ^#.*$ ]] || [ "$line" == "" ]; }; then + continue + fi + body_detected="true" + + echo "$line" + done < "$1" +)" + +# convco fails on fixup!, so remove fixup! prefix +MESSAGE="${MESSAGE#fixup! }" +if ! convco check --from-stdin <<<"$MESSAGE" ; then + >&2 echo "Please follow conventional commits(https://www.conventionalcommits.org)" + >&2 echo "Use git recommit to fix your commit" + exit 1 +fi diff --git a/misc/git-hooks/pre-commit b/misc/git-hooks/pre-commit new file mode 100755 index 0000000..0f08d56 --- /dev/null +++ b/misc/git-hooks/pre-commit @@ -0,0 +1,116 @@ +#!/usr/bin/env bash + +set -euo pipefail + +set +e +git diff-files --quiet +is_unclean=$? +set -e + +# Revert `git stash` on exit +function revert_git_stash { + >&2 echo "Unstashing uncommitted changes..." + git stash pop -q +} + +# Stash pending changes and revert them when script ends +if [ -z "${NO_STASH:-}" ] && [ $is_unclean -ne 0 ]; then + >&2 echo "Stashing uncommitted changes..." + GIT_LITERAL_PATHSPECS=0 git stash -q --keep-index + trap revert_git_stash EXIT +fi + +export FLAKEBOX_GIT_LS +FLAKEBOX_GIT_LS="$(git ls-files)" +export FLAKEBOX_GIT_LS_TEXT +FLAKEBOX_GIT_LS_TEXT="$(echo "$FLAKEBOX_GIT_LS" | grep -v -E "\.(png|ods|jpg|jpeg|woff2|keystore|wasm|ttf|jar|ico|gif)\$")" + + +function check_nothing() { + true +} +export -f check_nothing + +function check_cargo_fmt() { + set -euo pipefail + + flakebox-in-each-cargo-workspace cargo fmt --all --check + +} +export -f check_cargo_fmt + +function check_cargo_lock() { + set -euo pipefail + + # https://users.rust-lang.org/t/check-if-the-cargo-lock-is-up-to-date-without-building-anything/91048/5 + flakebox-in-each-cargo-workspace cargo update --workspace --locked + +} +export -f check_cargo_lock + +function check_leftover_dbg() { + set -euo pipefail + + errors="" + for path in $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep '.*\.rs'); do + if grep 'dbg!(' "$path" > /dev/null; then + >&2 echo "$path contains dbg! macro" + errors="true" + fi + done + + if [ -n "$errors" ]; then + >&2 echo "Fix the problems above or use --no-verify" 1>&2 + return 1 + fi + +} +export -f check_leftover_dbg + +function check_shellcheck() { + set -euo pipefail + + for path in $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep -E '.*\.sh$'); do + shellcheck --severity=warning "$path" + done + +} +export -f check_shellcheck + +function check_trailing_whitespace() { + set -euo pipefail + + rev="HEAD" + if ! git rev-parse -q 1>/dev/null HEAD 2>/dev/null ; then + >&2 echo "Warning: no commits yet, checking against --root" + rev="--root" + fi + if ! git diff --check $rev ; then + >&2 echo "Trailing whitespace detected. Please remove them before committing." + return 1 + fi + +} +export -f check_trailing_whitespace + +function check_typos() { + set -euo pipefail + + if ! echo "$FLAKEBOX_GIT_LS_TEXT" | typos --file-list - --force-exclude ; then + >&2 echo "Typos found: Valid new words can be added to '.typos.toml'" + return 1 + fi + +} +export -f check_typos + +parallel \ + --nonotice \ +::: \ + check_cargo_fmt \ + check_cargo_lock \ + check_leftover_dbg \ + check_shellcheck \ + check_trailing_whitespace \ + check_typos \ + check_nothing