Files
would-reformat/isort-and-black.sh
2023-09-30 17:30:15 -06:00

47 lines
907 B
Bash
Executable File

#!/bin/bash
set -uo pipefail
IFS=$'\n\t'
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/_reformat-common.bash"
file="$1"
if [[ "$WOULD_REFORMAT" = "would_reformat" ]]; then
out1=$(pipx run isort "$file" 2>/dev/null)
c1="$?"
if [[ "$c1" != "0" ]]; then
# would reorder imports
if [[ "$c1" = "1" ]]; then
exit 1
fi
# syntax error; 123 is normalized to 2
if [[ "$c1" = "123" ]]; then
exit 2
fi
# some other problem that we don't know how to handle
exit 255
fi
out2=$(pipx run black --check "$file" 2>/dev/null)
c2="$?"
printf '%s\n%s' "$out1" "$out2"
exit "$c2"
fi
if [[ "$WOULD_REFORMAT" = "do_reformat" ]]; then
out1=$(pipx run black "$1" 2>/dev/null && pipx run isort "$file" 2>/dev/null)
c="$?"
printf '%s\n' "$out1"
exit "$c"
fi
exit 255