format.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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"
  25. echo >&2 " -l Lint check without formatting the source code"
  26. }
  27. lint=false
  28. while getopts ":lh" opt; do
  29. case "$opt" in
  30. l)
  31. lint=true
  32. ;;
  33. h)
  34. usage
  35. exit 1
  36. ;;
  37. \?)
  38. usage
  39. exit 1
  40. ;;
  41. esac
  42. done
  43. THIS_SCRIPT=$0
  44. HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  45. REPO="$HERE/.."
  46. SWIFTFORMAT_DIR="$HERE/.swift-format-source"
  47. SWIFTFORMAT_VERSION=509.0.0
  48. # Clone SwiftFormat if we don't already have it.
  49. if [ ! -d "$SWIFTFORMAT_DIR" ]; then
  50. echo "- Cloning swift-format @ $SWIFTFORMAT_VERSION"
  51. git clone \
  52. --depth 1 \
  53. --branch "$SWIFTFORMAT_VERSION" \
  54. https://github.com/apple/swift-format.git \
  55. "$SWIFTFORMAT_DIR"
  56. fi
  57. cd "$SWIFTFORMAT_DIR"
  58. # Figure out the path for the binary.
  59. SWIFTFORMAT_BIN="$(swift build --show-bin-path -c release)/swift-format-$SWIFTFORMAT_VERSION"
  60. # Build it if we don't already have it.
  61. if [ ! -f "$SWIFTFORMAT_BIN" ]; then
  62. # We're not on the right tag, fetch and checkout the right one.
  63. echo "- Fetching swift-format @ $SWIFTFORMAT_VERSION"
  64. git fetch --depth 1 origin "refs/tags/$SWIFTFORMAT_VERSION:refs/tags/$SWIFTFORMAT_VERSION"
  65. git checkout "$SWIFTFORMAT_VERSION"
  66. # Now build and name the bin appropriately.
  67. echo "- Building swift-format @ $SWIFTFORMAT_VERSION"
  68. swift build -c release --product swift-format
  69. mv "$(swift build --show-bin-path -c release)/swift-format" "$SWIFTFORMAT_BIN"
  70. echo "- OK"
  71. fi
  72. if "$lint"; then
  73. "${SWIFTFORMAT_BIN}" lint \
  74. --parallel --recursive --strict \
  75. "${REPO}/Sources" "${REPO}/Tests" \
  76. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  77. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  78. fatal "Running swift-format produced errors.
  79. To fix, run the following command:
  80. % $THIS_SCRIPT -f
  81. "
  82. exit "${SWIFT_FORMAT_RC}"
  83. fi
  84. log "Ran swift-format lint with no errors."
  85. else
  86. "${SWIFTFORMAT_BIN}" format \
  87. --parallel --recursive --in-place \
  88. "${REPO}/Sources" "${REPO}/Tests" \
  89. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  90. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  91. fatal "Running swift-format produced errors." "${SWIFT_FORMAT_RC}"
  92. fi
  93. log "Ran swift-format with no errors."
  94. fi