Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Forked from 4z3/elm-format-wrapper.nix
Last active August 29, 2021 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Janiczek/10661c708219ba5a12632599eaffdc76 to your computer and use it in GitHub Desktop.
Save Janiczek/10661c708219ba5a12632599eaffdc76 to your computer and use it in GitHub Desktop.
elm-format-wrapper that allows to ignore blocks of code
#!/usr/bin/env bash
# elm-format-wrapper 1.0.0
#
# Usage:
# elm-format-wrapper [--help] [--action=ACTION] PATH...
#
# Description:
# Format paths like elm-format, but allow ignoring blocks of code:
#
# --elm-format-ignore-begin
# don't format code here...
# --elm-format-ignore-end
#
# Available options:
# --action=format (this is the default action)
# reformat each file
#
# --action=diff
# show patch that would be applied if action would be "format"
# instead of formating the files.
#
# -h, --help
# show this help :)
#
# --output=PATH
# --output PATH
# write formatted file to PATH.
# If multiple files are formatted, then the last one will end up in
# the output path. This option is mainly for compatibility with
# elm-format.
#
# --yes
# ignored; this option is for compatibility with elm-format
#
set -efu
help() {
gsed -rn '
0,/^$/{
/#!/d
s/^# ?//p
}
' "$0"
}
action=format
output=
for arg; do
shift
case $arg in
--action=*)
action=''${arg#*=}
case $action in
diff) continue;;
format) continue;;
esac
echo "error: bad action: $action" >&2
exit 1
;;
--output)
output=$1
shift
continue
;;
--output=*)
output=''${arg#*=}
continue
;;
--yes)
continue
;;
-h|--help)
help
exit
;;
-*)
echo "error: bad argument: $arg" >&2
help >&2
exit 1
esac
set -- "$@" "$arg"
done
action_format() {(
old=$1
new=$2
if test -n "$output"; then
cp "$new" "$output"
else
cp "$new" "$old"
fi
)}
action_diff() {(
old=$1
new=$2
diff -u --color "$old" "$new"
if test -n "$output"; then
cp "$new" "$output"
fi
)}
temp=$(mktemp -t elm-format-wrapper.XXXXXXXX)
clean() {
rm "$temp"
}
trap clean EXIT
yes n |
elm-format "$@" |
gsed -rn '
/^This will overwrite the following files to use Elm.s preferred style:$/,/^This cannot be undone!/{
s/^\s*(\S+)$/\1/p
}
' |
while IFS= read -r path; do
cat "$path" |
gsed -r '
/^--elm-format-ignore-begin$/,/^--elm-format-ignore-end$/{
s/^/--@/
}
' |
elm-format --stdin > "$temp"
gsed -ir '
/^--@--elm-format-ignore-begin$/,/^--@--elm-format-ignore-end$/{
s/^--@//
}
' "$temp"
action_"$action" "$path" "$temp"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment