|
| 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 | +# use array so we can easily append |
| 27 | +# Ignore the clash-ghc/src-bin-* since they basically copies of upstream GHC code |
| 28 | +readarray -d '' exclude_files < <(find ./clash-ghc/src-bin-* -type f -name "*.hs" -print0) |
| 29 | + |
| 30 | +# Make sure it doesn't matter from where this script is executed |
| 31 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 32 | +cd $DIR |
| 33 | + |
| 34 | +if [ $1 == "diff" ] ; then |
| 35 | + src_files=$(git diff --cached --name-only --diff-filter=ACMR -- '*.hs') |
| 36 | +else |
| 37 | + src_files=$(find ./* -type f -name "*.hs") |
| 38 | +fi |
| 39 | + |
| 40 | +src_files_str=$(printf "%s\n" "${src_files[@]}" | sed 's| |\\ |g') |
| 41 | +exclude_files_str=$(printf "%s\n" "${exclude_files[@]}" | sed 's| |\\ |g') |
| 42 | +src_files=$(echo "$src_files_str" | grep -vwE "$exclude_files_str") |
| 43 | + |
| 44 | +if [ -z "$src_files" ]; then |
| 45 | + exit 0; |
| 46 | +fi |
| 47 | + |
| 48 | +if [[ "$1" == "diff" || "$1" == "full" ]] ; then |
| 49 | + fourmolu --mode inplace $src_files |
| 50 | +elif [[ "$1" == "check" ]] ; then |
| 51 | + fourmolu --mode check $src_files |
| 52 | +else |
| 53 | + echo "Expected a single argument, \"full\", \"diff\", \"check\" or \"--help\"" >&2 |
| 54 | + exit 3 |
| 55 | +fi |
0 commit comments