bazel-format.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env bash
  2. # Before running this script, please make sure golang is installed
  3. # and buildifier is also installed. The example is showed in .travis.yml.
  4. set -e
  5. ROOT_DIR=$(cd "$(dirname "$0")/$(dirname "$(test -L "$0" && readlink "$0" || echo "/")")"; pwd)
  6. function usage()
  7. {
  8. echo "Usage: bazel-format.sh [<args>]"
  9. echo
  10. echo "Options:"
  11. echo " -h|--help print the help info"
  12. echo " -c|--check check whether there are format issues in bazel files"
  13. echo " -f|--fix fix all the format issue directly"
  14. echo
  15. }
  16. RUN_TYPE="diff"
  17. # Parse options
  18. while [ $# -gt 0 ]; do
  19. key="$1"
  20. case $key in
  21. -h|--help)
  22. usage
  23. exit 0
  24. ;;
  25. -c|--check)
  26. RUN_TYPE="diff"
  27. ;;
  28. -f|--fix)
  29. RUN_TYPE=fix
  30. ;;
  31. *)
  32. echo "ERROR: unknown option \"$key\""
  33. echo
  34. usage
  35. exit 1
  36. ;;
  37. esac
  38. shift
  39. done
  40. pushd "$ROOT_DIR"/../..
  41. BAZEL_FILES=(bazel/BUILD bazel/ray.bzl BUILD.bazel java/BUILD.bazel \
  42. cpp/BUILD.bazel cpp/example/BUILD.bazel WORKSPACE)
  43. buildifier -mode=$RUN_TYPE -diff_command="diff -u" "${BAZEL_FILES[@]}"
  44. popd