Files
would-reformat/would-reformat.sh
2023-09-20 10:11:01 -06:00

99 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
set -uo pipefail
IFS=$'\n\t'
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && 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