style.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/bash
  2. # Copyright 2017 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Usage:
  16. # ./scripts/style.sh [branch-name | filenames]
  17. #
  18. # With no arguments, formats all eligible files in the repo
  19. # Pass a branch name to format all eligible files changed since that branch
  20. # Pass a specific file or directory name to format just files found there
  21. #
  22. # Commonly
  23. # ./scripts/style.sh main
  24. # Set the environment variable FIR_CLANG_FORMAT_PATH to use a specific version
  25. # of clang-format, regardless of its order in the shell PATH.
  26. #
  27. # Example (add to ~/.bash_profile, ~/.zshrc or run in interactive shell):
  28. # FIR_CLANG_FORMAT_PATH="$(brew --prefix clang-format)/bin/clang-format"
  29. # export FIR_CLANG_FORMAT_PATH
  30. if [[ -n "$FIR_CLANG_FORMAT_PATH" ]]; then
  31. clang_format_bin="$FIR_CLANG_FORMAT_PATH"
  32. elif ! clang_format_bin=$(command -v clang-format); then
  33. echo "clang-format not found, install with 'brew install clang-format'"
  34. exit 1
  35. fi
  36. # Strip the clang-format version output down to the major version. Examples:
  37. # clang-format version 7.0.0 (tags/google/stable/2018-01-11)
  38. # clang-format version google3-trunk (trunk r333779)
  39. version=$("$clang_format_bin" --version)
  40. # Log the version in non-interactive use as it can be useful in travis logs.
  41. if [[ ! -t 1 ]]; then
  42. echo "Clang-format Path: $clang_format_bin"
  43. echo "Clang-format Version: $version"
  44. fi
  45. # Remove leading "clang-format version"
  46. version="${version/*version /}"
  47. # Remove trailing parenthetical version details
  48. version="${version/ (*)/}"
  49. # Remove everything after the first dot (note this is a glob, not a regex)
  50. version="${version/.*/}"
  51. case "$version" in
  52. 21)
  53. ;;
  54. google3-trunk)
  55. echo "Please use a publicly released clang-format; a recent LLVM release"
  56. echo "from http://releases.llvm.org/download.html will work."
  57. echo "Prepend to PATH when running style.sh."
  58. exit 1
  59. ;;
  60. *)
  61. echo "Please upgrade to clang-format version 21."
  62. echo "If it's installed via homebrew you can run:"
  63. echo "brew upgrade clang-format"
  64. exit 1
  65. ;;
  66. esac
  67. # Ensure that tools in `Mintfile` are installed locally to avoid permissions
  68. # problems that would otherwise arise from the default of installing in
  69. # /usr/local.
  70. export MINT_PATH=Mint
  71. if ! which mint >/dev/null 2>&1; then
  72. echo "mint is not available, install with 'brew install mint'"
  73. exit 1
  74. fi
  75. system=$(uname -s)
  76. # Joins the given arguments with the separator given as the first argument.
  77. function join() {
  78. local IFS="$1"
  79. shift
  80. echo "$*"
  81. }
  82. clang_options=(-style=file)
  83. # Swift formatting options for the repo should be configured in
  84. # https://github.com/firebase/firebase-ios-sdk/blob/main/.swiftformat.
  85. # These may be overridden with additional `.swiftformat` files in subdirectories.
  86. swift_options=()
  87. if [[ $# -gt 0 && "$1" == "test-only" ]]; then
  88. test_only=true
  89. clang_options+=(-output-replacements-xml)
  90. swift_options+=(--dryrun)
  91. shift
  92. else
  93. test_only=false
  94. clang_options+=(-i)
  95. fi
  96. #TODO(#2223) - Find a way to handle spaces in filenames
  97. files=$(
  98. (
  99. if [[ $# -gt 0 ]]; then
  100. if git rev-parse "$1" -- >& /dev/null; then
  101. # Argument was a branch name show files changed since that branch
  102. git diff --name-only --relative --diff-filter=ACMR "$1"
  103. else
  104. # Otherwise assume the passed things are files or directories
  105. find "$@" -type f
  106. fi
  107. else
  108. # Do everything by default
  109. find . -type f
  110. fi
  111. ) | sed -E -n '
  112. # find . includes a leading "./" that git does not include
  113. s%^./%%
  114. # Build outputs
  115. \%/Pods/% d
  116. \%^build/% d
  117. \%^Debug/% d
  118. \%^Release/% d
  119. \%^cmake-build-debug/% d
  120. \%^.build/% d
  121. \%^.swiftpm/% d
  122. # pod gen output
  123. \%^gen/% d
  124. # FirestoreEncoder is under 'third_party' for licensing reasons but should be
  125. # formatted.
  126. \%Firestore/third_party/FirestoreEncoder/.*\.swift% p
  127. # Sources controlled outside this tree
  128. \%/third_party/% d
  129. # Public headers for closed sourced FirebaseAnalytics
  130. \%^(FirebaseAnalyticsWrapper)/% d
  131. # Generated source
  132. \%/Firestore/core/src/util/config.h% d
  133. # Generated Code for Data Connect sample
  134. \%/Examples/FriendlyFlix/app/FriendlyFlixSDK/% d
  135. # Sources pulled in by travis bundler, with and without a leading slash
  136. \%^/?vendor/bundle/% d
  137. # Sources pulled in by the Mint package manager
  138. \%^mint% d
  139. # Auth Sample Objective-C does not format well
  140. \%^(FirebaseAuth/Tests/Sample/Sample)/% d
  141. # Keep Firebase.h indenting
  142. \%^CoreOnly/Sources/Firebase.h% d
  143. # Checked-in generated code
  144. \%\.pb(objc|rpc)\.% d
  145. \%\.pb\.% d
  146. \%\.nanopb\.% d
  147. # Format C-ish sources only
  148. \%\.(h|m|mm|cc|swift)$% p
  149. '
  150. )
  151. needs_formatting=false
  152. for f in $files; do
  153. if [[ "${f: -6}" == '.swift' ]]; then
  154. # Match output that says:
  155. # 1/1 files would have been formatted. (with --dryrun)
  156. # 1/1 files formatted. (without --dryrun)
  157. mint run swiftformat "${swift_options[@]}" "$f" 2>&1 | grep '^1/1 files' > /dev/null
  158. else
  159. "$clang_format_bin" "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  160. fi
  161. if [[ "$test_only" == true && $? -ne 1 ]]; then
  162. echo "$f needs formatting."
  163. needs_formatting=true
  164. fi
  165. done
  166. if [[ "$needs_formatting" == true ]]; then
  167. echo "Proposed commit is not style compliant."
  168. echo "Run scripts/style.sh and git add the result."
  169. exit 1
  170. fi