Gittyup/cl-fmt.sh

67 lines
1.9 KiB
Bash
Raw Normal View History

2022-05-01 10:09:42 +03:00
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2022-10-16 18:35:23 +03:00
cd "`dirname "$0"`"
2022-05-01 10:09:42 +03:00
# Variable that will hold the name of the clang-format command
FMT=""
2023-11-12 16:55:30 +03:00
FOLDERS=("./src" "./test" "./l10n")
2022-05-01 10:09:42 +03:00
# We specifically require clang-format v13. Some distros include the version
# number in the name, others don't. Prefer the specifically-named version.
for clangfmt in clang-format-13 clang-format
do
if command -v "$clangfmt" &>/dev/null; then
2022-05-01 10:09:42 +03:00
FMT="$clangfmt"
break
fi
done
# Check if we found a working clang-format
if [ -z "$FMT" ]; then
echo "failed to find clang-format"
exit 1
fi
# Check we have v13 of clang-format
VERSION=`$FMT --version | grep -Po 'version\s\K(\d+)'`
if [ "$VERSION" != "13" ]; then
echo "Found clang-format v$VERSION, but v13 is required. Please install v13 of clang-format and try again."
echo "On Debian-derived distributions, this can be done via: apt install clang-format-13"
exit 1
fi
2022-05-01 10:09:42 +03:00
function format() {
2022-10-16 18:35:23 +03:00
for f in $(find $@ \( -type d -path './test/dep/*' -prune \) -o \( -name '*.h' -or -name '*.m' -or -name '*.mm' -or -name '*.c' -or -name '*.cpp' \)); do
2022-05-01 10:09:42 +03:00
echo "format ${f}";
${FMT} -i ${f};
done
echo "~~~ $@ Done ~~~";
}
for dir in ${FOLDERS[@]}; do
if [ ! -d "${dir}" ]; then
echo "${dir} is not a directory";
else
format ${dir};
fi
done
# Format cmake files
# NOTE: requires support for python venv; on Debian-like distros, this can be
# installed using apt install python3-venv
2022-05-01 10:09:42 +03:00
echo "Start formatting cmake files"
CMAKE_FORMAT=${SCRIPT_DIR}/.venv/bin/cmake-format
if [ ! -f "$CMAKE_FORMAT" ]; then
pushd ${SCRIPT_DIR}
python3 -m venv .venv
.venv/bin/pip install cmake-format==0.6.13
popd
fi
2022-10-16 18:35:23 +03:00
find . \
\( -type d -path './test/dep/*' -prune \) \
-o \( -type d -path './dep/*/*' -prune \) \
-o \( -name CMakeLists.txt -exec "$CMAKE_FORMAT" --in-place {} + \)