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

63 lines
1.3 KiB
Plaintext
Executable File

#!/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
###
### 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
echo "Please run ./script/fmt"
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