Skip to content

Commit

Permalink
Redesign CI format check/update scripts according to review (#3486)
Browse files Browse the repository at this point in the history
* Redesign CI format check/update scripts according to review

* Address code review #2
  • Loading branch information
theohax committed Sep 30, 2021
1 parent c9236bb commit 6bff63e
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 166 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/analyzers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
22 changes: 0 additions & 22 deletions ci/check-cmake-format.sh

This file was deleted.

22 changes: 0 additions & 22 deletions ci/check-commit-format.sh

This file was deleted.

14 changes: 0 additions & 14 deletions ci/clang-format-all.sh

This file was deleted.

27 changes: 27 additions & 0 deletions ci/clang-format-check.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions ci/clang-format-do.sh
Original file line number Diff line number Diff line change
@@ -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

###################################################################################################
14 changes: 0 additions & 14 deletions ci/cmake-format-all.sh

This file was deleted.

27 changes: 27 additions & 0 deletions ci/cmake-format-check.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions ci/cmake-format-do.sh
Original file line number Diff line number Diff line change
@@ -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

###################################################################################################
5 changes: 0 additions & 5 deletions ci/common.sh

This file was deleted.

43 changes: 0 additions & 43 deletions ci/detect-clang-format.sh

This file was deleted.

43 changes: 0 additions & 43 deletions ci/detect-cmake-format.sh

This file was deleted.

102 changes: 102 additions & 0 deletions ci/impl/clang-format.sh
Original file line number Diff line number Diff line change
@@ -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
}

###################################################################################################
Loading

0 comments on commit 6bff63e

Please sign in to comment.