style.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. # Check if clang-format is installed, if not suggest installation using apt-get or apt
  34. echo "clang-format not found, install with 'apt-get install clang-format' or 'apt install clang-format'"
  35. exit 1
  36. fi
  37. # Set clang format version
  38. # Strip the clang-format version output down to the major version. Examples:
  39. # clang-format version 7.0.0 (tags/google/stable/2018-01-11)
  40. # clang-format version google3-trunk (trunk r333779)
  41. version=$("$clang_format_bin" --version)
  42. # Log the version in non-interactive use as it can be useful in travis logs.
  43. if [[ ! -t 1 ]]; then
  44. echo "Clang-format Path: $clang_format_bin"
  45. echo "Clang-format Version: $version"
  46. fi
  47. # Remove leading "clang-format version"
  48. version="${version/*version /}"
  49. # Remove trailing parenthetical version details
  50. version="${version/ (*)/}"
  51. # Remove everything after the first dot (note this is a glob, not a regex)
  52. version="${version/.*/}"
  53. case "$version" in
  54. 20)
  55. ;;
  56. google3-trunk)
  57. echo "Please use a publicly released clang-format; a recent LLVM release"
  58. echo "from http://releases.llvm.org/download.html will work."
  59. echo "Prepend to PATH when running style.sh."
  60. exit 1
  61. ;;
  62. *)
  63. echo "Please upgrade to clang-format version 20."
  64. echo "If it's installed via homebrew you can run:"
  65. echo "apt upgrade clang-format" #Suggest installing it via apt
  66. exit 1
  67. ;;
  68. esac
  69. # Ensure that tools in `Mintfile` are installed locally to avoid permissions
  70. # problems that would otherwise arise from the default of installing in
  71. # /usr/local.
  72. export MINT_PATH=Mint
  73. #Check if mint is installed, if not suggest installation using apt-get or apt.
  74. if ! which mint >/dev/null 2>&1; then
  75. echo "mint is not available, install with 'apt-get install mint' or 'apt install mint'"
  76. exit 1
  77. fi
  78. #Check if mint is installed, if not suggest installation using apt-get or apt.
  79. # Check if running on macOS
  80. if [[ "$system" == 'Darwin' ]]; then
  81. swift_is_available=true
  82. else
  83. swift_is_available=false
  84. fi
  85. # If the system is not macOS, swiftformat should not run.
  86. # Joins the given arguments with the separator given as the first argument.
  87. function join() {
  88. local IFS="$1"
  89. shift
  90. echo "$*"
  91. }
  92. clang_options=(-style=file)
  93. # Swift formatting options for the repo should be configured in
  94. # https://github.com/firebase/firebase-ios-sdk/blob/main/.swiftformat.
  95. # These may be overridden with additional `.swiftformat` files in subdirectories.
  96. swift_options=()
  97. if [[ $# -gt 0 && "$1" == "test-only" ]]; then
  98. test_only=true
  99. clang_options+=(-output-replacements-xml)
  100. swift_options+=(--dryrun)
  101. shift
  102. else
  103. test_only=false
  104. clang_options+=(-i)
  105. fi
  106. #TODO(#2223) - Find a way to handle spaces in filenames
  107. files=$(
  108. (
  109. if [[ $# -gt 0 ]]; then
  110. if git rev-parse "$1" -- >& /dev/null; then
  111. # Argument was a branch name show files changed since that branch
  112. git diff --name-only --relative --diff-filter=ACMR "$1"
  113. else
  114. # Otherwise assume the passed things are files or directories
  115. find "$@" -type f
  116. fi
  117. else
  118. # Do everything by default
  119. find . -type f
  120. fi
  121. ) | sed -E -n '
  122. # find . includes a leading "./" that git does not include
  123. s%^./%%
  124. # Build outputs
  125. \%/Pods/% d
  126. \%^build/% d
  127. \%^Debug/% d
  128. \%^Release/% d
  129. \%^cmake-build-debug/% d
  130. \%^.build/% d
  131. \%^.swiftpm/% d
  132. # pod gen output
  133. \%^gen/% d
  134. # FirestoreEncoder is under 'third_party' for licensing reasons but should be
  135. # formatted.
  136. \%Firestore/third_party/FirestoreEncoder/.*\.swift% p
  137. # Sources controlled outside this tree
  138. \%/third_party/% d
  139. # Public headers for closed sourced FirebaseAnalytics
  140. \%^(FirebaseAnalyticsWrapper)/% d
  141. # Generated source
  142. \%/Firestore/core/src/util/config.h% d
  143. # Generated Code for Data Connect sample
  144. \%/Examples/FriendlyFlix/app/FriendlyFlixSDK/% d
  145. # Sources pulled in by travis bundler, with and without a leading slash
  146. \%^/?vendor/bundle/% d
  147. # Sources pulled in by the Mint package manager
  148. \%^mint% d
  149. # Auth Sample Objective-C does not format well
  150. \%^(FirebaseAuth/Tests/Sample/Sample)/% d
  151. # Keep Firebase.h indenting
  152. \%^CoreOnly/Sources/Firebase.h% d
  153. # Checked-in generated code
  154. \%\.pb(objc|rpc)\.% d
  155. \%\.pb\.% d
  156. \%\.nanopb\.% d
  157. # Format C-ish sources only
  158. \%\.(h|m|mm|cc|swift)$% p
  159. '
  160. )
  161. needs_formatting=false
  162. for f in $files; do
  163. if [[ "${f: -6}" == '.swift' ]]; then
  164. if [[ "$system" == 'Darwin' ]]; then
  165. # Match output that says:
  166. # 1/1 files would have been formatted. (with --dryrun)
  167. # 1/1 files formatted. (without --dryrun)
  168. mint run swiftformat "${swift_options[@]}" "$f" 2>&1 | grep '^1/1 files' > /dev/null
  169. else
  170. false
  171. fi
  172. else
  173. "$clang_format_bin" "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  174. fi
  175. if [[ "$test_only" == true && $? -ne 1 ]]; then
  176. echo "$f needs formatting."
  177. needs_formatting=true
  178. fi
  179. done
  180. if [[ "$needs_formatting" == true ]]; then
  181. echo "Proposed commit is not style compliant."
  182. echo "Run scripts/style.sh and git add the result."
  183. exit 1
  184. fi