Fix cmake-format-all.sh script + friendlier xargs usage (#3470)

* Fix calling xargs on OSX

* Fix cmake-format-all.sh script

* Design ci/cmake-format-all.sh and ci/check-cmake-format.sh to be consistent with the clang ones
This commit is contained in:
theohax 2021-09-29 16:26:38 +03:00 committed by GitHub
commit c9236bbf03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 21 deletions

View file

@ -1,16 +1,22 @@
#!/bin/bash
#!/usr/bin/env bash
if ! command -v cmake-format &>/dev/null; then
echo "pip install cmake-format and try again"
set -e
if [[ ! -z $(git status --untracked-files=no --porcelain) ]]; then
echo "Unable to run script: working directory not clean (see git status)"
exit 1
fi
REPO_ROOT=$(git rev-parse --show-toplevel)
cd "${REPO_ROOT}"
find "${REPO_ROOT}" -iwholename "${REPO_ROOT}/nano/*/CMakeLists.txt" -o -iwholename "${REPO_ROOT}/CMakeLists.txt" -o -iwholename "${REPO_ROOT}/coverage/CMakeLists.txt" | xargs cmake-format --check &>.cmake_format_check
if [[ $(wc -l .cmake_format_check | cut -f1 -d ' ') != 0 ]]; then
echo
echo "Code formatting differs from expected - please run \n\t'bash ci/cmake-format-all.sh'"
RET=1
source "$(dirname "$BASH_SOURCE")/common.sh"
"$REPO_ROOT/ci/cmake-format-all.sh"
if [[ ! -z $(git status --untracked-files=no --porcelain) ]]; then
echo "CMake formatting differs from expected - please run ci/cmake-format-all.sh"
git diff
git reset --hard HEAD > /dev/null
exit 1
fi
rm -fr .cmake_format_check
exit ${RET-0}
echo "cmake-format passed"
exit 0

View file

@ -5,4 +5,10 @@ set -e
source "$(dirname "$BASH_SOURCE")/detect-clang-format.sh"
source "$(dirname "$BASH_SOURCE")/common.sh"
find "$REPO_ROOT/nano" -iname '*.h' -o -iname '*.hpp' -o -iname '*.cpp' | xargs "$CLANG_FORMAT" -i -style=file
find "$REPO_ROOT/nano" -iname "*.h" \
-o \
-iname "*.hpp" \
-o \
-iname "*.cpp" \
| xargs -I sourceFile \
"$CLANG_FORMAT" -i -style=file "sourceFile"

View file

@ -2,18 +2,13 @@
set -e
source "$(dirname "$BASH_SOURCE")/detect-cmake-format.sh"
source "$(dirname "$BASH_SOURCE")/common.sh"
if ! [[ $(builtin type -p cmake-format) ]]; then
echo "pip install cmake-format to continue"
exit 1
fi
cd "$REPO_ROOT"
find "$REPO_ROOT" -iwholename "$REPO_ROOT/nano/*/CMakeLists.txt" \
-o \
-iwholename "$REPO_ROOT/CMakeLists.txt" \
-o \
-iwholename "$REPO_ROOT/coverage/CMakeLists.txt" \
| xargs -i{} cmake-format -i {}
| xargs -I cmakeListsFile \
"$CMAKE_FORMAT" -i "cmakeListsFile"

43
ci/detect-cmake-format.sh Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -e
is_cmake_format_usable()
{
if [[ $(builtin type -p $1) ]]; then
local output=$($1 --version)
if [[ $output =~ ^(.)*$2(.)*$ ]]; then
echo "0"
else
echo $output
fi
else
echo "1"
fi
}
CMAKE_FORMAT=""
CMAKE_FORMAT_VERSION="0.6.13"
cmake_format_attempts=("cmake-format")
for itr in ${cmake_format_attempts[@]}; do
result=$(is_cmake_format_usable $itr $CMAKE_FORMAT_VERSION)
if [[ $result == "0" ]]; then
CMAKE_FORMAT=$itr
break
elif [[ $result == "1" ]]; then
continue
else
echo "Detected '$itr' with version '$result' " \
"(different than '$CMAKE_FORMAT_VERSION'), skipping it."
fi
done
if [[ -z $CMAKE_FORMAT ]]; then
echo "No 'cmake-format' of version '$CMAKE_FORMAT_VERSION' could be detected in your PATH." \
"Try pip/pip3 install cmake-format. Or try up/down-grading if you installed it differently."
exit 1
fi
echo "Using '$CMAKE_FORMAT' version '$CMAKE_FORMAT_VERSION'"