format.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  50. repo="$here/.."
  51. if "$lint"; then
  52. swift format lint \
  53. --parallel --recursive --strict \
  54. "${repo}/Sources" \
  55. "${repo}/Tests" \
  56. "${repo}/Examples" \
  57. "${repo}/IntegrationTests/Benchmarks/Benchmarks/GRPCSwiftBenchmark" \
  58. "${repo}/dev" \
  59. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  60. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  61. fatal "Running swift format produced errors.
  62. To fix, run the following command:
  63. % $here/format.sh -f
  64. " "${SWIFT_FORMAT_RC}"
  65. fi
  66. log "Ran swift format lint with no errors."
  67. elif "$format"; then
  68. swift format \
  69. --parallel --recursive --in-place \
  70. "${repo}/Sources" \
  71. "${repo}/Tests" \
  72. "${repo}/Examples" \
  73. "${repo}/IntegrationTests/Benchmarks/Benchmarks/GRPCSwiftBenchmark" \
  74. "${repo}/dev" \
  75. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  76. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  77. fatal "Running swift format produced errors." "${SWIFT_FORMAT_RC}"
  78. fi
  79. log "Ran swift format with no errors."
  80. else
  81. fatal "No actions taken."
  82. fi