Carp/scripts/format.sh
Tim Dévé 1863c82559
chore: Creates pre-commit hook to format Haskell code (#1047)
* chore: moves git hook into own folder

This is is done in preparation of having other git hooks

* chore: Creates script to run formatter

* chore: Checks that ormolu is installed before running it

* chore: Adds pre-commit hook to format Haskell files

* chore: Ensures the ormolu instruction are outputted when running hook

* chore: Prints formatted files on newlines
2020-12-05 14:26:28 +01:00

39 lines
953 B
Bash
Executable File

#!/bin/sh
set -eu
echoerr() {
echo "$@" >&2
}
check_ormolu_installed() {
if ! command -v ormolu >/dev/null 2>&1
then
echoerr "'ormolu' is needed to format Haskell files"
echoerr "On MacOS it is available in brew: 'brew install ormolu'"
echoerr "Otherwise it can be installed using stack: 'stack install ormolu'"
echoerr "The last command needs to be run outside of the repository"
exit 1
fi
}
print_on_newlines() {
echo "$@" | tr ' ' '\n'
}
repo_root=$(git rev-parse --show-toplevel)
if [ "${1-}" = "--only-changed" ]; then
cd "$repo_root"
changed_haskell_files=$(git diff --cached --name-only --diff-filter=ACMR "*.hs" | sed 's| |\\ |g')
[ -z "$changed_haskell_files" ] && exit 0
check_ormolu_installed
ormolu --mode inplace $changed_haskell_files
print_on_newlines $changed_haskell_files
else
check_ormolu_installed
cd "$repo_root"
ormolu --mode inplace ./**/*.hs
print_on_newlines ./**/*.hs
fi