2020-07-24 14:06:32 +03:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#!nix-shell -i bash
|
|
|
|
#!nix-shell -I nixpkgs=./nix
|
|
|
|
#!nix-shell -p ormolu
|
|
|
|
#!nix-shell -p glibcLocales
|
|
|
|
#!nix-shell --keep GITHUB_TOKEN
|
2020-07-08 21:59:20 +03:00
|
|
|
###
|
2020-07-23 17:10:30 +03:00
|
|
|
### fmt - Format Haskell files with Ormolu
|
2020-07-08 21:59:20 +03:00
|
|
|
###
|
|
|
|
### Usage:
|
2020-07-23 17:10:30 +03:00
|
|
|
### fmt [-c|--check]
|
2020-07-08 21:59:20 +03:00
|
|
|
###
|
|
|
|
### Options:
|
2020-07-23 17:10:30 +03:00
|
|
|
### -c,--check Only check formatting, don't change files
|
|
|
|
|
|
|
|
set -euo pipefail
|
2020-07-08 21:59:20 +03:00
|
|
|
|
|
|
|
help() {
|
|
|
|
sed -rn 's/^### ?//;T;p' "$0"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt() {
|
2020-07-23 17:10:30 +03:00
|
|
|
local mode="$1"
|
2020-07-08 21:59:20 +03:00
|
|
|
command -v ormolu >/dev/null 2>&1 || {
|
|
|
|
echo >&2 "error: ormolu not found. run this in niv's nix-shell"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2020-07-23 17:10:30 +03:00
|
|
|
needs_formatting=( )
|
|
|
|
for f in $(find . -name '*.hs')
|
2020-07-08 21:59:20 +03:00
|
|
|
do
|
|
|
|
echo "checking: $f"
|
2020-07-23 17:10:30 +03:00
|
|
|
if ! ormolu --mode "$mode" "$f"; then
|
|
|
|
needs_formatting+=( "$f" )
|
2020-07-08 21:59:20 +03:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2020-07-23 17:10:30 +03:00
|
|
|
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
|
2020-07-24 14:06:32 +03:00
|
|
|
echo "Please run ./script/fmt"
|
2020-07-23 17:10:30 +03:00
|
|
|
fi
|
|
|
|
}
|
2020-07-08 21:59:20 +03:00
|
|
|
|
2020-07-23 17:10:30 +03:00
|
|
|
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
|