Skip to content

Instantly share code, notes, and snippets.

@ssaavedra
Last active Sep 19, 2018
Embed
What would you like to do?
Setup a repo pre-commit hook to use scalafmt
#!/bin/sh
# Licensed under CC0 in jurisdictions where this is not directly in the Public Domain
# Authored by Santiago Saavedra (github.com/ssaavedra)
git scalafmt --test || exit 1
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
#!/bin/bash
# Licensed under CC0 in jurisdictions where this is not directly in the Public Domain
# Authored by Santiago Saavedra (github.com/ssaavedra)
scalafmt="$(git rev-parse --git-path bin/scalafmt)"
download-scalafmt () {
if [[ -x "$scalafmt" ]]; then
echo "scalafmt is already downloaded"
return 0
fi
mkdir -p "$(git rev-parse --git-path bin)"
if which scalafmt >/dev/null 2>&1; then
echo "There is a scalafmt on the PATH. We will use that, but you can customize it at:"
echo "$scalafmt"
ln -s "$(which scalafmt)" "$scalafmt"
return 0
fi
if [[ $(uname -s) == "Linux" && $(uname -i) == "x86_64" ]]; then
echo "Downloading binary-image of scalafmt..."
curl -L -o "$scalafmt" https://github.com/ssaavedra/scalafmt/releases/download/v1.6.0-RC4-graalvm1/scalafmt-1.6.0-rc4-graalvm1-linux-x86_64-stripped-static
else
echo "Could not find a binary scalafmt available for your platform. Falling back to using sbt scalafmtCheck"
echo "#!/bin/bash\nexec sbt scalafmtCheck\n" > "$scalafmt"
fi
chmod +x "$scalafmt"
}
setup-hooks () {
ln -fsv "$PWD/_git-hooks/pre-commit" "$(git rev-parse --git-path hooks/pre-commit)"
}
setup-config-alias () {
git config --replace-all alias.scalafmt '! $(git rev-parse --git-path bin/scalafmt) -Xmn1g --git 1'
}
download-scalafmt
setup-hooks
setup-config-alias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment