Redesign CI format check/update scripts according to review (#3486)

* Redesign CI format check/update scripts according to review

* Address code review #2
This commit is contained in:
theohax 2021-09-30 10:37:09 +03:00 committed by GitHub
commit 6bff63efce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 304 additions and 166 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

27
ci/clang-format-check.sh Executable file
View file

@ -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
###################################################################################################

15
ci/clang-format-do.sh Executable file
View file

@ -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
###################################################################################################

View file

@ -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"

27
ci/cmake-format-check.sh Executable file
View file

@ -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
###################################################################################################

15
ci/cmake-format-do.sh Executable file
View file

@ -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
###################################################################################################

View file

@ -1,5 +0,0 @@
#!/usr/bin/env bash
set -e
REPO_ROOT=$(git rev-parse --show-toplevel)

View file

@ -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'"

View file

@ -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'"

102
ci/impl/clang-format.sh Normal file
View file

@ -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
}
###################################################################################################

108
ci/impl/cmake-format.sh Normal file
View file

@ -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
}
###################################################################################################

7
ci/impl/common.sh Normal file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env bash
###################################################################################################
ROOTPATH="$(git rev-parse --show-toplevel)"
###################################################################################################