shellchecks.sh 1.0 KB

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env bash
  2. # Based on https://github.com/koreader/koreader/blob/master/.ci/helper_shellchecks.sh
  3. ANSI_RED="\\033[31;1m"
  4. ANSI_GREEN="\\033[32;1m"
  5. ANSI_RESET="\\033[0m"
  6. mapfile -t shellscript_locations < <({ git grep -lE '^#!(/usr)?/bin/(env )?(bash|sh)' && git ls-files ./*.sh; } | sort | uniq)
  7. SHELLSCRIPT_ERROR=0
  8. for shellscript in "${shellscript_locations[@]}"; do
  9. echo -e "${ANSI_GREEN}Running shellcheck on ${shellscript}"
  10. shellcheck "${shellscript}" || SHELLSCRIPT_ERROR=1
  11. echo -e "${ANSI_GREEN}Running shfmt on ${shellscript}"
  12. if ! shfmt "${shellscript}" >/dev/null 2>&1; then
  13. echo -e "${ANSI_RED}Warning: ${shellscript} contains the following problem:"
  14. shfmt "${shellscript}" || SHELLSCRIPT_ERROR=1
  15. continue
  16. fi
  17. if [ "$(cat "${shellscript}")" != "$(shfmt "${shellscript}")" ]; then
  18. echo -e "${ANSI_RED}Warning: ${shellscript} does not abide by coding style, diff for expected style:"
  19. shfmt "${shellscript}" | diff "${shellscript}" - || SHELLSCRIPT_ERROR=1
  20. fi
  21. done
  22. echo -ne "${ANSI_RESET}"
  23. exit "${SHELLSCRIPT_ERROR}"