From 78f2862596d785a83908e5724e6145c901b1a617 Mon Sep 17 00:00:00 2001 From: storypku Date: Mon, 14 Sep 2020 09:48:41 +0800 Subject: [PATCH] Scripts: make "apollo_format.sh" smarter on files --- scripts/apollo.bashrc | 107 +++++++++++++++++++++++++++++++-------- scripts/apollo_format.sh | 52 +++++++++++++------ 2 files changed, 124 insertions(+), 35 deletions(-) diff --git a/scripts/apollo.bashrc b/scripts/apollo.bashrc index 442d118468b..ab4d46f704a 100644 --- a/scripts/apollo.bashrc +++ b/scripts/apollo.bashrc @@ -109,28 +109,25 @@ function determine_gpu_use_target() { } function file_ext() { - local __ext="${1##*.}" - if [ "${__ext}" == "$1" ]; then - __ext="" + local filename="$(basename $1)" + local actual_ext="${filename##*.}" + if [[ "${actual_ext}" == "${filename}" ]]; then + actual_ext="" fi - echo "${__ext}" + echo "${actual_ext}" } function c_family_ext() { - local __ext - __ext="$(file_ext $1)" + local actual_ext + actual_ext="$(file_ext $1)" for ext in "h" "hh" "hxx" "hpp" "cxx" "cc" "cpp" "cu"; do - if [ "${ext}" == "${__ext}" ]; then + if [[ "${ext}" == "${actual_ext}" ]]; then return 0 fi done return 1 } -function find_proto_srcs() { - find "$@" -type f -name "*.proto" -} - function find_c_cpp_srcs() { find "$@" -type f -name "*.h" \ -o -name "*.c" \ @@ -143,6 +140,76 @@ function find_c_cpp_srcs() { -o -name "*.cu" } +function proto_ext() { + if [[ "$(file_ext $1)" == "proto" ]]; then + return 0 + else + return 1 + fi +} + +function find_proto_srcs() { + find "$@" -type f -name "*.proto" +} + +function py_ext() { + if [[ "$(file_ext $1)" == "py" ]]; then + return 0 + else + return 1 + fi +} + +function find_py_srcs() { + find "$@" -type f -name "*.py" +} + +function bash_ext() { + local actual_ext + actual_ext="$(file_ext $1)" + for ext in "sh" "bash" "bashrc"; do + if [[ "${ext}" == "${actual_ext}" ]]; then + return 0 + fi + done + return 1 +} + +function bazel_extended() { + local actual_ext="$(file_ext $1)" + if [[ -z "${actual_ext}" ]]; then + if [[ "${arg}" == "BUILD" || "${arg}" == "WORKSPACE" ]]; then + return 0 + else + return 1 + fi + else + for ext in "BUILD" "bazel" "bzl"; do + if [[ "${ext}" == "${actual_ext}" ]]; then + return 0 + fi + done + return 1 + fi +} + +function prettier_ext() { + local actual_ext + actual_ext="$(file_ext $1)" + for ext in "md" "json" "yml"; do + if [[ "${ext}" == "${actual_ext}" ]]; then + return 0 + fi + done + return 1 +} + +function find_prettier_srcs() { + find "$@" -type f -name "*.md" \ + -or -name "*.json" \ + -or -name "*.yml" +} + ## Prevent multiple entries of my_bin_path in PATH function add_to_path() { if [ -z "$1" ]; then @@ -176,8 +243,8 @@ function run() { "${@}" || exit $? else local errfile="${APOLLO_ROOT_DIR}/.errors.log" - echo "${@}" > "${errfile}" - if ! "${@}" >> "${errfile}" 2>&1; then + echo "${@}" >"${errfile}" + if ! "${@}" >>"${errfile}" 2>&1; then local exitcode=$? cat "${errfile}" 1>&2 exit $exitcode @@ -187,22 +254,22 @@ function run() { #commit_id=$(git log -1 --pretty=%H) function git_sha1() { - if [ -x "$(which git 2> /dev/null)" ] \ - && [ -d "${APOLLO_ROOT_DIR}/.git" ]; then - git rev-parse --short HEAD 2> /dev/null || true + if [ -x "$(which git 2>/dev/null)" ] && + [ -d "${APOLLO_ROOT_DIR}/.git" ]; then + git rev-parse --short HEAD 2>/dev/null || true fi } function git_date() { - if [ -x "$(which git 2> /dev/null)" ] \ - && [ -d "${APOLLO_ROOT_DIR}/.git" ]; then + if [ -x "$(which git 2>/dev/null)" ] && + [ -d "${APOLLO_ROOT_DIR}/.git" ]; then git log -1 --pretty=%ai | cut -d " " -f 1 || true fi } function git_branch() { - if [ -x "$(which git 2> /dev/null)" ] \ - && [ -d "${APOLLO_ROOT_DIR}/.git" ]; then + if [ -x "$(which git 2>/dev/null)" ] && + [ -d "${APOLLO_ROOT_DIR}/.git" ]; then git rev-parse --abbrev-ref HEAD else echo "@non-git" diff --git a/scripts/apollo_format.sh b/scripts/apollo_format.sh index aba3958545b..08d8465afa8 100755 --- a/scripts/apollo_format.sh +++ b/scripts/apollo_format.sh @@ -67,6 +67,42 @@ function run_prettier() { bash "${TOP_DIR}/scripts/mdfmt.sh" "$@" } +function run_apollo_format() { + for arg in "$@"; do + if [[ -f "${arg}" ]]; then + if c_family_ext "${arg}" || proto_ext "${arg}"; then + run_clang_format "${arg}" + elif py_ext "${arg}"; then + run_autopep8 "${arg}" + elif prettier_ext "${arg}"; then + run_prettier "${arg}" + elif bazel_extended "${arg}"; then + run_buildifier "${arg}" + elif bash_ext "${arg}"; then + run_shfmt "${arg}" + fi + elif [[ -d "${arg}" ]]; then + if [ "${FORMAT_BAZEL}" -eq 1 ]; then + run_buildifier "${arg}" + fi + if [ "${FORMAT_CPP}" -eq 1 ]; then + run_clang_format "${arg}" + fi + if [ "${FORMAT_PYTHON}" -eq 1 ]; then + run_autopep8 "${arg}" + fi + if [ "${FORMAT_SHELL}" -eq 1 ]; then + run_shfmt "${arg}" + fi + if [ "${FORMAT_MARKDOWN}" -eq 1 ]; then + run_prettier "${arg}" + fi + else + warning "Ignored ${arg} as not a regular file/directory" + fi + done +} + function main() { if [ "$#" -eq 0 ]; then print_usage @@ -131,21 +167,7 @@ function main() { 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 + run_apollo_format "$@" } main "$@"