style.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. 9)
  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 9."
  46. echo "If it's installed via homebrew you can run:"
  47. echo "brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/c6f1cbd/Formula/clang-format.rb"
  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 at least 0.35.x since (as of 2019-02-01)
  60. # travis runs 0.35.7. We may need to be more strict about version checks in
  61. # the future if we run into different versions making incompatible format
  62. # changes.
  63. if [[ ! "$version" =~ ^0.[4-9] ]]; then
  64. echo "Version $version installed. Please upgrade to at least swiftformat 0.44.6"
  65. echo "If it's installed via homebrew you can run: brew upgrade swiftformat"
  66. exit 1
  67. fi
  68. fi
  69. # Joins the given arguments with the separator given as the first argument.
  70. function join() {
  71. local IFS="$1"
  72. shift
  73. echo "$*"
  74. }
  75. clang_options=(-style=file)
  76. # Rules to disable in swiftformat:
  77. swift_disable=(
  78. # sortedImports is broken, sorting into the middle of the copyright notice.
  79. sortedImports
  80. # Too many of our swift files have simplistic examples. While technically
  81. # it's correct to remove the unused argument labels, it makes our examples
  82. # look wrong.
  83. unusedArguments
  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. # pod gen output
  125. \%^gen/% d
  126. # FirestoreEncoder is under 'third_party' for licensing reasons but should be
  127. # formatted.
  128. \%Firestore/third_party/FirestoreEncoder/.*\.swift% p
  129. # Sources controlled outside this tree
  130. \%/third_party/% d
  131. # Generated source
  132. \%/Firestore/core/src/util/config.h% d
  133. # Sources pulled in by travis bundler, with and without a leading slash
  134. \%^/?vendor/bundle/% d
  135. # Auth Sample is not subject to formatting
  136. \%^(FirebaseAuth/Tests/Sample)/% d
  137. # Keep Firebase.h indenting
  138. \%^CoreOnly/Sources/Firebase.h% d
  139. # Checked-in generated code
  140. \%\.pb(objc|rpc)\.% d
  141. \%\.pb\.% d
  142. \%\.nanopb\.% d
  143. # Format C-ish sources only
  144. \%\.(h|m|mm|cc|swift)$% p
  145. '
  146. )
  147. needs_formatting=false
  148. for f in $files; do
  149. if [[ "${f: -6}" == '.swift' ]]; then
  150. if [[ "$system" == 'Darwin' ]]; then
  151. # Match output that says:
  152. # 1/1 files would have been formatted. (with --dryrun)
  153. # 1/1 files formatted. (without --dryrun)
  154. swiftformat "${swift_options[@]}" "$f" 2>&1 | grep '^1/1 files' > /dev/null
  155. else
  156. false
  157. fi
  158. else
  159. clang-format "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  160. fi
  161. if [[ "$test_only" == true && $? -ne 1 ]]; then
  162. echo "$f needs formatting."
  163. needs_formatting=true
  164. fi
  165. done
  166. if [[ "$needs_formatting" == true ]]; then
  167. echo "Proposed commit is not style compliant."
  168. echo "Run scripts/style.sh and git add the result."
  169. exit 1
  170. fi