style.sh 5.5 KB

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