Skip to content

Commit

Permalink
Scripts: make "apollo_format.sh" smarter on files
Browse files Browse the repository at this point in the history
  • Loading branch information
storypku authored and changsh726 committed Sep 14, 2020
1 parent 38495c7 commit 78f2862
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 35 deletions.
107 changes: 87 additions & 20 deletions scripts/apollo.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
52 changes: 37 additions & 15 deletions scripts/apollo_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "$@"

0 comments on commit 78f2862

Please sign in to comment.