forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat-cmake
executable file
·51 lines (43 loc) · 1.54 KB
/
reformat-cmake
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
#!/bin/sh
#
# @author René Schwaiger <sanssecours@me.com>
# @brief Reformats CMake source code
# @date 07.05.2018
# @tags reformat
SCRIPTS_DIR=$(dirname "$0")
. "${SCRIPTS_DIR}/include-common"
# The command `cmake-format --version` seems to print the version number either to `stdout` or `stderr`, depending on the current system.
# To make sure we always capture the version number correctly we therefore redirect `stderr` to the same file descriptor as `stdout`,
# before we save the output of the command.
CMAKE_FORMAT="$(which cmake-format)" && CMAKE_FORMAT_VERSION="$("$CMAKE_FORMAT" 2>&1 --version)"
if [ -z "${CMAKE_FORMAT}" ] || [ "$CMAKE_FORMAT_VERSION" != "0.5.4" ]; then
printf >&2 'cmake-format: %s\n' "$CMAKE_FORMAT"
printf >&2 'Version: %s\n' "$CMAKE_FORMAT_VERSION"
printf >&2 'Please install `cmake-format` version 0.5.4\n'
exit 1
fi
cd "$SOURCE" || {
printf >&2 'Unable to change into source directory'
exit 1
}
output=$("${CMAKE_FORMAT}" 2>&1 CMakeLists.txt)
if [ $? != 0 ]; then
printf >&2 'It seems your local installation of `cmake-format` is broken:\n\n%s' "$output"
exit 1
fi
if [ -z "$(which sponge)" ]; then
printf >&2 'Please install `sponge`\n'
exit 1
fi
if [ $# -gt 0 ]; then
files=$(printf "%s\n" "$@" | grep -x -E '.*\.cmake|.*/CMakeLists.txt|CMakeLists.txt')
if [ -z "$files" ]; then
exit 0
fi
else
files=$(git ls-files '*.cmake' '*/CMakeLists.txt' 'CMakeLists.txt')
fi
printf "%s\n" "$files" | sed -nE 's/(.*)/'"'"'\1'"'"'/p' | xargs "$CMAKE_FORMAT" -i
for file in $files; do
unexpand "$file" | sponge "$file"
done