format.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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}/IntegrationTests" \
  57. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  58. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  59. fatal "Running swift format produced errors.
  60. To fix, run the following command:
  61. % $THIS_SCRIPT -f
  62. " "${SWIFT_FORMAT_RC}"
  63. fi
  64. log "Ran swift format lint with no errors."
  65. elif "$format"; then
  66. swift format \
  67. --parallel --recursive --in-place \
  68. "${repo}/Sources" \
  69. "${repo}/Tests" \
  70. "${repo}/IntegrationTests" \
  71. && SWIFT_FORMAT_RC=$? || SWIFT_FORMAT_RC=$?
  72. if [[ "${SWIFT_FORMAT_RC}" -ne 0 ]]; then
  73. fatal "Running swift format produced errors." "${SWIFT_FORMAT_RC}"
  74. fi
  75. log "Ran swift format with no errors."
  76. else
  77. fatal "No actions taken."
  78. fi