code-style.sh 1.83 KB
Newer Older
Rahix's avatar
Rahix committed
1
#!/usr/bin/env bash
Rahix's avatar
Rahix committed
2
3
set -e

4
###########################
Rahix's avatar
Rahix committed
5
# Check tools
6
7
8
9
10
11
12
13
14
15
16
17

# 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
Rahix's avatar
Rahix committed
18
19
fi

20
21
22
23
24
25
26
27
28
29
30
# 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
Rahix's avatar
Rahix committed
31
32
fi

33
if [[ "$#" == 0 ]]; then
34
35
36
37
    echo "usage: $0 <source.c> ..."
    exit 1
fi

Rahix's avatar
Rahix committed
38
39
script_dir="$(dirname "$0")"

40
41
42
43
formatter_blacklist=(
    lib/ff13/
    lib/FreeRTOS/
    lib/FreeRTOS-Plus/
schneider's avatar
schneider committed
44
    lib/lodepng/
45
46
47
48
49
50
51
52
    lib/micropython/
    lib/mx25lba/
    lib/sdk/
    lib/vendor/
    openocd/
    docker/
)

Rahix's avatar
Rahix committed
53
for source_file in "$@"; do
54
55
    # Check if file is actually one which we want to check
    rel_path="$(realpath --relative-to="$script_dir/.." "$source_file")"
56
57
58
59
60
61
62
63
64
    format_item=true
    for blacklist_item in "${formatter_blacklist[@]}"; do
        if [[ "$rel_path" == "$blacklist_item"* ]]; then
            format_item=false
            break
        fi
    done
    if [[ "$format_item" == false ]]; then
        echo -e "\tIGN\t\t$source_file"
65
66
67
        continue
    fi

68
    if [[ "$source_file" == *.c ]]; then
69
        echo -e "\tCLANG-FORMAT\t$source_file"
70
71
        clang-format -i "$source_file"
        python3 "$script_dir/fix-multi-decl.py" "$source_file"
72
    elif [[ "$source_file" == *.py ]]; then
73
        echo -e "\tBLACK\t\t$source_file"
74
        black -q "$source_file"
75
    else
Rahix's avatar
Rahix committed
76
        echo -e "\tIGN\t\t$source_file"
q3k's avatar
q3k committed
77
	fi
Rahix's avatar
Rahix committed
78
done