Skip to content

Instantly share code, notes, and snippets.

@m9aertner
Created March 18, 2021 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m9aertner/74673899ec7f5d0ca7f45a4c39d342d7 to your computer and use it in GitHub Desktop.
Save m9aertner/74673899ec7f5d0ca7f45a4c39d342d7 to your computer and use it in GitHub Desktop.
Quick script that applies editorconfig-checker to our code base, with per-file good/bad output, one per line
#!/bin/bash
#
# Checking files for .editorconfig compliance
#
# For use with https://github.com/editorconfig-checker/editorconfig-checker
#
# Default output shows non-compliant files on stderr
# Verbose output also shows compliant files, on stdout.
# Quiet mode suppresses output of non-compliant file names, but still checks and counts.
# File can come from the command line, via stdin (say "-"), from a files list (--files)
# or from a folder with a pattern (--folder, --pattern).
#
# Use --help for details.
#
# Sample use:
# find modules/main/ui -type f -name "*.css" | ec-codebase -
# ec-codebase -+ --verbose --folder src --pattern *.c
#
# MIT License
set -e
function fail() {
1>&2 echo "ERROR: $1"
exit 1
}
test -x "$(which ec)" || fail "No ec executable found. For installation, see https://editorconfig-checker.github.io"
#EC_OPTS=
#EC_OPTS+="-disable-end-of-line "
#EC_OPTS+="-disable-indent-size "
#EC_OPTS+="-disable-indentation "
#EC_OPTS+="-disable-insert-final-newline "
#EC_OPTS+="-disable-max-line-length "
#EC_OPTS+="-disable-trim-trailing-whitespace "
PLUSMINUSP=
PLUSMINUSM=
VERBOSE=
QUIET=
STRICT=
NO_SUMMARY=
FOLDER=
DFLT_PATTERN="*.java"
for i in $@
do
case "$i" in
--help|-h*)
cat - <<END
ec-codebase
Run editorconfig-checker (ec) against code base files
Default is to recurse from current directory and check
all *.java files. Show the non-compliant ones.
Options:
[--verbose] Show compliant files as well, use twice for raw ec output
[--quiet] Do not show failing files, just count them
[--disable-*] See ec --help for disable options, they are passed to ec.
[--strict] Return $?=1 if at least one file failed
[--no-summary] Do not show OK/FAIL/TOTAL counts
[--folder=<.>] File selection, defaults to cwd. Ignored if other input given.
[--pattern=<*.java>] With folder: which file name pattern (find)
[--files=<listfile>] Which files to check, one per line
[-+] Prefix filename with -/+ for (non-)compliant files
[--help] Show this help
Arguments:
[<files, folders>] ec called once per command line item as well.
Use '-' to read from stdin.
END
exit 0
;;
--verbose*)
VERBOSE=$((${VERBOSE-0} + 1))
shift
;;
--strict*)
STRICT=1
shift
;;
--quiet*)
QUIET=1
shift
;;
--no-summary*)
NO_SUMMARY=1
shift
;;
--folder=*)
FOLDER="${i#*=}"
shift
;;
--files=*)
ARGFILES="${i#*=}"
shift
;;
--pattern=*)
PATTERN="${i#*=}"
shift
;;
-disable-*|--disable-*)
EC_OPTS+="${i/--/-} "
shift
;;
-+)
PLUSMINUSP=" "
PLUSMINUSM="-"
shift
;;
--)
shift
break
;;
-)
break
;;
-*)
>&2 echo "Unrecognized option '$i', aborting."
exit 3
;;
esac
done
if [ "-" == "${ARGFILES}" ]; then
: # stdin, good for piping from find (--files=-)
elif [ -n "${ARGFILES}" ]; then
if [ ! -r "${ARGFILES}" ]; then
fail "Unable to read input file ${ARGFILES}, aborting"
fi
else
ARGFILES=/dev/null
fi
# Enumerate all files
FILES="$(mktemp -t editorconfig-checker)"
cat "${ARGFILES}" > "${FILES}"
for file in $@; do
if [ "-" == "$file" ]; then
cat - >> "${FILES}"
else
echo "$file" >> "${FILES}"
fi
done
if [ ! -s "${FILES}" -a -z "${FOLDER}" ]; then
FOLDER=.
fi
if [ -d "${FOLDER}" -o -n "${PATTERN}" ]; then
find "${FOLDER-.}" -type f -name "${PATTERN-${DFLT_PATTERN}}" >> "${FILES}"
fi
# Iterate and check, record and count if OK or not
N_OK=0
N_FAIL=0
N_TOTAL=0
while read fn; do
N_TOTAL=$((++N_TOTAL))
RC=0
if [ "${VERBOSE}" == 2 ]; then
ec ${EC_OPTS} "${fn}" 2>&1 || RC=$?
else
ec ${EC_OPTS} "${fn}" > /dev/null 2>&1 || RC=$?
fi
if [ "${RC}" == 0 ]; then
test -n "${VERBOSE}" && echo "${PLUSMINUSP}${fn}"
N_OK=$((++N_OK))
else
test -z "${QUIET}" && 1>&2 echo "${PLUSMINUSM}${fn}"
N_FAIL=$((++N_FAIL))
fi
done < <(cat "${FILES}")
if [ -z "${NO_SUMMARY}" ]; then
echo "OK: ${N_OK}, FAIL: ${N_FAIL}, TOTAL: ${N_TOTAL}"
fi
rm -f "${FILES}"
if [ -n "${STRICT}" ]; then
if [ "${N_FAIL}" != 0 ]; then
exit 3
fi
fi
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment