diff options
Diffstat (limited to 'code_format/format_dir')
-rwxr-xr-x | code_format/format_dir | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/code_format/format_dir b/code_format/format_dir new file mode 100755 index 0000000..1601402 --- /dev/null +++ b/code_format/format_dir @@ -0,0 +1,92 @@ +#!/bin/bash + +# +# This script will try formatting all valid files in the specified +# folder and in all its subfolder. If errors are detected, files may +# be skipped or the formatting be incomplete. +# +# +# Parameters: +# $1: folder to format +# $2: maximum number of formatting attempts (optional integer) +# + +# Check source file exists +SRC_DIR=$1 +if [ ! -d "$1" ]; then + echo "[FD] --- Unable to find the specified source folder [${SRC_DIR}]. Aborting." + exit 1 +fi + +SCRIPT_DIR=`dirname $(readlink -f "$0")` + +# Check code format information exists +MOD_ROOT_DIR=`git rev-parse --show-toplevel 2>/dev/null` +if [ -z "${MOD_ROOT_DIR}" ]; then + echo "[FD] --- This script can only be run on valid repository. Aborting." + exit 2 +fi + +CONFIG_INDEX_FILE=${MOD_ROOT_DIR}/.repo_settings/code_format.txt +if [ ! -e "${CONFIG_INDEX_FILE}" ]; then + echo "[FD] No code format version information file found for repository." + exit 0 +fi + +# Find code format version and configuration file +MOD_CFG_ENTRY=`grep "Version" "${CONFIG_INDEX_FILE}" | grep -v "^\s*#"` +if [ -z "${MOD_CFG_ENTRY}" ]; then + echo "[FD] --- Unable to find the format version information. Aborting." + exit 4 +fi +if [ `echo "${MOD_CFG_ENTRY}" | wc -l` != 1 ]; then + echo "[FD] --- Multiple entries found in the format configuration file. Aborting." + exit 5 +fi +MOD_CFG_VERSION=`echo "${MOD_CFG_ENTRY}" | sed "s|^\s*Version\s\+\([0-9]\+\)\s*$|\1|"` +MOD_CFG_FILE=${SCRIPT_DIR}/uncrustify_cfg_files/uncrustify_tde_${MOD_CFG_VERSION}.cfg +if [ ! -e "${MOD_CFG_FILE}" ]; then + echo "[FD] --- Unable to find the specified format configuration version file. Aborting." + exit 6 +fi + +# Format folder +ERROR_FOUND="n" +declare -a ERROR_FILES + +for file in $(find "${SRC_DIR}" \ + -type f \( \ + -name "*.h" -o \ + -name "*.hh" -o \ + -name "*.hpp" -o \ + -name "*.hxx" -o \ + -name "*.h.cmake" -o \ + -name "*.hpp.cmake" -o \ + -name "*.c" -o \ + -name "*.cc" -o \ + -name "*.cpp" -o \ + -name "*.cxx" -o \ + -name "*.tcc" -o \ + -name "*.c.cmake" -o \ + -name "*.cpp.cmake" \)); do + echo "-----------------------" + MOD_CFG_FILE="${MOD_CFG_FILE}" ${SCRIPT_DIR}/format_file $file $2 + if [ $? -ne 0 ]; then + ERROR_FOUND="y" + ERROR_FILES+=($file) + fi +done + +echo -e "\n-------------------------------------------\n" +if [ "${ERROR_FOUND}" != "n" ]; then + echo "[FD] --- Some errors were detected during formatting." + echo -e "\nList of files with errors:\n" + for file in "${ERROR_FILES[@]}"; do + echo " ${file}" + done + echo -e "\n-------------------------------------------" + exit 2 +fi + +echo "[FD] All files formatted correctly." +echo -e "\n-------------------------------------------" |