From d63e34304daa5467e7a53a87e68624a55dadda69 Mon Sep 17 00:00:00 2001 From: fryorcraken <110212804+fryorcraken@users.noreply.github.com> Date: Tue, 20 Aug 2024 15:14:35 +1000 Subject: [PATCH] chore: lock in nph version and add pre-commit hook (#2938) --- .gitmodules | 7 ++++++- Makefile | 35 +++++++++++++++++++++++++++++--- README.md | 23 +++++++++++++++++++-- scripts/git_pre_commit_format.sh | 16 +++++++++++++++ vendor/nph | 1 + 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 scripts/git_pre_commit_format.sh create mode 160000 vendor/nph diff --git a/.gitmodules b/.gitmodules index a2bc64ffbb..5650701b4c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -173,4 +173,9 @@ ignore = untracked path = vendor/negentropy url = https://github.com/waku-org/negentropy.git - branch = master \ No newline at end of file + branch = master +[submodule "vendor/nph"] + ignore = untracked + branch = master + path = vendor/nph + url = https://github.com/arnetheduck/nph.git diff --git a/Makefile b/Makefile index 7a6cb3af65..5393567f9b 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ BUILD_SYSTEM_DIR := vendor/nimbus-build-system EXCLUDED_NIM_PACKAGES := vendor/nim-dnsdisc/vendor LINK_PCRE := 0 LOG_LEVEL := TRACE - +NPH := vendor/nph/src/nph +FORMAT_MSG := "\\x1B[95mFormatting:\\x1B[39m" # we don't want an error here, so we can handle things later, in the ".DEFAULT" target -include $(BUILD_SYSTEM_DIR)/makefiles/variables.mk @@ -98,7 +99,7 @@ ifeq (, $(shell which cargo)) endif anvil: rustup -ifeq (, $(shell which anvil)) +ifeq (, $(shell which anvil 2> /dev/null)) # Install Anvil if it's not installed ./scripts/install_anvil.sh endif @@ -241,6 +242,34 @@ networkmonitor: | build deps librln echo -e $(BUILD_MSG) "build/$@" && \ $(ENV_SCRIPT) nim networkmonitor $(NIM_PARAMS) waku.nims +############ +## Format ## +############ +.PHONY: build-nph clean-nph install-nph + +build-nph: +ifeq ("$(wildcard $(NPH))","") + $(ENV_SCRIPT) nim c vendor/nph/src/nph.nim +endif + +GIT_PRE_COMMIT_HOOK := .git/hooks/pre-commit + +install-nph: build-nph +ifeq ("$(wildcard $(GIT_PRE_COMMIT_HOOK))","") + cp ./scripts/git_pre_commit_format.sh $(GIT_PRE_COMMIT_HOOK) +else + echo "$(GIT_PRE_COMMIT_HOOK) already present, will NOT override" + exit 1 +endif + +nph/%: build-nph + echo -e $(FORMAT_MSG) "nph/$*" && \ + $(NPH) $* + +clean-nph: + rm -f $(NPH) + +clean: | clean-nph ################### ## Documentation ## @@ -429,4 +458,4 @@ negentropy: cp vendor/negentropy/cpp/libnegentropy.so ./ negentropy-clean: $(MAKE) -C vendor/negentropy/cpp clean && \ - rm libnegentropy.so \ No newline at end of file + rm libnegentropy.so diff --git a/README.md b/README.md index 5b0ac7d4c4..5fb8afc1e2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The nwaku repository implements Waku, and provides tools related to it. - Examples of Waku usage. - Various tests of above. -For more details see the [source code](waku/v2/README.md) +For more details see the [source code](waku/README.md) ## How to Build & Run @@ -53,7 +53,8 @@ If you encounter difficulties building the project on WSL, consider placing the #### Nim Runtime This repository is bundled with a Nim runtime that includes the necessary dependencies for the project. -Before you can utilise the runtime you'll need to build the project, as detailed in a previous section. This will generate a `vendor` directory containing various dependencies, including the `nimbus-build-system` which has the bundled nim runtime. +Before you can utilise the runtime you'll need to build the project, as detailed in a previous section. +This will generate a `vendor` directory containing various dependencies, including the `nimbus-build-system` which has the bundled nim runtime. After successfully building the project, you may bring the bundled runtime into scope by running: ```bash @@ -84,6 +85,24 @@ Binary will be created as `.bin` under the `build` d make test/tests/common/test_enr_builder.nim ``` +## Formatting + +Nim files are expected to be formatted using the [`nph`](https://github.com/arnetheduck/nph) version present in `vendor/nph`. + +You can easily format file with the `make nph/ file` command. +For example: + +``` +make nph/waku/waku_core.nim +``` + +A convenient git hook is provided to automatically format file at commit time. +Run the following command to install it: + +```shell +make install-nph +``` + ### Examples Examples can be found in the examples folder. diff --git a/scripts/git_pre_commit_format.sh b/scripts/git_pre_commit_format.sh new file mode 100644 index 0000000000..f52c365070 --- /dev/null +++ b/scripts/git_pre_commit_format.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +echo "Running pre-commit hook" + +# Regexp for grep to only choose some file extensions for formatting +exts="\.\(nim\|nims\)$" + +# Build nph lazily +make build-nph || (1>&2 echo "failed to build nph. Pre-commit formatting will not be done."; exit 0) + +# Format staged files +git diff --cached --name-only --diff-filter=ACMR | grep "$exts" | while read file; do + echo "Formatting $file" + make nph/"$file" + git add "$file" +done diff --git a/vendor/nph b/vendor/nph new file mode 160000 index 0000000000..de5cd4823e --- /dev/null +++ b/vendor/nph @@ -0,0 +1 @@ +Subproject commit de5cd4823e63424adb58ef3717524348ae6c4d87