1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-18 19:07:19 +03:00

Add a formatting script

This way you can just do `script/fmt -i` during development, or use -c
in a CI job for the PR.

The help message is from: https://samizdat.dev/help-message-for-shell-scripts/
This commit is contained in:
Ben Sima 2020-07-08 11:59:20 -07:00 committed by Nicolas Mattia
parent 6bb2a97db7
commit 7572ac8ddb

51
script/fmt Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
###
### fmt — find changed Haskell files and format them with ormolu
###
### Usage:
### fmt [-c] [-i]
###
### Options:
### -i Format files inplace, will change files!
### -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