From 76705c213a042a6e5e0e2c49c48af540f8872615 Mon Sep 17 00:00:00 2001 From: yanganto Date: Wed, 8 Jan 2020 13:04:50 +0800 Subject: [PATCH 1/2] trival: add pre-commit git hook & bootstrap script - add pre-commit git hook - add bootstrap script to help people setting up --- .hooks/pre-commit | 37 +++++++++++++++++++++++++++++++++++++ README.adoc | 11 +++++++++++ scripts/bootstrap.sh | 19 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100755 .hooks/pre-commit create mode 100755 scripts/bootstrap.sh diff --git a/.hooks/pre-commit b/.hooks/pre-commit new file mode 100755 index 000000000..151de953c --- /dev/null +++ b/.hooks/pre-commit @@ -0,0 +1,37 @@ +#!/bin/sh + +export PATH=$PATH:$HOME/.cargo/bin + +rustfmt --version &>/dev/null +if [ $? != 0 ]; then + printf "[pre_commit] [ERROR] \"rustfmt\" not available. \n" + printf "[pre_commit] [ERROR] rustfmt can be installed via - \n" + printf "[pre_commit] $ rustup component add rustfmt \n" + exit 1 +fi + + +problem_files=() + +# first collect all the files that need reformatting +for file in $(git diff --name-only --cached); do + if [ ${file: -3} == ".rs" ]; then + rustfmt --check $file + if [ $? != 0 ]; then + problem_files+=($file) + fi + fi +done + +if [ ${#problem_files[@]} != 0 ]; then + printf "[pre_commit] [ERROR] Plaese format the files via -\n" + printf "[pre_commit] $ cargo fmt --all \n" + printf "[pre_commit] [ERROR] If you want to keep your format with special reason, \n" + printf "[pre_commit] [ERROR] you can use this macro. \n" + printf "[pre_commit] #[rustfmt::skip] \n" + exit 1 +fi + +printf "[pre_commit] [SUCCESS] rustfmt ok \n" + +exit 0 diff --git a/README.adoc b/README.adoc index 21b675824..b12cba122 100644 --- a/README.adoc +++ b/README.adoc @@ -339,6 +339,17 @@ node-cli, node-executor, node-primitives, node-rpc, node-rpc-client, node-runtim == Contributing +=== Environment + +If you are using Ubuntu, +you may use the `scripts/bootstrap.sh` to set up your develop environment. +In this script, the nightly `Rust`, `cargo`, `rustfmt` will be installed, +the git hooks will be set, and ready to code. +We will appreciate your contribution. + +If you are using different environment, you may copy the git hooks mannually. +`$ cp .hooks/* .git/hooks` + === Contributing Guidelines link:CONTRIBUTING.adoc[CONTRIBUTING.adoc] diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh new file mode 100755 index 000000000..b98165b5c --- /dev/null +++ b/scripts/bootstrap.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# The script help you set up your develop envirnment + +# Setup git hooks +cp .hooks/* .git/hooks + +# Install nightly Rust +curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=nightly -y + +# Install wasm toolchain +rustup target add wasm32-unknown-unknown + +# Install rustfmt for coding style checking +rustup component add rustfmt --toolchain nightly + +# TODO: help other developers with different platform +sudo apt-get -y update +sudo apt-get install -y cmake pkg-config libssl-dev From 50bebdefb0c1cd1e4bc872f36c15f026c9e7d252 Mon Sep 17 00:00:00 2001 From: yanganto Date: Wed, 8 Jan 2020 15:04:21 +0800 Subject: [PATCH 2/2] Add rustfmt test in ci - test rustfmt before running test --- .travis.yml | 3 ++- ci/script.sh | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 88fcf3939..8cbe38a39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ env: global: - RUST_BACKTRACE=1 matrix: + - RUST_TOOLCHAIN=nightly TARGET=rustfmt - RUST_TOOLCHAIN=nightly TARGET=wasm - RUST_TOOLCHAIN=nightly TARGET=native @@ -26,4 +27,4 @@ script: after_script: # Check how much free disk space left after the build - - df -h \ No newline at end of file + - df -h diff --git a/ci/script.sh b/ci/script.sh index 3b902f0e0..edd0d7cf5 100755 --- a/ci/script.sh +++ b/ci/script.sh @@ -16,10 +16,13 @@ cargo --version rustc --version case $TARGET in - "native") + "rustfmt") sudo apt-get -y update sudo apt-get install -y cmake pkg-config libssl-dev + cargo fmt --all + ;; + "native") # Unit test cargo test --release --all --locked "$@" ;; @@ -28,4 +31,4 @@ case $TARGET in # Build test cargo build --locked "$@" ;; -esac \ No newline at end of file +esac