style.sh 5.2 KB

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