utils/shfmt.sh: success with auto-fixable changes on brew style --fix
This commit is contained in:
parent
32a3c89eef
commit
735eb78601
@ -325,6 +325,12 @@ align_multiline_switch_cases() {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Return codes:
|
||||||
|
# 0: success, good styles
|
||||||
|
# 1: file system permission errors
|
||||||
|
# 2: shfmt failed
|
||||||
|
# 3: forbidden styles detected
|
||||||
|
# 4: bad styles but can be auto-fixed
|
||||||
format() {
|
format() {
|
||||||
local file="$1"
|
local file="$1"
|
||||||
local tempfile
|
local tempfile
|
||||||
@ -348,11 +354,11 @@ format() {
|
|||||||
if ! "${SHFMT}" -w "${SHFMT_ARGS[@]}" "${tempfile}"
|
if ! "${SHFMT}" -w "${SHFMT_ARGS[@]}" "${tempfile}"
|
||||||
then
|
then
|
||||||
onoe "Failed to run \`shfmt\` for file \"${file}\"."
|
onoe "Failed to run \`shfmt\` for file \"${file}\"."
|
||||||
return 1
|
return 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Fail fast when forbidden styles detected
|
# Fail fast when forbidden styles detected
|
||||||
! no_forbidden_styles "${file}" "${tempfile}" && return 2
|
no_forbidden_styles "${file}" "${tempfile}" || return 3
|
||||||
|
|
||||||
# Tweak it with custom shell script styles
|
# Tweak it with custom shell script styles
|
||||||
wrap_then_do "${file}" "${tempfile}"
|
wrap_then_do "${file}" "${tempfile}"
|
||||||
@ -360,13 +366,14 @@ format() {
|
|||||||
|
|
||||||
if ! "${DIFF}" -q "${file}" "${tempfile}" &>/dev/null
|
if ! "${DIFF}" -q "${file}" "${tempfile}" &>/dev/null
|
||||||
then
|
then
|
||||||
# Show differences
|
|
||||||
"${DIFF}" "${DIFF_ARGS[@]}" "${file}" "${tempfile}"
|
|
||||||
if [[ -n "${INPLACE}" ]]
|
if [[ -n "${INPLACE}" ]]
|
||||||
then
|
then
|
||||||
cp -af "${tempfile}" "${file}"
|
cp -af "${tempfile}" "${file}"
|
||||||
|
else
|
||||||
|
# Show differences
|
||||||
|
"${DIFF}" "${DIFF_ARGS[@]}" "${file}" "${tempfile}"
|
||||||
fi
|
fi
|
||||||
return 2
|
return 4
|
||||||
else
|
else
|
||||||
# File is identical between code formations (good styling)
|
# File is identical between code formations (good styling)
|
||||||
return 0
|
return 0
|
||||||
@ -376,16 +383,31 @@ format() {
|
|||||||
RETCODE=0
|
RETCODE=0
|
||||||
for file in "${FILES[@]}"
|
for file in "${FILES[@]}"
|
||||||
do
|
do
|
||||||
format "${file}"
|
retcode=''
|
||||||
retcode="$?"
|
if [[ -n "${INPLACE}" ]]
|
||||||
|
then
|
||||||
|
INPLACE=1 format "${file}"
|
||||||
|
retcode="$?"
|
||||||
|
if [[ "${retcode}" == 4 ]]
|
||||||
|
then
|
||||||
|
onoe "${0##*/}: Bad styles detected in file \"${file}\", apply auto-fix."
|
||||||
|
retcode=''
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [[ -z "${retcode}" ]]
|
||||||
|
then
|
||||||
|
INPLACE='' format "${file}"
|
||||||
|
retcode="$?"
|
||||||
|
fi
|
||||||
if [[ "${retcode}" != 0 ]]
|
if [[ "${retcode}" != 0 ]]
|
||||||
then
|
then
|
||||||
if [[ "${retcode}" == 1 ]]
|
case "${retcode}" in
|
||||||
then
|
1) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code 1 (permission error)." ;;
|
||||||
onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code 1."
|
2) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code 2 (\`shfmt\` failed)." ;;
|
||||||
else
|
3) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code 3 (forbidden styles detected)." ;;
|
||||||
onoe "${0##*/}: Bad style for file \"${file}\". Formatter exited with code 2."
|
4) onoe "${0##*/}: Fixable bad styles detected in file \"${file}\", run \`brew style --fix\` to apply. Formatter exited with code 4." ;;
|
||||||
fi
|
*) onoe "${0##*/}: Failed to format file \"${file}\". Formatter exited with code ${retcode}." ;;
|
||||||
|
esac
|
||||||
onoe
|
onoe
|
||||||
RETCODE=1
|
RETCODE=1
|
||||||
fi
|
fi
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user