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/.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/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/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 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