From c9236bbf03c9398da1824a49c2f3cd209b36df02 Mon Sep 17 00:00:00 2001 From: theohax <81556890+theohax@users.noreply.github.com> Date: Wed, 29 Sep 2021 16:26:38 +0300 Subject: [PATCH] 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 --- ci/check-cmake-format.sh | 30 ++++++++++++++++----------- ci/clang-format-all.sh | 8 +++++++- ci/cmake-format-all.sh | 11 +++------- ci/detect-cmake-format.sh | 43 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 ci/detect-cmake-format.sh diff --git a/ci/check-cmake-format.sh b/ci/check-cmake-format.sh index 633f8182..47e7b24c 100755 --- a/ci/check-cmake-format.sh +++ b/ci/check-cmake-format.sh @@ -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 diff --git a/ci/clang-format-all.sh b/ci/clang-format-all.sh index c2401833..fe9ba2fe 100755 --- a/ci/clang-format-all.sh +++ b/ci/clang-format-all.sh @@ -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" diff --git a/ci/cmake-format-all.sh b/ci/cmake-format-all.sh index c5bde79a..90f6ca13 100755 --- a/ci/cmake-format-all.sh +++ b/ci/cmake-format-all.sh @@ -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" diff --git a/ci/detect-cmake-format.sh b/ci/detect-cmake-format.sh new file mode 100644 index 00000000..d0917e7a --- /dev/null +++ b/ci/detect-cmake-format.sh @@ -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'"