#!/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 --no-cabal --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 " - $i" 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