format.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. # Copyright 2020, gRPC Authors All rights reserved.
  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. set -eu
  16. function log() { printf -- "** %s\n" "$*" >&2; }
  17. function error() { printf -- "** ERROR: %s\n" "$*" >&2; }
  18. function fatal() { error "$*"; exit 1; }
  19. function usage() {
  20. echo >&2 "Usage:"
  21. echo >&2 " $0 -[f|l]"
  22. echo >&2 ""
  23. echo >&2 "Options:"
  24. echo >&2 " -f Format source code in place (default)"
  25. echo >&2 " -l Lint check without formatting the source code"
  26. }
  27. format=true
  28. lint=false
  29. while getopts ":flh" opt; do
  30. case "$opt" in
  31. f)
  32. format=true
  33. lint=false
  34. ;;
  35. l)
  36. format=false
  37. lint=true
  38. ;;
  39. h)
  40. usage
  41. exit 1
  42. ;;
  43. \?)
  44. usage
  45. exit 1
  46. ;;
  47. esac
  48. done
  49. THIS_SCRIPT=$0
  50. HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  51. REPO="$HERE/.."
  52. SWIFTFORMAT_DIR="$HERE/.swift-format-source"
  53. SWIFTFORMAT_VERSION=510.0.0
  54. # Clone SwiftFormat if we don't already have it.
  55. if [ ! -d "$SWIFTFORMAT_DIR" ]; then
  56. echo "- Cloning swift-format @ $SWIFTFORMAT_VERSION"
  57. git clone \
  58. --depth 1 \
  59. --branch "$SWIFTFORMAT_VERSION" \
  60. https://github.com/apple/swift-format.git \
  61. "$SWIFTFORMAT_DIR"
  62. fi
  63. cd "$SWIFTFORMAT_DIR"
  64. # Figure out the path for the binary.
  65. SWIFTFORMAT_BIN="$(swift build --show-bin-path -c release)/swift-format-$SWIFTFORMAT_VERSION"
  66. # Build it if we don't already have it.
  67. if [ ! -f "$SWIFTFORMAT_BIN" ]; then
  68. # We're not on the right tag, fetch and checkout the right one.
  69. echo "- Fetching swift-format @ $SWIFTFORMAT_VERSION"
  70. git fetch --depth 1 origin "refs/tags/$SWIFTFORMAT_VERSION:refs/tags/$SWIFTFORMAT_VERSION"
  71. git checkout "$SWIFTFORMAT_VERSION"
  72. # Now build and name the bin appropriately.
  73. echo "- Building swift-format @ $SWIFTFORMAT_VERSION"
  74. swift build -c release --product swift-format
  75. mv "$(swift build --show-bin-path -c release)/swift-format" "$SWIFTFORMAT_BIN"
  76. echo "- OK"
  77. fi
  78. if "$lint"; then
  79. "${SWIFTFORMAT_BIN}" lint \
  80. --parallel --recursive --strict \
  81. "${REPO}/Sources" \
  82. "${REPO}/Tests" \
  83. "${REPO}/Plugins" \
  84. "${REPO}/Performance/Benchmarks/Benchmarks/GRPCSwiftBenchmark" \
  85. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  86. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  87. fatal "Running swift-format produced errors.
  88. To fix, run the following command:
  89. % $THIS_SCRIPT -f
  90. "
  91. exit "${SWIFT_FORMAT_RC}"
  92. fi
  93. log "Ran swift-format lint with no errors."
  94. elif "$format"; then
  95. "${SWIFTFORMAT_BIN}" format \
  96. --parallel --recursive --in-place \
  97. "${REPO}/Sources" \
  98. "${REPO}/Tests" \
  99. "${REPO}/Plugins" \
  100. "${REPO}/Performance/Benchmarks/Benchmarks/GRPCSwiftBenchmark" \
  101. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  102. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  103. fatal "Running swift-format produced errors." "${SWIFT_FORMAT_RC}"
  104. fi
  105. log "Ran swift-format with no errors."
  106. else
  107. fatal "No actions taken."
  108. fi