提交 678cc1cd 编写于 作者: S storypku 提交者: Liu Jiaming

Scripts: improved apollo_format.sh

上级 fe977119
......@@ -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] <path/to/src/dir/or/files>"
......@@ -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 "$@"
......@@ -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
}
......
......@@ -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}
......
......@@ -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
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册