|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Help message |
| 5 | +show_help() { |
| 6 | + local script_name |
| 7 | + script_name=$(basename "$0") |
| 8 | + echo "Fourmolu formatting Script. |
| 9 | +
|
| 10 | +Usage: |
| 11 | + $script_name diff Format the current git diff. |
| 12 | + $script_name full Format all source files. |
| 13 | + $script_name check Check the formatting of the source files. |
| 14 | + $script_name (-h | --help) |
| 15 | +
|
| 16 | +Options: |
| 17 | + -h --help Show this screen." |
| 18 | +} |
| 19 | + |
| 20 | +# Main script logic |
| 21 | +if [[ "$#" -eq 0 || "$1" == "-h" || "$1" == "--help" ]]; then |
| 22 | + show_help |
| 23 | + exit 0 |
| 24 | +fi |
| 25 | + |
| 26 | +exclude_files=( |
| 27 | + "src/Protocols/Cpp.hs" |
| 28 | +) |
| 29 | + |
| 30 | +ghc_opts=( |
| 31 | + "-XTemplateHaskell" |
| 32 | +) |
| 33 | + |
| 34 | +ghc_opt_flag="" |
| 35 | +for opt in "${ghc_opts[@]}"; do |
| 36 | + ghc_opt_flag+="-o ${opt} " |
| 37 | +done |
| 38 | + |
| 39 | +# Make sure it doesn't matter from where this script is executed |
| 40 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 41 | +cd $DIR |
| 42 | + |
| 43 | +if [ $1 == "diff" ] ; then |
| 44 | + src_files=$(git diff --cached --name-only --diff-filter=ACMR -- '*.hs') |
| 45 | +else |
| 46 | + src_files=$(find . -type f -name "*.hs") |
| 47 | +fi |
| 48 | + |
| 49 | +src_files_str=$(printf "%s\n" "${src_files[@]}" | sed 's| |\\ |g') |
| 50 | +exclude_files_str=$(printf "%s\n" "${exclude_files[@]}" | sed 's| |\\ |g') |
| 51 | +src_files=$(echo "$src_files_str" | grep -vwE "$exclude_files_str") |
| 52 | + |
| 53 | +if [ -z "$src_files" ]; then |
| 54 | + exit 0; |
| 55 | +fi |
| 56 | + |
| 57 | +if [[ "$1" == "diff" || "$1" == "full" ]] ; then |
| 58 | + fourmolu --mode inplace $src_files |
| 59 | +elif [[ "$1" == "check" ]] ; then |
| 60 | + fourmolu $ghc_opt_flag --mode check $src_files |
| 61 | +else |
| 62 | + echo "Expected a single argument, \"full\", \"diff\", \"check\" or \"--help\"" >&2 |
| 63 | + exit 3 |
| 64 | +fi |
0 commit comments