diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2024-01-03 22:42:04 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2024-02-09 11:23:03 +0900 |
commit | 5467861028c350bf50be5c0e657785ecce158c5d (patch) | |
tree | 96ec320da82f8d02273c51cb7a8cca8d0eaf4689 | |
parent | 0e60f5c6834eb89dec4c62ab67d0a08a87a2ba77 (diff) | |
download | scripts-feat/code-formatting.tar.gz scripts-feat/code-formatting.zip |
Code format: add client side core scriptsfeat/code-formatting
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
-rwxr-xr-x | code_format/format_dir | 92 | ||||
-rwxr-xr-x | code_format/format_file | 64 | ||||
-rwxr-xr-x | code_format/uncrustify_file | 117 |
3 files changed, 273 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-------------------------------------------" diff --git a/code_format/format_file b/code_format/format_file new file mode 100755 index 0000000..d3ac38d --- /dev/null +++ b/code_format/format_file @@ -0,0 +1,64 @@ +#!/bin/bash + +# +# This script will search for the correct code formatting configuration +# for the file given, based on the repository the file belong to. +# If a configuration version is found, it will format the file by +# invoking the 'uncrustify_file' script. +# +# Parameters: +# $1: source file to format +# $2: maximum number of formatting attempts (optional integer - default is 5) +# + +echo "[FF] Formatting file $1" + +# Check source file exists +SRC_FILE=$1 +if [ ! -e "$1" ]; then + echo "[FF] --- Unable to find the specified source file [${SRC_FILE}]. Aborting." + exit 1 +fi + +SCRIPT_DIR=`dirname $(readlink -f "$0")` + +# Check code format information exists +MOD_CFG_FILE=${MOD_CFG_FILE:-} # Allows injection of module config file from other scripts +if [ -z "${MOD_CFG_FILE}" ]; then + MOD_ROOT_DIR=`git rev-parse --show-toplevel 2>/dev/null` + if [ -z "${MOD_ROOT_DIR}" ]; then + echo "[FF] --- This script can only be run on valid repository files. Aborting." + exit 2 + fi + + CONFIG_INDEX_FILE=${MOD_ROOT_DIR}/.repo_settings/code_format.txt + if [ ! -e "${CONFIG_INDEX_FILE}" ]; then + echo "[FF] No code format version information file found for repository." + exit 0 + fi + + # Find code format version + MOD_CFG_ENTRY=`grep "Version" "${CONFIG_INDEX_FILE}" | grep -v "^\s*#"` + if [ -z "${MOD_CFG_ENTRY}" ]; then + echo "[FF] --- Unable to find the format version information. Aborting." + exit 4 + fi + if [ `echo "${MOD_CFG_ENTRY}" | wc -l` != 1 ]; then + echo "[FF] --- 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 +fi +if [ ! -e "${MOD_CFG_FILE}" ]; then + echo "[FF] --- Unable to find the specified format configuration version file. Aborting." + exit 6 +fi + +# Format the file +${SCRIPT_DIR}/uncrustify_file "${MOD_CFG_FILE}" "${SRC_FILE}" "$2" +RET_CODE=$? +if [ $RET_CODE -ne 0 ]; then + echo "[FF] --- Unable to format the specified file [${SRC_FILE}] (error code ${RET_CODE})." + exit 10 +fi diff --git a/code_format/uncrustify_file b/code_format/uncrustify_file new file mode 100755 index 0000000..0f2204a --- /dev/null +++ b/code_format/uncrustify_file @@ -0,0 +1,117 @@ +#!/bin/bash + +# +# This script tries to format a single file with uncrustify-trinity +# using the specified configuration file and until there are no +# further changes, up to the maximum number of times specified. +# +# Parameters: +# $1: config file to use +# $2: source file to format +# $3: maximum number of formatting attempts (optional integer - default is 5) +# Use 0 to just validate whether a file adhere to the required code format +# + +# Check uncrustify-trinity location +UNCRUSTIFY_BIN="" +if [ -x "/opt/trinity/bin/uncrustify-trinity" ]; then + UNCRUSTIFY_BIN="/opt/trinity/bin/uncrustify-trinity" +elif [ -x "/usr/bin/uncrustify-trinity" ]; then + UNCRUSTIFY_BIN="/usr/bin/uncrustify-trinity" +elif [ -x "/usr/local/bin/uncrustify-trinity" ]; then + UNCRUSTIFY_BIN="/usr/local/bin/uncrustify-trinity" +fi +if [ "${UNCRUSTIFY_BIN}" = "" ]; then + echo "[UF] --- Unable to find uncrustify-trinity executable. Aborting." + exit 1 +fi + + +# Check config file exists +CFG_FILE=$1 +if [ ! -e "$1" ]; then + echo "[UF] --- Unable to find the specified configuration file [${CFG_FILE}]. Aborting." + exit 2 +fi + +# Check source file exists +SRC_FILE=$2 +if [ ! -e "$2" ]; then + echo "[UF] --- Unable to find the specified source file [${SRC_FILE}]. Aborting." + exit 3 +fi + +# Setup intermediate file names +FMT_SRC_FILE="${SRC_FILE%.*}_uncrusted_src.${SRC_FILE##*.}" +FMT_DST_FILE="${SRC_FILE%.*}_uncrusted_dst.${SRC_FILE##*.}" + +# Validate the number of retries, if specified +NUM_ATTEMPTS=5 # default, if not specified +if [ "$3" != "" ]; then + NUM_ATTEMPTS=$(($3)) + if [ ${NUM_ATTEMPTS} -lt 0 ]; then + echo "[UF] --- The number of attempts [${NUM_ATTEMPTS}] must be zero or more. Aborting." + exit 4 + fi +fi +REAL_NUM_ATTEMPTS=$((${NUM_ATTEMPTS} + 1)) + +# Format file and check for errors +# Each pass formats FMT_SRC_FILE into FMT_DST_FILE. +# If there are no errors, a check for changes is done and +# if there are no changes since the previous pass, the process stops. +# If changes are detected, the process continue with a new pass +# until either there are no more changes of the maximum number of +# attempts has been reached. +# At each new pass, the last FMT_DST_FILE becomes the new FMT_SRC_FILE. +echo "[UF] Processing file ${SRC_FILE}" +cp "${SRC_FILE}" "${FMT_SRC_FILE}" +FINISHED="n" +COUNTER=0 +while [ "${FINISHED}" != "y" -a ${COUNTER} -lt ${REAL_NUM_ATTEMPTS} ]; do + COUNTER=$((${COUNTER} + 1)) + echo "[UF] Pass ${COUNTER}" + ${UNCRUSTIFY_BIN} -c "${CFG_FILE}" -f "$FMT_SRC_FILE" -o "${FMT_DST_FILE}" + RESULT=$? + if [ ! ${RESULT} -eq 0 ]; then + echo "[UF] --- Processing error reported by uncrustify-trinity [error code ${RESULT}]" + rm "${FMT_SRC_FILE}" 2>/dev/null + rm "${FMT_DST_FILE}" 2>/dev/null + exit 5 + fi + # Check for changes + if cmp -s "${FMT_SRC_FILE}" "${FMT_DST_FILE}"; then + # No changes detected + FINISHED="y" + else + # Changes detected, prepare for next pass + mv "${FMT_DST_FILE}" "${FMT_SRC_FILE}" + fi +done + +# If a stable formatted output is achieved within the maximum number +# of attempts allowed, update the source file and exit successfully. +# Otherwise remove all the temporary files and exit with error. +if [ "${FINISHED}" != "y" ]; then + if [ ${NUM_ATTEMPTS} -eq 0 ]; then + echo "[UF] --- Code format verification FAILED." + else + echo "[UF] --- Unable to reach stable formatted code in ${NUM_ATTEMPTS} attempts." + fi + rm "${FMT_SRC_FILE}" 2>/dev/null + rm "${FMT_DST_FILE}" 2>/dev/null + exit 6 +fi + +if [ $COUNTER -gt 1 ]; then + # Only update the file if there were actual changes (COUNTER > 1) + # to avoid updating the modified time unnecessarily + mv "${FMT_DST_FILE}" "${SRC_FILE}" +else + rm "${FMT_DST_FILE}" +fi +rm "${FMT_SRC_FILE}" 2>/dev/null + +if [ ${NUM_ATTEMPTS} -eq 0 ]; then + echo "[UF] Code format verification OK." +fi |