summaryrefslogtreecommitdiffstats
path: root/code_format/format_file
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2024-01-03 22:42:04 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2024-02-09 11:23:03 +0900
commit5467861028c350bf50be5c0e657785ecce158c5d (patch)
tree96ec320da82f8d02273c51cb7a8cca8d0eaf4689 /code_format/format_file
parent0e60f5c6834eb89dec4c62ab67d0a08a87a2ba77 (diff)
downloadscripts-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>
Diffstat (limited to 'code_format/format_file')
-rwxr-xr-xcode_format/format_file64
1 files changed, 64 insertions, 0 deletions
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