autoformat.sh 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. folderArray=( "daemon" "cli" "gui" )
  3. # Variable that will hold the name of the clang-format command
  4. FMT=""
  5. # Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent
  6. # that the version number be part of the command. We prefer clang-format if
  7. # that's present, otherwise we work backwards from highest version to lowest
  8. # version.
  9. for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do
  10. if which "$clangfmt" &>/dev/null; then
  11. FMT="$clangfmt"
  12. break
  13. fi
  14. done
  15. # Check if we found a working clang-format
  16. if [ -z "$FMT" ]; then
  17. echo "failed to find clang-format"
  18. exit 1
  19. fi
  20. function format() {
  21. for f in $(find $@ -type d -path "$@/build" -prune -o -type f -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do
  22. if [ ! -d "${f}" ]; then
  23. echo "format ${f}";
  24. ${FMT} -i ${f};
  25. fi
  26. done
  27. echo "~~~ $@ Done ~~~";
  28. }
  29. # Check all of the arguments first to make sure they're all directories
  30. for dir in ${folderArray[@]}; do
  31. if [ ! -d "${dir}" ]; then
  32. echo "${dir} is not a directory";
  33. else
  34. format ${dir};
  35. fi
  36. done