summaryrefslogtreecommitdiffstats
path: root/code_format/format_dir
blob: 16014024ccd8118949ca61c59b92c5ad64627c25 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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-------------------------------------------"