2
0

format.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. # Copyright The OpenTelemetry Authors
  3. # SPDX-License-Identifier: Apache-2.0
  4. if [[ ! -e tools/format.sh ]]; then
  5. echo "This tool must be run from the topmost directory." >&2
  6. exit 1
  7. fi
  8. set -e
  9. FIND="find . -name third_party -prune -o -name tools -prune -o -name .git -prune -o -name _deps -prune -o -name .build -prune -o -name out -prune -o -name .vs -prune -o -name opentelemetry_logo.png -prune -o -name TraceLoggingDynamic.h -prune -o"
  10. # GNU syntax.
  11. SED=(sed -i)
  12. if [[ "$(uname)" = "Darwin" ]]; then
  13. SED=(sed -i "")
  14. fi
  15. # Correct common miscapitalizations.
  16. "${SED[@]}" 's/Open[t]elemetry/OpenTelemetry/g' $($FIND -type f -print)
  17. # No CRLF line endings, except Windows files.
  18. "${SED[@]}" 's/\r$//' $($FIND -name '*.ps1' -prune -o \
  19. -name '*.cmd' -prune -o -type f -print)
  20. # No trailing spaces, except in patch.
  21. "${SED[@]}" 's/ \+$//' $($FIND -name "*.patch" -prune -o -type f -print)
  22. # If not overridden, try to use clang-format-18 or clang-format.
  23. if [[ -z "$CLANG_FORMAT" ]]; then
  24. CLANG_FORMAT=clang-format
  25. if which clang-format-18 >/dev/null; then
  26. CLANG_FORMAT=clang-format-18
  27. fi
  28. fi
  29. $CLANG_FORMAT -version
  30. $CLANG_FORMAT -i -style=file \
  31. $($FIND -name '*.cc' -print -o -name '*.h' -print)
  32. if which cmake-format >/dev/null; then
  33. echo "Running cmake-format $(cmake-format --version 2>&1)."
  34. cmake-format -i $($FIND -name 'CMakeLists.txt' -print -name '*.cmake' -print -name '*.cmake.in' -print)
  35. else
  36. echo "Can't find cmake-format. It can be installed with:"
  37. echo " pip install --user cmake_format"
  38. exit 1
  39. fi
  40. if [[ -z "$BUILDIFIER" ]]; then
  41. BUILDIFIER="$HOME/go/bin/buildifier"
  42. if ! which "$BUILDIFIER" >/dev/null; then
  43. BUILDIFIER=buildifier
  44. fi
  45. fi
  46. if which "$BUILDIFIER" >/dev/null; then
  47. echo "Running $BUILDIFIER"
  48. "$BUILDIFIER" $($FIND -name WORKSPACE -print -o -name BUILD -print -o \
  49. -name '*.BUILD' -o -name '*.bzl' -print)
  50. else
  51. echo "Can't find buildifier. It can be installed with:"
  52. echo " go get github.com/bazelbuild/buildtools/buildifier"
  53. exit 1
  54. fi