style.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 18)
  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 18."
  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. # Rules to disable in swiftformat:
  84. swift_disable=(
  85. # sortedImports is broken, sorting into the middle of the copyright notice.
  86. sortedImports
  87. # Too many of our swift files have simplistic examples. While technically
  88. # it's correct to remove the unused argument labels, it makes our examples
  89. # look wrong.
  90. unusedArguments
  91. # We prefer trailing braces.
  92. wrapMultilineStatementBraces
  93. )
  94. swift_options=(
  95. # Mimic Objective-C style.
  96. --indent 2
  97. --maxwidth 100
  98. --wrapparameters afterfirst
  99. --disable $(join , "${swift_disable[@]}")
  100. )
  101. if [[ $# -gt 0 && "$1" == "test-only" ]]; then
  102. test_only=true
  103. clang_options+=(-output-replacements-xml)
  104. swift_options+=(--dryrun)
  105. shift
  106. else
  107. test_only=false
  108. clang_options+=(-i)
  109. fi
  110. #TODO(#2223) - Find a way to handle spaces in filenames
  111. files=$(
  112. (
  113. if [[ $# -gt 0 ]]; then
  114. if git rev-parse "$1" -- >& /dev/null; then
  115. # Argument was a branch name show files changed since that branch
  116. git diff --name-only --relative --diff-filter=ACMR "$1"
  117. else
  118. # Otherwise assume the passed things are files or directories
  119. find "$@" -type f
  120. fi
  121. else
  122. # Do everything by default
  123. find . -type f
  124. fi
  125. ) | sed -E -n '
  126. # find . includes a leading "./" that git does not include
  127. s%^./%%
  128. # Build outputs
  129. \%/Pods/% d
  130. \%^build/% d
  131. \%^Debug/% d
  132. \%^Release/% d
  133. \%^cmake-build-debug/% d
  134. \%^.build/% d
  135. \%^.swiftpm/% d
  136. # pod gen output
  137. \%^gen/% d
  138. # FirestoreEncoder is under 'third_party' for licensing reasons but should be
  139. # formatted.
  140. \%Firestore/third_party/FirestoreEncoder/.*\.swift% p
  141. # Sources controlled outside this tree
  142. \%/third_party/% d
  143. # Public headers for closed sourced FirebaseAnalytics
  144. \%^(FirebaseAnalyticsWrapper)/% d
  145. # Generated source
  146. \%/Firestore/core/src/util/config.h% d
  147. # Sources pulled in by travis bundler, with and without a leading slash
  148. \%^/?vendor/bundle/% d
  149. # Sources pulled in by the Mint package manager
  150. \%^mint% d
  151. # Auth Sample Objective-C does not format well
  152. \%^(FirebaseAuth/Tests/Sample/Sample)/% d
  153. # Keep Firebase.h indenting
  154. \%^CoreOnly/Sources/Firebase.h% d
  155. # Checked-in generated code
  156. \%\.pb(objc|rpc)\.% d
  157. \%\.pb\.% d
  158. \%\.nanopb\.% d
  159. # Format C-ish sources only
  160. \%\.(h|m|mm|cc|swift)$% p
  161. '
  162. )
  163. needs_formatting=false
  164. for f in $files; do
  165. if [[ "${f: -6}" == '.swift' ]]; then
  166. if [[ "$system" == 'Darwin' ]]; then
  167. # Match output that says:
  168. # 1/1 files would have been formatted. (with --dryrun)
  169. # 1/1 files formatted. (without --dryrun)
  170. mint run swiftformat "${swift_options[@]}" "$f" 2>&1 | grep '^1/1 files' > /dev/null
  171. else
  172. false
  173. fi
  174. else
  175. "$clang_format_bin" "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  176. fi
  177. if [[ "$test_only" == true && $? -ne 1 ]]; then
  178. echo "$f needs formatting."
  179. needs_formatting=true
  180. fi
  181. done
  182. if [[ "$needs_formatting" == true ]]; then
  183. echo "Proposed commit is not style compliant."
  184. echo "Run scripts/style.sh and git add the result."
  185. exit 1
  186. fi