From 54e2fca88c85148509b457aa9fcac8ea187be5d6 Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Sun, 28 Jul 2019 23:07:51 +0200 Subject: [PATCH] feat(code-style.sh): Format Python files using black Signed-off-by: Rahix <rahix@rahix.de> --- tools/code-style.sh | 46 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/tools/code-style.sh b/tools/code-style.sh index 2d459c57..b6943dbd 100755 --- a/tools/code-style.sh +++ b/tools/code-style.sh @@ -1,15 +1,33 @@ #!/bin/bash set -e +########################### # Check tools -if ! command -v clang-format >/dev/null 2>&1; then - echo "$0: requires clang-format." >&2 - exit 127 + +# If any C files are formatted +if [[ "$*" == *.c* ]]; then + if ! command -v clang-format >/dev/null 2>&1; then + echo "$0: requires clang-format." >&2 + exit 127 + fi + + if ! command -v python3 >/dev/null 2>&1; then + echo "$0: requires python3." >&2 + exit 127 + fi fi -if ! command -v python3 >/dev/null 2>&1; then - echo "$0: requires python3." >&2 - exit 127 +# If any Python files are formatted +if [[ "$*" == *.py* ]]; then + if ! command -v black >/dev/null 2>&1; then + echo "$0: requires black (python formatter)." >&2 + echo "" >&2 + echo "You can install \`black\` using pip3:" >&2 + echo "" >&2 + echo " pip3 install --user black" >&2 + echo "" >&2 + exit 127 + fi fi if [[ "$#" == 0 ]]; then @@ -20,12 +38,24 @@ fi script_dir="$(dirname "$0")" for source_file in "$@"; do + # Check if file is actually one which we want to check + rel_path="$(realpath --relative-to="$script_dir/.." "$source_file")" + if [[ ( "$rel_path" == lib/* && "$rel_path" != lib/card10/* ) || + ( "$rel_path" == openocd/* ) || + ( "$rel_path" == ble/* ) ]]; then + echo " - Ignoring $source_file" + continue + fi + if [[ "$source_file" == *.c ]]; then - echo "Formatting $source_file ..." + echo " - Formatting $source_file ..." clang-format -i "$source_file" python3 "$script_dir/fix-multi-decl.py" "$source_file" + elif [[ "$source_file" == *.py ]]; then + echo " - Formatting $source_file ..." + black -q "$source_file" else - echo "Not a C file: $source_file" >&2 + echo " - Ignoring $source_file" >&2 continue fi done -- GitLab