1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-29 09:42:35 +03:00
niv/script/fmt

51 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
###
2020-07-22 21:22:21 +03:00
### fmt - find changed Haskell files and format them with ormolu
###
### Usage:
2020-07-22 21:22:21 +03:00
### fmt [-c]
###
### Options:
### -c Only check formatting, don't change files
### -h Print this message
help() {
sed -rn 's/^### ?//;T;p' "$0"
}
fmt() {
command -v ormolu >/dev/null 2>&1 || {
echo >&2 "error: ormolu not found. run this in niv's nix-shell"
exit 1
}
merge_base=$(git merge-base HEAD origin/master)
changed_files=$(git diff --name-only $merge_base *.hs)
for f in ${changed_files[@]}
do
echo "checking: $f"
ormolu --mode $mode $f
if [[ ! $? -eq 0 ]] && [[ "$mode" == "check" ]]
# ormolu silently fails. let's print an error message so the user know's
# what's going on
then
echo "need fmt: $f"
fi
done
echo "all files checked for formatting"
}
while getopts "ich" arg; do
case $arg in
i) mode="inplace" fmt
;;
c) mode="check" fmt
;;
*) help
exit
;;
esac
done