1
1
mirror of https://github.com/nmattia/niv.git synced 2024-12-01 15:56:03 +03:00
niv/script/fmt

57 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
###
### fmt - Format Haskell files with Ormolu
###
### Usage:
### fmt [-c|--check]
###
### Options:
### -c,--check Only check formatting, don't change files
set -euo pipefail
help() {
sed -rn 's/^### ?//;T;p' "$0"
}
fmt() {
local mode="$1"
command -v ormolu >/dev/null 2>&1 || {
echo >&2 "error: ormolu not found. run this in niv's nix-shell"
exit 1
}
needs_formatting=( )
for f in $(find . -name '*.hs')
do
echo "checking: $f"
if ! ormolu --mode "$mode" "$f"; then
needs_formatting+=( "$f" )
fi
done
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
}
if [ "$#" == "0" ]; then
fmt "inplace"
elif [ "$1" == "-c" ] || [ "$1" == "--check" ]; then
fmt "check"
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
help
exit 0
else
help
exit 1
fi