From b4f69b21cf7dc65c7b6364e7851b35653867a8b7 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 20 Sep 2023 07:15:33 -0600 Subject: [PATCH] Extract isort and black --- isort-and-black.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ would-reformat.sh | 17 +++++------------ 2 files changed, 48 insertions(+), 12 deletions(-) create mode 100755 isort-and-black.sh diff --git a/isort-and-black.sh b/isort-and-black.sh new file mode 100755 index 0000000..4131f5f --- /dev/null +++ b/isort-and-black.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +set -uo pipefail +IFS=$'\n\t' + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +source "$DIR/_reformat-common.bash" + +file="$1" + +if [[ x"$WOULD_REFORMAT" = x"would_reformat" ]] ; then + out1=$(pipx run isort $file 2>/dev/null) + c1="$?" + + if [[ x"$c1" != x"0" ]] ; then + # would reorder imports + if [[ x"$c1" = x"1" ]] ; then + exit 1 + fi + # syntax error; 123 is normalized to 2 + if [[ x"$c1" = x"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="$?" + + exit "$c2" +fi + +if [[ x"$WOULD_REFORMAT" = x"do_reformat" ]] ; then + out1=$(pipx run black $1 2>/dev/null && pipx run isort $file 2>/dev/null) + c="$?" + + exit "$c" +fi + +exit 255 + diff --git a/would-reformat.sh b/would-reformat.sh index 657daf6..f24d7ae 100755 --- a/would-reformat.sh +++ b/would-reformat.sh @@ -19,6 +19,8 @@ file_type=$(sniff_file_type $file) wrflog got file_type $file_type +WOULD_REFORMAT=would_reformat + if [[ $file_type = "javascript" || \ $file_type = "vue" || \ $file_type = "typescript" || \ @@ -39,20 +41,11 @@ wrflog "prettier: $(ls -l $(wfroot $DIR)/./prettier.sh)" would_reformat_retval=1 syntax_error_retval=2 elif [[ $file_type == "python" ]]; then - - out=$(pipx run isort --check $file 2>/dev/null) - isort_retval="$?" - + out=$($(wfroot $DIR)/./isort-and-black.sh $file 2>&1 > /dev/null) + retval="$?" success_retval=0 would_reformat_retval=1 - syntax_error_retval=123 - - if [[ x"$isort_retval" = x"0" ]] ; then - out=$(pipx run black --check $file 2>/dev/null) - retval="$?" - else - retval="$isort_retval" - fi + syntax_error_retval=2 elif [[ $file_type == "golang" ]] ; then success_retval=0 would_reformat_retval=1