Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit git hook & bootstrap script #233

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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