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

Run Ormolu on all files instead of changed

This commit is contained in:
Nicolas Mattia 2020-07-23 16:10:30 +02:00
parent fc2cd34b83
commit 1edb6856ad

View File

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