shellcheck_tree.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2020 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. me=$(basename "$0")
  18. # Run shellcheck on all the shell-ish files in the tree
  19. patterns=('*.bash*' 'bash*'
  20. '*.ksh*' 'ksh*'
  21. '*.zsh*' 'zsh*'
  22. '.zlogin*' 'zlogin*'
  23. '.zlogout*' 'zlogout*'
  24. '.zprofile*' 'zprofile*'
  25. '*.sh' '.shlib*' 'shlib*'
  26. '.profile*' 'profile*'
  27. 'suid_profile')
  28. # excluding these directories (e.g. third_party/* )
  29. excludes=()
  30. # shellcheck disable=SC1091
  31. [[ -f .shellcheck_tree ]] && . .shellcheck_tree
  32. [[ ${*/--help//} != "${*}" ]] && {
  33. echo "Usage: $me <OPTIONS>
  34. Shellcheck all scripts in the tree.
  35. Options:
  36. --git shellcheck via git ls-files, default is 'find .'
  37. --help get this message
  38. "
  39. exit 0
  40. }
  41. if [[ ${*/--git//} != "${*}" ]]; then
  42. #
  43. # To run on git-files only
  44. #
  45. git ls-files -- "${patterns[@]}" | while read -r file; do
  46. for exclude in "${excludes[@]}"; do
  47. [[ $file =~ $exclude ]] && continue 2
  48. done
  49. shellcheck -f gcc "$file"
  50. done
  51. else
  52. #
  53. # use find
  54. #
  55. exclude_args=()
  56. for exclude in "${excludes[@]}"; do
  57. exclude_args+=('!' -path "./$exclude" -a)
  58. done
  59. pattern_args=()
  60. for pattern in "${patterns[@]}"; do
  61. pattern_args+=(-o -name "$pattern")
  62. done
  63. find . "${exclude_args[@]}" '(' "${pattern_args[@]:1}" ')' -type f -exec shellcheck -f gcc {} +
  64. fi