Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'"