#! /usr/bin/env bash
set -euo pipefail
TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
source "${TOP_DIR}/scripts/qbot_base.sh"
GIT_MODE=0
IS_CI="${IS_CI:-false}"
FORMAT_BAZEL=0
FORMAT_CPP=0
FORMAT_PYTHON=0
FORMAT_BASH=0
FORMAT_GO=0
FORMAT_ALL=0
SKIP_STR_NORM_PY=0
HAS_OPTION=0
function print_usage() {
echo -e "${RED}Usage${NO_COLOR}:
${BOLD}$0${NO_COLOR} --git ${BLUE}# Format changed files${NO_COLOR}
OR:
${BOLD}$0${NO_COLOR} [OPTION] <path/to/src/dir/or/files>"
echo -e "\n${RED}Options${NO_COLOR}:
${BLUE}-p|--python ${NO_COLOR}Format Python sources.
${BLUE}-B|--bazel ${NO_COLOR}Format Bazel files.
${BLUE}-c|--cpp ${NO_COLOR}Format C/C++/Proto sources.
${BLUE}-b|--bash ${NO_COLOR}Format Bash scripts.
${BLUE}-g|--go ${NO_COLOR}Format Go sources.
${BLUE}-S|skip-str-norm-py ${NO_COLOR}Don't normalize string quotes or prefixes for Python
${BLUE}-a|--all ${NO_COLOR}Format all.
${BLUE}-h|--help ${NO_COLOR}Show this message and exit."
}
function run_clang_format() {
bash "${TOP_DIR}/scripts/clang_format.sh" "$@"
}
function run_pyfmt() {
bash "${TOP_DIR}/scripts/pyfmt.sh" "$@"
}
function run_shfmt() {
bash "${TOP_DIR}/scripts/shfmt.sh" "$@"
}
function run_gofmt() {
bash "${TOP_DIR}/scripts/gofmt.sh" "$@"
}
function run_format() {
for arg in "$@"; do
if [[ -f "${arg}" ]]; then
if plain_py_ext "${arg}"; then
if [[ "${IS_CI}" == true || "${SKIP_STR_NORM_PY}" -eq 1 ]]; then
run_pyfmt -S "${arg}"
else
run_pyfmt "${arg}"
fi
elif bash_ext "${arg}"; then
run_shfmt "${arg}"
elif go_ext "${arg}"; then
run_gofmt "${arg}"
fi
elif [[ -d "${arg}" ]]; then
if [[ "${FORMAT_CPP}" -eq 1 ]]; then
run_clang_format "${arg}"
fi
if [[ "${FORMAT_PYTHON}" -eq 1 ]]; then
if [[ "${IS_CI}" == true || "${SKIP_STR_NORM_PY}" -eq 1 ]]; then
run_pyfmt -S "${arg}"
else
run_pyfmt "${arg}"
fi
fi
if [[ "${FORMAT_BASH}" -eq 1 ]]; then
run_shfmt "${arg}"
fi
if [[ "${FORMAT_GO}" -eq 1 ]]; then
run_gofmt "${arg}"
fi
else
warning "Ignored ${arg} as not a regular file/directory"
fi
done
}
function main() {
if [ "$#" -eq 0 ]; then
print_usage
exit 1
fi
while [[ $# -gt 0 ]]; do
local opt="$1"
case "${opt}" in
-p | --python)
FORMAT_PYTHON=1
HAS_OPTION=1
shift
;;
-c | --cpp)
FORMAT_CPP=1
HAS_OPTION=1
shift
;;
-B | --bazel)
FORMAT_BAZEL=1
HAS_OPTION=1
shift
;;
-b | --bash)
FORMAT_BASH=1
HAS_OPTION=1
shift
;;
-g | --go)
FORMAT_GO=1
HAS_OPTION=1
shift
;;
-S | --skip-str-norm-py)
SKIP_STR_NORM_PY=1
shift
;;
-a | --all)
FORMAT_ALL=1
shift
;;
--git)
GIT_MODE=1
FORMAT_ALL=1
shift
;;
-h | --help)
print_usage
exit 1
;;
*)
if [[ "${opt}" == -* ]]; then
print_usage
exit 1
else
if [[ "$HAS_OPTION" -eq 0 ]]; then
FORMAT_ALL=1
fi
break
fi
;;
esac
done
if [ "${FORMAT_ALL}" -eq 1 ]; then
FORMAT_BAZEL=1
FORMAT_CPP=1
FORMAT_BASH=1
FORMAT_GO=1
FORMAT_PYTHON=1
fi
if [[ "${GIT_MODE}" -eq 1 ]]; then
local what_to_diff
what_to_diff="${CI_MERGE_REQUEST_DIFF_BASE_SHA:-HEAD~1}"
while IFS=\= read diff_file; do
diff_files+=("$diff_file")
done < <(git diff --ignore-submodules --diff-filter=d --name-only "${what_to_diff}")
for one_change in "${diff_files[@]}"; do
run_format "${TOP_DIR}/${one_change}"
done
bash "${TOP_DIR}/scripts/format_extra_check.sh"
else
run_format "$@"
fi
}
main "$@"