forked from RafikFarhad/clang-format-github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·81 lines (74 loc) · 2.03 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash -
#title :entrypoint.sh
#description :This script runs clang format on slected file/folder and exit with status 0 if every file is formated correctly.
#author :RafikFarhad<[email protected]>
#date :20200809
#version :1.0.0
#usage :./entrypoint.sh
#notes :Optional env values are: INPUT_STYLE,INPUT_SOURCES
#bash_version :5.0.17(1)-release
###################################################
function log() {
echo -e "[ gh-action ] :: $1"
}
function split_csv() {
IFS=','
csv_data="$1"
local -n global_list_array="$2"
for i in $csv_data; do
if [ -f "$i" ]; then
global_list_array+=("$i")
fi
done
unset IFS
}
SOURCES=()
PROBLEMETIC_FILES=()
EXIT_STATUS=0
STYLE="-style=file"
shopt -s globstar
function resolve_inputs() {
INPUT_STYLE=${INPUT_STYLE:-"file"}
if [[ $INPUT_STYLE != "file" ]]; then
STYLE="--style=$INPUT_STYLE"
fi
INPUT_SOURCES=${INPUT_SOURCES:-"**/*"}
}
function check_file() {
local file="$1"
message="$(clang-format-12 -n -Werror --ferror-limit=1 $STYLE --fallback-style=LLVM "${file}")"
local status="$?"
if [ $status -ne 0 ]; then
echo "$message" >&2
EXIT_STATUS=1
return 1
fi
return 0
}
function main() {
log "Action started"
resolve_inputs
log "Sources to check: $INPUT_SOURCES\n"
split_csv "$INPUT_SOURCES" SOURCES
for file in "${SOURCES[@]}"; do
check_file "$file"
if [ $? -ne 0 ]; then
PROBLEMETIC_FILES+=("$file")
fi
done
if [ $EXIT_STATUS -eq 0 ]; then
log "Congrats! The sources are clang formatted."
exit 0
else
log "Some file is not formatted correctly."
log "You might want to run: "
for ((i = 0; i < ${#PROBLEMETIC_FILES[@]}; i++)); do
if [ $i -ne 0 ]; then
echo -n " && "
fi
clang-format-12 $STYLE -i ${PROBLEMETIC_FILES[$i]}
done
fi
}
cd "$GITHUB_WORKSPACE" || exit 2
main