From 678cc1cd001ac862eda28a13dfb903f14e621295 Mon Sep 17 00:00:00 2001 From: storypku Date: Fri, 4 Sep 2020 16:30:40 +0800 Subject: [PATCH] Scripts: improved apollo_format.sh --- scripts/apollo_format.sh | 114 +++++++++++++++++++++++++-------------- scripts/autopep8.sh | 2 +- scripts/clang_format.sh | 3 +- scripts/shfmt.sh | 6 +-- 4 files changed, 79 insertions(+), 46 deletions(-) diff --git a/scripts/apollo_format.sh b/scripts/apollo_format.sh index 7ae9358c47..45cac3324d 100755 --- a/scripts/apollo_format.sh +++ b/scripts/apollo_format.sh @@ -25,6 +25,13 @@ set -e TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" source "${TOP_DIR}/scripts/apollo.bashrc" +FORMAT_BAZEL=0 +FORMAT_CPP=0 +FORMAT_MARKDOWN=0 +FORMAT_PYTHON=0 +FORMAT_SHELL=0 +FORMAT_ALL=0 + function print_usage() { echo -e "\n${RED}Usage${NO_COLOR}: .${BOLD}$0${NO_COLOR} [OPTION] " @@ -35,7 +42,7 @@ function print_usage() { ${BLUE}-s|--shell ${NO_COLOR}Format Shell code ${BLUE}-m|--markdown ${NO_COLOR}Format Markdown file ${BLUE}-a|--all ${NO_COLOR}Format all - ${BLUE}-h|--help ${NO_COLOR}Show this message" + ${BLUE}-h|--help ${NO_COLOR}Show this message and exit" } function run_clang_format() { @@ -58,51 +65,78 @@ function run_prettier() { bash "${TOP_DIR}/scripts/mdfmt.sh" "$@" } -function run_format_all() { - run_clang_format "$@" - run_buildifier "$@" - run_autopep8 "$@" - run_shfmt "$@" - run_prettier "$@" -} - function main() { if [ "$#" -eq 0 ]; then print_usage exit 1 fi - local option="$1" - shift - case "${option}" in - -p | --python) - run_autopep8 "$@" - ;; - -c | --cpp) - run_clang_format "$@" - ;; - -b | --bazel) - run_buildifier "$@" - ;; - -s | --shell) - run_shfmt "$@" - ;; - -m | --markdown) - run_prettier "$@" - ;; - -a | --all) - run_format_all "$@" - ;; - -h | --help) - print_usage - exit 1 - ;; - *) - echo "Unknown option: ${option}" - print_usage - exit 1 - ;; - esac + while [ $# -gt 0 ]; do + local opt="$1" + case "${opt}" in + -p | --python) + FORMAT_PYTHON=1 + shift + ;; + -c | --cpp) + FORMAT_CPP=1 + shift + ;; + -b | --bazel) + FORMAT_BAZEL=1 + shift + ;; + -s | --shell) + FORMAT_SHELL=1 + shift + ;; + -m | --markdown) + FORMAT_MARKDOWN=1 + shift + ;; + -a | --all) + FORMAT_ALL=1 + shift + ;; + -h | --help) + print_usage + exit 1 + ;; + *) + if [[ "${opt}" = -* ]]; then + print_usage + exit 1 + else + FORMAT_ALL=1 + break + fi + ;; + esac + done + + if [ "${FORMAT_ALL}" -eq 1 ]; then + FORMAT_BAZEL=1 + FORMAT_CPP=1 + FORMAT_MARKDOWN=1 + FORMAT_SHELL=1 + FORMAT_PYTHON=1 + fi + + if [ "${FORMAT_BAZEL}" -eq 1 ]; then + run_buildifier "$@" + fi + if [ "${FORMAT_CPP}" -eq 1 ]; then + run_clang_format "$@" + fi + if [ "${FORMAT_PYTHON}" -eq 1 ]; then + run_autopep8 "$@" + fi + if [ "${FORMAT_SHELL}" -eq 1 ]; then + run_shfmt "$@" + fi + if [ "${FORMAT_MARKDOWN}" -eq 1 ]; then + run_prettier "$@" + fi } main "$@" diff --git a/scripts/autopep8.sh b/scripts/autopep8.sh index 5d02fe012c..4ba5c6507b 100755 --- a/scripts/autopep8.sh +++ b/scripts/autopep8.sh @@ -71,7 +71,7 @@ function run_autopep8() { continue fi autopep8_run ${srcs} - ok "Done formatting Python source files under ${target}" + ok "Done formatting Python files under ${target}" fi done } diff --git a/scripts/clang_format.sh b/scripts/clang_format.sh index e72a1203de..f4a6e40c3c 100755 --- a/scripts/clang_format.sh +++ b/scripts/clang_format.sh @@ -57,8 +57,7 @@ function run_clang_format() { local srcs srcs="$(find_c_cpp_srcs ${target})" if [ -z "${srcs}" ]; then - warning "Do nothing. No c/c++/cuda header/source" \ - "files found under ${target} ." + ok "No need to format C/C++/CUDA files under ${target} as none found" continue fi clang_format_run ${srcs} diff --git a/scripts/shfmt.sh b/scripts/shfmt.sh index 4de2b20126..38c8c18c52 100755 --- a/scripts/shfmt.sh +++ b/scripts/shfmt.sh @@ -65,17 +65,17 @@ function run_shfmt() { shell_format_run "${target}" info "Done formatting ${target}" else - warning "Do nothing. ${target} is not a Shell file." + warning "Do nothing. ${target} is not a bash scripts." fi else local srcs srcs="$(_find_shell_srcs ${target})" if [ -z "${srcs}" ]; then - warning "Do nothing. No Shell files found under ${target} ." + ok "No need to format shell scripts under ${target} as none found" continue fi shell_format_run ${srcs} - ok "Done formatting Shell source files under ${target}" + ok "Done formatting shell scripts under ${target}" fi done } -- GitLab