#!/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