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,6 +340,11 @@ align_multiline_switch_cases() {
format() { format() {
local file="$1" local file="$1"
local tempfile local tempfile
if [[ -n "${STDIN}" ]]
then
tempfile="$(mktemp)"
else
if [[ ! -f "${file}" || ! -r "${file}" ]] if [[ ! -f "${file}" || ! -r "${file}" ]]
then then
onoe "File \"${file}\" is not readable." onoe "File \"${file}\" is not readable."
@ -341,21 +352,30 @@ format() {
fi fi
tempfile="$(dirname "${file}")/.${file##*/}.temp" tempfile="$(dirname "${file}")/.${file##*/}.temp"
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
cp -af "${file}" "${tempfile}" cp -af "${file}" "${tempfile}"
fi
trap 'rm -f "${tempfile}" 2>/dev/null' RETURN
# Format with `shfmt` first
if [[ -z "${STDIN}" ]]
then
if [[ ! -f "${tempfile}" || ! -w "${tempfile}" ]] if [[ ! -f "${tempfile}" || ! -w "${tempfile}" ]]
then then
onoe "File \"${tempfile}\" is not writable." onoe "File \"${tempfile}\" is not writable."
return 1 return 1
fi fi
# Format with `shfmt` first
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 2 return 2
fi fi
else
if ! "${SHFMT}" "${SHFMT_ARGS[@]}" >"${tempfile}"
then
onoe "Failed to run \`shfmt\` for file \"${file}\"."
return 2
fi
fi
# Fail fast when forbidden styles detected # Fail fast when forbidden styles detected
no_forbidden_styles "${file}" "${tempfile}" || return 3 no_forbidden_styles "${file}" "${tempfile}" || return 3
@ -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}" ]]