Skip to content

Commit

Permalink
Add an init script of precommit hook with scalafmt. (#177)
Browse files Browse the repository at this point in the history
* Add an init script of precommit hook with scalafmt.
* Readme update about scalafmt.
  • Loading branch information
eryshev authored Oct 2, 2017
1 parent a5dbbd2 commit b4548d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Development

To work on the Scala API, run sbt using `sbt -DdevMode` (the devMode
To work on the Scala API, run sbt using `sbt -DdevMode=true` (the devMode
option allows to skip webpack execution to speed up execution). You can
use `~compile` or `~test` on the root project or focus on a specific
project using `~timeseries/test`.
Expand Down Expand Up @@ -33,15 +33,23 @@ Use `yarn format` to format the Javascript files.

### Scalafmt

We use Scalafmt to enforce style. The minimal config is found in the
We use Scalafmt to enforce style. The minimal config is found in the
.scalafmt.conf file (you probably won't make any friends if you change
this).

To use you can install the [IntelliJ
Several ways to use it:

1. We provide a `git-init-scalafmt-precommit-hook.sh` that **replaces you git precommit hook** by a Scalafmt check.

__Note__:
- It works only in UNIX like systems with bash installed.
- [You need a Scalafmt installed in your PATH in CLI mode](http://scalameta.org/scalafmt/#CLI).

2. You can install the [IntelliJ
plugin](http://scalameta.org/scalafmt/#IntelliJ) and use the familiar
shift-ctrl-L shortcut.

You can also use the sbt plugin:
3. You can also use the sbt plugin:
```
$> sbt "scalafmt -i"
```
Expand Down
18 changes: 18 additions & 0 deletions git-init-scalafmt-precommit-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# This script creates a pre-commit git hook that run scalafmt before each commit
# http://scalameta.org/scalafmt/
# Need scalafmt installed in CLI mode.
# Tested in Ubuntu's bash.
[ -d .git/hooks/ ] || (echo "It's not a git directory";exit 1)
[ -d .git/hooks/ ] && echo '#!/usr/bin/env bash
echo -e "\e[0;33m Scalafmt RUNNING \e[0m"
scalafmt --git true --diff-branch $(git branch | grep \* | cut -d " " -f2)
RESULT=$?
if [ ${RESULT} -ne 0 ]; then
echo -e "\e[0;31m Scalafmt FAILED \e[0m"
exit ${RESULT}
fi
echo -e "\e[0;32m Scalafmt SUCCEEDED \e[0m"
exit 0
' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

0 comments on commit b4548d0

Please sign in to comment.