diff --git a/.github/workflows/analyzers.yml b/.github/workflows/analyzers.yml index 22dc8e6a2d..cea239a81e 100644 --- a/.github/workflows/analyzers.yml +++ b/.github/workflows/analyzers.yml @@ -12,8 +12,8 @@ jobs: env: DEBIAN_FRONTEND: noninteractive run: sudo apt-get install clang-format-12 - - name: Clang Format - run: ci/check-commit-format.sh + - name: Check clang-format + run: ci/clang-format-check.sh cmake_format: runs-on: ubuntu-20.04 @@ -29,4 +29,4 @@ jobs: packages: | cmake-format - name: Check cmake-format - run: bash ci/check-cmake-format.sh + run: ci/cmake-format-check.sh diff --git a/ci/check-cmake-format.sh b/ci/check-cmake-format.sh deleted file mode 100755 index 47e7b24ca7..0000000000 --- a/ci/check-cmake-format.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -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 - -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 - -echo "cmake-format passed" -exit 0 diff --git a/ci/check-commit-format.sh b/ci/check-commit-format.sh deleted file mode 100755 index 8a1c1e44a8..0000000000 --- a/ci/check-commit-format.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -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 - -source "$(dirname "$BASH_SOURCE")/common.sh" - -"$REPO_ROOT/ci/clang-format-all.sh" - -if [[ ! -z $(git status --untracked-files=no --porcelain) ]]; then - echo "Code formatting differs from expected - please run ci/clang-format-all.sh" - git diff - git reset --hard HEAD > /dev/null - exit 1 -fi - -echo "clang-format passed" -exit 0 diff --git a/ci/clang-format-all.sh b/ci/clang-format-all.sh deleted file mode 100755 index fe9ba2fe63..0000000000 --- a/ci/clang-format-all.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -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 -I sourceFile \ - "$CLANG_FORMAT" -i -style=file "sourceFile" diff --git a/ci/clang-format-check.sh b/ci/clang-format-check.sh new file mode 100755 index 0000000000..358f955fb6 --- /dev/null +++ b/ci/clang-format-check.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +################################################################################################### + +source "$(dirname "$BASH_SOURCE")/impl/common.sh" +source "$(dirname "$BASH_SOURCE")/impl/clang-format.sh" + +################################################################################################### + +does_clang_format_exist +if [[ $? == 0 ]]; then + clang_format_check + result=$? + + if [[ $result == 2 ]]; then + exit $result + fi + + if [[ $result == 1 ]]; then + echo "Source code formatting differs from expected - please run ci/clang-format-do.sh" + exit 1 + fi + + echo "clang-format check passed" +fi + +################################################################################################### diff --git a/ci/clang-format-do.sh b/ci/clang-format-do.sh new file mode 100755 index 0000000000..66d9b8ab44 --- /dev/null +++ b/ci/clang-format-do.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +################################################################################################### + +source "$(dirname "$BASH_SOURCE")/impl/common.sh" +source "$(dirname "$BASH_SOURCE")/impl/clang-format.sh" + +################################################################################################### + +does_clang_format_exist +if [[ $? == 0 ]]; then + clang_format_do +fi + +################################################################################################### diff --git a/ci/cmake-format-all.sh b/ci/cmake-format-all.sh deleted file mode 100755 index 90f6ca1342..0000000000 --- a/ci/cmake-format-all.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -set -e - -source "$(dirname "$BASH_SOURCE")/detect-cmake-format.sh" -source "$(dirname "$BASH_SOURCE")/common.sh" - -find "$REPO_ROOT" -iwholename "$REPO_ROOT/nano/*/CMakeLists.txt" \ - -o \ - -iwholename "$REPO_ROOT/CMakeLists.txt" \ - -o \ - -iwholename "$REPO_ROOT/coverage/CMakeLists.txt" \ - | xargs -I cmakeListsFile \ - "$CMAKE_FORMAT" -i "cmakeListsFile" diff --git a/ci/cmake-format-check.sh b/ci/cmake-format-check.sh new file mode 100755 index 0000000000..c9314a9108 --- /dev/null +++ b/ci/cmake-format-check.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +################################################################################################### + +source "$(dirname "$BASH_SOURCE")/impl/common.sh" +source "$(dirname "$BASH_SOURCE")/impl/cmake-format.sh" + +################################################################################################### + +does_cmake_format_exist +if [[ $? == 0 ]]; then + cmake_format_check + result=$? + + if [[ $result == 2 ]]; then + exit $result + fi + + if [[ $result == 1 ]]; then + echo "CMake formatting differs from expected - please run ci/cmake-format-do.sh" + exit 1 + fi + + echo "cmake-format check passed" +fi + +################################################################################################### diff --git a/ci/cmake-format-do.sh b/ci/cmake-format-do.sh new file mode 100755 index 0000000000..94f44c34d9 --- /dev/null +++ b/ci/cmake-format-do.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +################################################################################################### + +source "$(dirname "$BASH_SOURCE")/impl/common.sh" +source "$(dirname "$BASH_SOURCE")/impl/cmake-format.sh" + +################################################################################################### + +does_cmake_format_exist +if [[ $? == 0 ]]; then + cmake_format_do +fi + +################################################################################################### diff --git a/ci/common.sh b/ci/common.sh deleted file mode 100644 index 5205c3a1d2..0000000000 --- a/ci/common.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash - -set -e - -REPO_ROOT=$(git rev-parse --show-toplevel) diff --git a/ci/detect-clang-format.sh b/ci/detect-clang-format.sh deleted file mode 100644 index 4179ec889b..0000000000 --- a/ci/detect-clang-format.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash - -set -e - -is_clang_format_usable() -{ - if [[ $(builtin type -p $1) ]]; then - local output=$($1 --version) - if [[ $output =~ ^(.)*clang-format\ version\ $2(.)*$ ]]; then - echo "0" - else - echo $output - fi - else - echo "1" - fi -} - -CLANG_FORMAT="" -CLANG_FORMAT_VERSION="12" - -clang_format_attempts=("clang-format" - "clang-format-$CLANG_FORMAT_VERSION") - -for itr in ${clang_format_attempts[@]}; do - result=$(is_clang_format_usable $itr $CLANG_FORMAT_VERSION) - if [[ $result == "0" ]]; then - CLANG_FORMAT=$itr - break - elif [[ $result == "1" ]]; then - continue - else - echo "Detected '$itr' with version '$result' " \ - "(different than '$CLANG_FORMAT_VERSION'), skipping it." - fi -done - -if [[ -z $CLANG_FORMAT ]]; then - echo "No 'clang-format' of version '$CLANG_FORMAT_VERSION' could be detected in your PATH." - exit 1 -fi - -echo "Using '$CLANG_FORMAT' version '$CLANG_FORMAT_VERSION'" diff --git a/ci/detect-cmake-format.sh b/ci/detect-cmake-format.sh deleted file mode 100644 index d0917e7a55..0000000000 --- a/ci/detect-cmake-format.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/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'" diff --git a/ci/impl/clang-format.sh b/ci/impl/clang-format.sh new file mode 100644 index 0000000000..3a3d69626e --- /dev/null +++ b/ci/impl/clang-format.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +################################################################################################### + +CLANG_FORMAT="" +CLANG_FORMAT_VERSION="12" + +################################################################################################### + +does_clang_format_exist() +{ + local attempts=("clang-format" "clang-format-$CLANG_FORMAT_VERSION") + for itr in ${attempts[@]}; do + version=$(_is_clang_format_usable $itr $CLANG_FORMAT_VERSION) + if [[ $? == 1 ]]; then + continue + fi + + if [[ $? == 0 ]]; then + CLANG_FORMAT=$itr + break + fi + + echo "Detected '$itr' with version '$version' " \ + "(different than '$CLANG_FORMAT_VERSION'), skipping it." + done + + if [[ -z $CLANG_FORMAT ]]; then + echo "No 'clang-format' of version '$CLANG_FORMAT_VERSION' could be detected in your " \ + "PATH. Try 'sudo apt-get install clang-format-$CLANG_FORMAT_VERSION' or, if macOS, " \ + "'brew install clang-format'. Or up/down grade, if installed differently." + return 1 + fi + + echo "Using '$CLANG_FORMAT' version '$CLANG_FORMAT_VERSION'" + return 0 +} + +################################################################################################### + +clang_format_do() +{ + _clang_format_perform "do" +} + +################################################################################################### + +clang_format_check() +{ + _clang_format_perform "check" +} + +################################################################################################### + +_is_clang_format_usable() +{ + if [[ $(builtin type -p $1) ]]; then + local output=$($1 --version) + if [[ $output =~ ^(.)*$2(.)*$ ]]; then + return 0 + fi + + echo $output + return 1 + fi + + return 2 +} + +################################################################################################### + +_clang_format_perform() +{ + if [[ -z "$CLANG_FORMAT" ]]; then + echo "Logic error: '_lang_format_perform' called, but 'CLANG_FORMAT' " \ + "is empty. Have you called 'does_clang_format_exist'?" + return 2 + fi + + find "$ROOTPATH/nano" -type f \( -iname "*.hpp" \ + -o \ + -iname "*.cpp" \ + \) \ + -print0 | + while read -d $'\0' file + do + if [[ $1 == "do" ]]; then + "$CLANG_FORMAT" -i "$file" + elif [[ $1 == "check" ]]; then + "$CLANG_FORMAT" -style=file -Werror --dry-run "$file" + if [[ $? != 0 ]]; then + return 1 + fi + else + echo "Logic error: '_clang_format_perform' called " \ + "with neither 'do' nor 'check' as argument, but '$1'" + return 2 + fi + done +} + +################################################################################################### diff --git a/ci/impl/cmake-format.sh b/ci/impl/cmake-format.sh new file mode 100644 index 0000000000..9a8c187319 --- /dev/null +++ b/ci/impl/cmake-format.sh @@ -0,0 +1,108 @@ +#!/usr/bin/env bash + +################################################################################################### + +CMAKE_FORMAT="" +CMAKE_FORMAT_VERSION="0.6.13" + +################################################################################################### + +does_cmake_format_exist() +{ + local attempts=("cmake-format") + for itr in ${attempts[@]}; do + version=$(_is_cmake_format_usable $itr $CMAKE_FORMAT_VERSION) + if [[ $? == 1 ]]; then + continue + fi + + if [[ $? == 0 ]]; then + CMAKE_FORMAT=$itr + break + fi + + echo "Detected '$itr' with version '$version' " \ + "(different than '$CMAKE_FORMAT_VERSION'), skipping it." + done + + if [[ -z $CMAKE_FORMAT ]]; then + echo "No 'cmake-format' of version '$CMAKE_FORMAT_VERSION' could be detected in your " \ + "PATH. Try 'pip3 install cmake-format'. Or up/down grade, if installed differently." + return 1 + fi + + echo "Using '$CMAKE_FORMAT' version '$CMAKE_FORMAT_VERSION'" + return 0 +} + +################################################################################################### + +cmake_format_do() +{ + _cmake_format_perform "do" +} + +################################################################################################### + +cmake_format_check() +{ + _cmake_format_perform "check" +} + +################################################################################################### + +_is_cmake_format_usable() +{ + if [[ $(builtin type -p $1) ]]; then + local output=$($1 --version) + if [[ $output =~ ^(.)*$2(.)*$ ]]; then + return 0 + fi + + echo $output + return 1 + fi + + return 2 +} + +################################################################################################### + +_cmake_format_perform() +{ + if [[ -z "$CMAKE_FORMAT" ]]; then + echo "Logic error: '_cmake_format_perform' called, but 'CMAKE_FORMAT' " \ + "is empty. Have you called 'does_cmake_format_exist'?" + return 2 + fi + + find "$ROOTPATH" -type f \( -iwholename "$ROOTPATH/CMakeLists.txt" \ + -o \ + -iwholename "$ROOTPATH/coverage/CMakeLists.txt" \ + -o \ + -iwholename "$ROOTPATH/nano/*/CMakeLists.txt" \ + \) \ + -print0 | + while read -d $'\0' file + do + if [[ $1 == "do" ]]; then + "$CMAKE_FORMAT" -i "$file" + elif [[ $1 == "check" ]]; then + "$CMAKE_FORMAT" "$file" -o tmp + + diff "$file" tmp > /dev/null + if [[ $? != 0 ]]; then + rm tmp + return 1 + fi + + rm tmp + else + echo "Logic error: '_cmake_format_perform' called " \ + "with neither 'do' nor 'check' as argument, but '$1'" + return 2 + fi + done +} + +################################################################################################### diff --git a/ci/impl/common.sh b/ci/impl/common.sh new file mode 100644 index 0000000000..dc97b26204 --- /dev/null +++ b/ci/impl/common.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +################################################################################################### + +ROOTPATH="$(git rev-parse --show-toplevel)" + +###################################################################################################