summaryrefslogtreecommitdiffstats
path: root/code_format/format_file
blob: d3ac38d1b58e84d1c0af6c6db326d4849305379c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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