utils/shfmt.sh: use utils/shfmt.sh as shell script formmatter

This commit is contained in:
XuehaiPan 2021-10-12 02:12:48 +08:00
parent 2537b8d76d
commit 0331cbeda3
2 changed files with 48 additions and 18 deletions

View File

@ -13,5 +13,9 @@
"--external-sources", "--external-sources",
"--source-path=${workspaceFolder}/Library" "--source-path=${workspaceFolder}/Library"
], ],
"shellformat.flag": "-i 2 -ci" "shellformat.effectLanguages": [
"shellscript"
],
"shellformat.path": "${workspaceFolder}/Library/Homebrew/utils/shfmt.sh",
"shellformat.flag": "-i 2 -ci -ln bash"
} }

View File

@ -35,6 +35,10 @@ then
if [[ -x "$(PATH="${HOMEBREW_PATH}" command -v diff)" ]] if [[ -x "$(PATH="${HOMEBREW_PATH}" command -v diff)" ]]
then then
DIFF="$(PATH="${HOMEBREW_PATH}" command -v diff)" # fall back to `diff` in PATH without coloring DIFF="$(PATH="${HOMEBREW_PATH}" command -v diff)" # fall back to `diff` in PATH without coloring
elif [[ -z "${HOMEBREW_PATH}" && -x "$(command -v diff)" ]]
then
# HOMEBREW_PATH may unset if shfmt.sh is called by vscode
DIFF="$(command -v diff)" # fall back to `diff` in PATH without coloring
else else
odie "${0##*/}: Please install diff by running \`brew install diffutils\`." odie "${0##*/}: Please install diff by running \`brew install diffutils\`."
fi fi
@ -81,9 +85,11 @@ do
done done
unset file unset file
STDIN=''
if [[ "${#FILES[@]}" == 0 ]] if [[ "${#FILES[@]}" == 0 ]]
then then
exit FILES=(/dev/stdin)
STDIN=1
fi fi
### ###
@ -334,27 +340,41 @@ align_multiline_switch_cases() {
format() { format() {
local file="$1" local file="$1"
local tempfile local tempfile
if [[ ! -f "${file}" || ! -r "${file}" ]]
then
onoe "File \"${file}\" is not readable."
return 1
fi
tempfile="$(dirname "${file}")/.${file##*/}.temp" if [[ -n "${STDIN}" ]]
then
tempfile="$(mktemp)"
else
if [[ ! -f "${file}" || ! -r "${file}" ]]
then
onoe "File \"${file}\" is not readable."
return 1
fi
tempfile="$(dirname "${file}")/.${file##*/}.temp"
cp -af "${file}" "${tempfile}"
fi
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
cp -af "${file}" "${tempfile}"
if [[ ! -f "${tempfile}" || ! -w "${tempfile}" ]]
then
onoe "File \"${tempfile}\" is not writable."
return 1
fi
# Format with `shfmt` first # Format with `shfmt` first
if ! "${SHFMT}" -w "${SHFMT_ARGS[@]}" "${tempfile}" if [[ -z "${STDIN}" ]]
then then
onoe "Failed to run \`shfmt\` for file \"${file}\"." if [[ ! -f "${tempfile}" || ! -w "${tempfile}" ]]
return 2 then
onoe "File \"${tempfile}\" is not writable."
return 1
fi
if ! "${SHFMT}" -w "${SHFMT_ARGS[@]}" "${tempfile}"
then
onoe "Failed to run \`shfmt\` for file \"${file}\"."
return 2
fi
else
if ! "${SHFMT}" "${SHFMT_ARGS[@]}" >"${tempfile}"
then
onoe "Failed to run \`shfmt\` for file \"${file}\"."
return 2
fi
fi fi
# Fail fast when forbidden styles detected # Fail fast when forbidden styles detected
@ -364,6 +384,12 @@ format() {
wrap_then_do "${file}" "${tempfile}" wrap_then_do "${file}" "${tempfile}"
align_multiline_switch_cases "${file}" "${tempfile}" align_multiline_switch_cases "${file}" "${tempfile}"
if [[ -n "${STDIN}" ]]
then
cat "${tempfile}"
return 0
fi
if ! "${DIFF}" -q "${file}" "${tempfile}" &>/dev/null if ! "${DIFF}" -q "${file}" "${tempfile}" &>/dev/null
then then
if [[ -n "${INPLACE}" ]] if [[ -n "${INPLACE}" ]]