-
Notifications
You must be signed in to change notification settings - Fork 521
/
Copy pathRunClangFormat.sh
97 lines (74 loc) · 2.81 KB
/
RunClangFormat.sh
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
93
94
95
96
97
#!/usr/bin/env bash
set -e
solution_dir=$(cd "$(dirname "$0")"; pwd)
FOLDERS="src tests"
ALLOWED_EXTENSIONS_RE=".*\.\(cpp\|hpp\|c\|h\|inl\)"
EXCLUSION_RE="\(Bindings\|CLI\)/\|\(CppParser/\(Parse\)?\(Expr\|Stmt\)\)"
CLANG_FORMAT_OPTIONS="--fallback-style=none -i --verbose"
clang_format=""
VS_INSTALL_DIR=""
format()
{
find_clang_format
DRY_RUN=0
# read -p "Dry run? (1/0): " DRY_RUN
if [ "$DRY_RUN" -eq 1 ]; then
CLANG_FORMAT_OPTIONS="$CLANG_FORMAT_OPTIONS --dry-run --Werror"
fi
# Format all files, in the specified list of directories
# Loop through each file in the directory (and subdirectories)
for p in $FOLDERS; do
echo "Formatting files in folder \"$p\"..."
cd "$solution_dir/$p"
file_list=$(find . -type f -regex $ALLOWED_EXTENSIONS_RE -and -not -regex ".*/\($EXCLUSION_RE\).*" -print)
"$clang_format" $CLANG_FORMAT_OPTIONS $file_list
cd ..
done
}
find_vswhere()
{
echo "Looking for 'vswhere'..."
VSWHERE_PATH="$(printenv 'ProgramFiles(x86)')/Microsoft Visual Studio/Installer/vswhere.exe"
if [ -x "$(command -v vswhere.exe)" ]; then
vswhere="vswhere.exe"
elif [ -f "$VSWHERE_PATH" ]; then
vswhere=$VSWHERE_PATH
else
echo -e "[91mCould not locate vswhere.exe at:\n " $VSWHERE_PATH
read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
exit 1
fi
echo -e "Found 'vswhere.exe':\n [90m@" $vswhere "[0m\n"
}
find_visual_studio()
{
find_vswhere
echo "Looking for visual studio..."
# Find visual studio installation path
VS_INSTALL_DIR=$("$vswhere" -latest -property installationPath)
# Find visual studio installation path
if [ ! -d "$VS_INSTALL_DIR" ]; then
echo -e "[91mVisual Studio Installation directory not found at vswhere specified path:\n " $VS_INSTALL_DIR
read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
exit 1
fi
echo -e "Found Visual Studio:\n [90m@" $VS_INSTALL_DIR "[0m\n"
}
find_clang_format()
{
echo "Looking for clang-format..."
find_visual_studio
CLANG_FORMAT_PATH="VC/Tools/Llvm/bin/clang-format.exe"
clang_format="$VS_INSTALL_DIR/$CLANG_FORMAT_PATH"
# Verify clang-format actually exists as well
if ! [ -f "$clang_format" ]; then
echo "[91mclang-format.exe could not be located at: $clang_format"
read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
exit 1
fi
echo -e "Found clang-format.exe:\n" " [90mUsing" $("$clang_format" --version) "[0m\n" " [90m@" $clang_format "[0m\n"
}
format
echo "Done!"
read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
exit 0