Files
would-reformat/would-reformat.sh
Michael Wolf 3793a11fbf Use set -e
This is annoying because a lot of times a command will "fail" even though
"failure" isn't an exceptional condition.
2023-09-30 17:21:23 -06:00

102 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
## This should be rarely if ever necessary, but here it is:
# source "$DIR/_wrflog.sh"
# wrflog before sourcing common funcs, I think i am in dir: $(pwd)
source "$DIR/_reformat-common.bash"
wrflog asdf asdf asdf
file="$1"
## fixme use realpath or something on "$file"
file_type=$(sniff_file_type "$file")
wrflog got file_type "$file_type"
export WOULD_REFORMAT=would_reformat
if [[ $file_type = "javascript" || \
$file_type = "vue" || \
$file_type = "typescript" || \
$file_type = "css" || \
$file_type = "php" || \
$file_type = "html" || \
$file_type = "jsx" || \
$file_type = "tsx" || \
$file_type == "css" || \
$file_type == "scss" || \
$file_type = "sh" ]]; then
wrflog will run thing
wrflog "$(wfroot "$DIR")"
wrflog "prettier: $(ls -l "$(wfroot "$DIR")"/./prettier.sh)"
out=$("$(wfroot "$DIR")"/./prettier.sh "$file" 2>&1 > /dev/null)
retval="$?"
success_retval=0
would_reformat_retval=1
syntax_error_retval=2
elif [[ $file_type == "python" ]]; then
out=$("$(wfroot "$DIR")"/./isort-and-black.sh "$file" 2>&1 > /dev/null)
retval="$?"
success_retval=0
would_reformat_retval=1
syntax_error_retval=2
elif [[ $file_type == "golang" ]] ; then
success_retval=0
would_reformat_retval=1
syntax_error_retval=2
out=$(gofmt -l "$file")
exitval="$?"
if [[ "$exitval" = "$syntax_error_retval" ]] ; then
retval="$exitval"
elif [[ "$out" = "$file" ]] ; then
retval="$would_reformat_retval"
else
# success
retval="$exitval"
fi
elif [[ $file_type == "dart" ]] ; then
success_retval=0
would_reformat_retval=1
# probably others too :(
syntax_error_retval=65
out=$(dart format -o none --set-exit-if-changed "$file")
exitval="$?"
retval="$exitval"
else
echo -n "ignoring"
exit 0
fi
if [[ "$retval" == "$success_retval" ]]; then
echo -n "would not change"
exit 0
elif [[ "$retval" == "$would_reformat_retval" ]]; then
echo -n "would change"
exit 1
elif [[ "$retval" == "$syntax_error_retval" ]]; then
echo -n "syntax error"
echo
echo "$out"
exit 2
else
echo -n "problem running reformatter"
exit 3
fi
echo -n "ok"
exit 0