Skip to content

Commit

Permalink
scripts/check: add -a, --resume, -i
Browse files Browse the repository at this point in the history
  • Loading branch information
johanmalm committed Apr 14, 2020
1 parent d052fa6 commit 60371bf
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -16,4 +16,4 @@ binsiz.log
.d/
checkdeps
tags
clang-format-tmpfile
.tmp-*
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -140,7 +140,7 @@ ex:
@$(MAKE) --no-print-directory -C examples/ all

check:
@./scripts/check
@./scripts/check src/*.sh src/*.c src/*.h

print-%:
@echo '$*=$($*)'
Expand Down
85 changes: 61 additions & 24 deletions scripts/check
@@ -1,65 +1,102 @@
#!/usr/bin/env bash

g_analyse_all=f
g_cppcheck=t
g_clangformat=t
g_shellcheck=t
g_resume_mode=f

logfile=.tmp-check-script

die () {
printf '\033[31mfatal:\033[m %b\n' "$@" >&2
exit 1
}

warn () {
printf '\033[31mwarn:\033[m %b\n' "$@" >&2
}

say () {
printf '\033[32m%s\033[m\n' "$@"
printf '\033[32m%s\033[m' "$@"
}

isinstalled () {
type "$1" >/dev/null 2>&1
}

usage () {
say "Usage: check [<options>]"
say "Options:"
say "-h show help"
printf "%s\n" \
"Usage: check [<options>] [<file>...]
Options:
-a, --all run check on all .c .h and .sh files in src/
-h, --help show help
-i, --in-situ pass -i to clang-format
--resume same as --all, but carry on with last aborted"
exit 0
}

analyse () {
[[ -e $1 ]] || die "file ($1) does not exist"
say " CHECK $1"
if [[ $(file $1) == *shell\ script* ]]; then
[[ $g_shellcheck = f ]] && return
say " CHECK $1"
say " [shellcheck]"
shellcheck "$1"
fi
if [[ $(file $1) == *C\ source* ]]; then
./scripts/cppcheck-wrapper.sh "$1"
say " CHECK $1"
if [[ $g_cppcheck = t ]]; then
say " [cppcheck]"
./scripts/cppcheck-wrapper.sh "$1"
fi
say " [checkpatch.pl]"
if [[ $g_clangformat = t ]]; then
say " [clang-format]"
./scripts/clang-format-wrapper.sh "$1"
fi
./scripts/checkpatch-wrapper.sh "$1"
fi
printf "\n"
}

analyse_all () {
for f in src/*.sh; do
analyse "$f"
done
for f in src/*.c; do
analyse "$f"
done
for f in src/*.h; do
[[ $g_resume_mode = f ]] && rm -f "${logfile}"
for f in src/*.sh src/*.c src/*.h; do
grep "${f}" "${logfile}" >/dev/null 2>&1 && continue
analyse "$f"
printf '%b\n' "${f}" >> "${logfile}"
done
}

args() {
main () {
[[ -e src/jgmenu.c ]] || die "must be run from top-level directory"
[[ $# = 0 ]] && usage
isinstalled cppcheck || { warn "cppcheck not installed"; g_cppcheck=f; }
isinstalled clang-format || { warn "clang-format not installed"; g_clangformat=f; }
isinstalled shellcheck || { warn "shellcheck not installed"; g_shellcheck=f; }

for arg
do
case $arg in
-h)
usage ;;
*)
analyse "$arg"
opt=${arg%%=*}
var=${arg#*=}
case "$opt" in
-a|-all)
g_analyse_all=t ;;
-i)
g_insitu=t ;;
-h|--help)
usage ;;
--resume)
g_analyse_all=t
g_resume_mode=t
;;
*)
analyse "$var"
esac
done
}

main() {
[[ -e src/jgmenu.c ]] || die "must be run from top-level directory"
args "$@"
[[ $# -eq 0 ]] && analyse_all
[[ $g_analyse_all = t ]] && analyse_all
}

main "$@"
41 changes: 16 additions & 25 deletions scripts/clang-format-wrapper.sh
@@ -1,6 +1,7 @@
#!/bin/bash

tmpfile="clang-format-tmpfile"
tmpfile=".tmp-clang-format"
g_insitu=f

iterators=(
list_for_each_entry
Expand All @@ -12,10 +13,9 @@ iterators=(
)

usage () {
printf '%b' "\
Usage: clang-format-wrapper.sh [<file>]\n\
Produces diff of changes.\n\
"
printf '%s\n' "\
Usage: clang-format-wrapper.sh [<file>]
Produces diff of changes"
}

strip_space_before_foreach_iterators () {
Expand All @@ -26,34 +26,25 @@ strip_space_before_foreach_iterators () {
done
}

format_file () {
echo $1
format () {
g_content=$(clang-format $1)
strip_space_before_foreach_iterators
printf '%s\n' "${g_content}" >$tmpfile
diff $1 $tmpfile
[[ $g_insitu = t ]] && mv -f $tmpfile $1
}

format_all_files () {
for f in ./src/*.c
main () {
for arg
do
format_file "$f"
done
}

args () {
case $1 in
opt=${arg%%=*}
var=${arg#*=}
case "$opt" in
-h) usage ;;
*) format_file "$1" ;;
esac
}

main () {
if [[ $# -gt 0 ]]; then
args "$@"
else
format_all_files
fi
-i) g_insitu=t ;;
*) format "$opt" ;;
esac
done
}

main "$@"

0 comments on commit 60371bf

Please sign in to comment.