style.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 master
  24. # Strip the clang-format version output down to the major version. Examples:
  25. # clang-format version 7.0.0 (tags/google/stable/2018-01-11)
  26. # clang-format version google3-trunk (trunk r333779)
  27. version=$(clang-format --version)
  28. # Log the version in non-interactive use as it can be useful in travis logs.
  29. if [[ ! -t 1 ]]; then
  30. echo "Found: $version"
  31. fi
  32. # Remove leading "clang-format version"
  33. version="${version/*version /}"
  34. # Remove trailing parenthetical version details
  35. version="${version/ (*)/}"
  36. # Remove everything after the first dot (note this is a glob, not a regex)
  37. version="${version/.*/}"
  38. case "$version" in
  39. 16)
  40. ;;
  41. google3-trunk)
  42. echo "Please use a publicly released clang-format; a recent LLVM release"
  43. echo "from http://releases.llvm.org/download.html will work."
  44. echo "Prepend to PATH when running style.sh."
  45. exit 1
  46. ;;
  47. *)
  48. echo "Please upgrade to clang-format version 16."
  49. echo "If it's installed via homebrew you can run:"
  50. echo "brew upgrade clang-format"
  51. exit 1
  52. ;;
  53. esac
  54. # Ensure that tools in `Mintfile` are installed locally to avoid permissions
  55. # problems that would otherwise arise from the default of installing in
  56. # /usr/local.
  57. export MINT_PATH=Mint
  58. if ! which mint >/dev/null 2>&1; then
  59. echo "mint is not available, install with 'brew install mint'"
  60. exit 1
  61. fi
  62. system=$(uname -s)
  63. # Joins the given arguments with the separator given as the first argument.
  64. function join() {
  65. local IFS="$1"
  66. shift
  67. echo "$*"
  68. }
  69. clang_options=(-style=file)
  70. # Rules to disable in swiftformat:
  71. swift_disable=(
  72. # sortedImports is broken, sorting into the middle of the copyright notice.
  73. sortedImports
  74. # Too many of our swift files have simplistic examples. While technically
  75. # it's correct to remove the unused argument labels, it makes our examples
  76. # look wrong.
  77. unusedArguments
  78. # We prefer trailing braces.
  79. wrapMultilineStatementBraces
  80. )
  81. swift_options=(
  82. # Mimic Objective-C style.
  83. --indent 2
  84. --maxwidth 100
  85. --wrapparameters afterfirst
  86. --disable $(join , "${swift_disable[@]}")
  87. )
  88. if [[ $# -gt 0 && "$1" == "test-only" ]]; then
  89. test_only=true
  90. clang_options+=(-output-replacements-xml)
  91. swift_options+=(--dryrun)
  92. shift
  93. else
  94. test_only=false
  95. clang_options+=(-i)
  96. fi
  97. #TODO(#2223) - Find a way to handle spaces in filenames
  98. files=$(
  99. (
  100. if [[ $# -gt 0 ]]; then
  101. if git rev-parse "$1" -- >& /dev/null; then
  102. # Argument was a branch name show files changed since that branch
  103. git diff --name-only --relative --diff-filter=ACMR "$1"
  104. else
  105. # Otherwise assume the passed things are files or directories
  106. find "$@" -type f
  107. fi
  108. else
  109. # Do everything by default
  110. find . -type f
  111. fi
  112. ) | sed -E -n '
  113. # find . includes a leading "./" that git does not include
  114. s%^./%%
  115. # Build outputs
  116. \%/Pods/% d
  117. \%^build/% d
  118. \%^Debug/% d
  119. \%^Release/% d
  120. \%^cmake-build-debug/% d
  121. \%^.build/% d
  122. \%^.swiftpm/% d
  123. # pod gen output
  124. \%^gen/% d
  125. # FirestoreEncoder is under 'third_party' for licensing reasons but should be
  126. # formatted.
  127. \%Firestore/third_party/FirestoreEncoder/.*\.swift% p
  128. # Sources controlled outside this tree
  129. \%/third_party/% d
  130. # Public headers for closed sourced FirebaseAnalytics
  131. \%^(FirebaseAnalyticsWrapper)/% d
  132. # Generated source
  133. \%/Firestore/core/src/util/config.h% d
  134. # Sources pulled in by travis bundler, with and without a leading slash
  135. \%^/?vendor/bundle/% d
  136. # Sources pulled in by the Mint package manager
  137. \%^mint% d
  138. # Auth Sample Objective-C does not format well
  139. \%^(FirebaseAuth/Tests/Sample/Sample)/% d
  140. # Keep Firebase.h indenting
  141. \%^CoreOnly/Sources/Firebase.h% d
  142. # Checked-in generated code
  143. \%\.pb(objc|rpc)\.% d
  144. \%\.pb\.% d
  145. \%\.nanopb\.% d
  146. # Format C-ish sources only
  147. \%\.(h|m|mm|cc|swift)$% p
  148. '
  149. )
  150. needs_formatting=false
  151. for f in $files; do
  152. if [[ "${f: -6}" == '.swift' ]]; then
  153. if [[ "$system" == 'Darwin' ]]; 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. false
  160. fi
  161. else
  162. clang-format "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  163. fi
  164. if [[ "$test_only" == true && $? -ne 1 ]]; then
  165. echo "$f needs formatting."
  166. needs_formatting=true
  167. fi
  168. done
  169. if [[ "$needs_formatting" == true ]]; then
  170. echo "Proposed commit is not style compliant."
  171. echo "Run scripts/style.sh and git add the result."
  172. exit 1
  173. fi