Skip to content

Commit

Permalink
Fix cmake-format-all.sh script + friendlier xargs usage (#3470)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
theohax authored Sep 29, 2021
1 parent 9f397d2 commit c9236bb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
30 changes: 18 additions & 12 deletions ci/check-cmake-format.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion ci/clang-format-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 3 additions & 8 deletions ci/cmake-format-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 43 additions & 0 deletions ci/detect-cmake-format.sh
Original file line number Diff line number Diff line change
@@ -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'"

0 comments on commit c9236bb

Please sign in to comment.