Skip to content

Commit

Permalink
Add pre-commit git hook & bootstrap script (#233)
Browse files Browse the repository at this point in the history
Add pre-commit git hook & bootstrap script
  • Loading branch information
AurevoirXavier authored Jan 8, 2020
2 parents a3b17fd + 50bebde commit 9a08671
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
37 changes: 37 additions & 0 deletions .hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -26,4 +27,4 @@ script:

after_script:
# Check how much free disk space left after the build
- df -h
- df -h
11 changes: 11 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
;;
Expand All @@ -28,4 +31,4 @@ case $TARGET in
# Build test
cargo build --locked "$@"
;;
esac
esac
19 changes: 19 additions & 0 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9a08671

Please sign in to comment.