style.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. system=$(uname -s)
  22. if [[ $(clang-format --version) != *"version 6"* ]]; then
  23. echo "Please upgrade to clang-format version 6."
  24. echo "If it's installed via homebrew you can run: brew upgrade clang-format"
  25. exit 1
  26. fi
  27. if [[ "$system" == "Darwin" ]]; then
  28. version=$(swiftformat --version)
  29. version="${version/*version /}"
  30. # Allow an older swiftformat because travis isn't running High Sierra yet
  31. # and the formula hasn't been updated in a while on Sierra :-/.
  32. if [[ "$version" != "0.32.0" && "$version" != "0.33"* ]]; then
  33. echo "Please upgrade to swiftformat 0.33.3"
  34. echo "If it's installed via homebrew you can run: brew upgrade swiftformat"
  35. exit 1
  36. fi
  37. fi
  38. # Joins the given arguments with the separator given as the first argument.
  39. function join() {
  40. local IFS="$1"
  41. shift
  42. echo "$*"
  43. }
  44. clang_options=(-style=file)
  45. # Rules to disable in swiftformat:
  46. swift_disable=(
  47. # sortedImports is broken, sorting into the middle of the copyright notice.
  48. sortedImports
  49. # Too many of our swift files have simplistic examples. While technically
  50. # it's correct to remove the unused argument labels, it makes our examples
  51. # look wrong.
  52. unusedArguments
  53. )
  54. swift_options=(
  55. # Mimic Objective-C style.
  56. --indent 2
  57. --disable $(join , "${swift_disable[@]}")
  58. )
  59. if [[ $# -gt 0 && "$1" == "test-only" ]]; then
  60. test_only=true
  61. clang_options+=(-output-replacements-xml)
  62. swift_options+=(--dryrun)
  63. shift
  64. else
  65. test_only=false
  66. clang_options+=(-i)
  67. fi
  68. files=$(
  69. (
  70. if [[ $# -gt 0 ]]; then
  71. if git rev-parse "$1" -- >& /dev/null; then
  72. # Argument was a branch name show files changed since that branch
  73. git diff --name-only --relative --diff-filter=ACMR "$1"
  74. else
  75. # Otherwise assume the passed things are files or directories
  76. find "$@" -type f
  77. fi
  78. else
  79. # Do everything by default
  80. find . -type f
  81. fi
  82. ) | sed -E -n '
  83. # Build outputs
  84. \%/Pods/% d
  85. \%^./build/% d
  86. \%^./Debug/% d
  87. \%^./Release/% d
  88. # Sources controlled outside this tree
  89. \%/third_party/% d
  90. \%/Firestore/Port/% d
  91. # Generated source
  92. \%/Firestore/core/src/firebase/firestore/util/config.h% d
  93. # Sources pulled in by travis bundler
  94. \%/vendor/bundle/% d
  95. # Sources within the tree that are not subject to formatting
  96. \%^./(Example|Firebase)/(Auth|AuthSamples|Database|Messaging)/% d
  97. # Checked-in generated code
  98. \%\.pb(objc|rpc)\.% d
  99. \%\.pb\.% d
  100. # Format C-ish sources only
  101. \%\.(h|m|mm|cc|swift)$% p
  102. '
  103. )
  104. needs_formatting=false
  105. for f in $files; do
  106. if [[ "${f: -6}" == '.swift' ]]; then
  107. if [[ "$system" == 'Darwin' ]]; then
  108. swiftformat "${swift_options[@]}" "$f" 2> /dev/null | grep 'would have updated' > /dev/null
  109. else
  110. false
  111. fi
  112. else
  113. clang-format "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
  114. fi
  115. if [[ "$test_only" == true && $? -ne 1 ]]; then
  116. echo "$f needs formatting."
  117. needs_formatting=true
  118. fi
  119. done
  120. if [[ "$needs_formatting" == true ]]; then
  121. echo "Proposed commit is not style compliant."
  122. echo "Run scripts/style.sh and git add the result."
  123. exit 1
  124. fi