v1_to_v2.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. ## Copyright 2025, 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 -eou pipefail
  16. log() { printf -- "** %s\n" "$*" >&2; }
  17. error() { printf -- "** ERROR: %s\n" "$*" >&2; }
  18. fatal() { error "$@"; exit 1; }
  19. # Clones v1 into the given directory and applies a number of patches to rename
  20. # the package from 'grpc-swift' to 'grpc-swift-v1' and 'protoc-gen-grpc-swift'
  21. # to 'protoc-gen-grpc-swift-v1'.
  22. function checkout_v1 {
  23. # The directory to clone grpc-swift into.
  24. grpc_checkout_dir="$(realpath "$1")"
  25. # The path of the checkout.
  26. grpc_checkout_path="${grpc_checkout_dir}/grpc-swift-v1"
  27. # Clone the repo.
  28. log "Cloning grpc-swift to ${grpc_checkout_path}"
  29. git clone \
  30. --quiet \
  31. https://github.com/grpc/grpc-swift.git \
  32. "${grpc_checkout_path}"
  33. # Get the latest version of 1.x.y.
  34. local -r version=$(git -C "${grpc_checkout_path}" tag --list | grep '1.\([0-9]\+\).\([0-9]\+\)$' | sort -V | tail -n 1)
  35. log "Checking out $version"
  36. git -C "${grpc_checkout_path}" checkout --quiet "$version"
  37. # Remove the git bits.
  38. log "Removing ${grpc_checkout_path}/.git"
  39. rm -rf "${grpc_checkout_path}/.git"
  40. # Update the manifest to rename the package and the protoc plugin.
  41. package_manifest="${grpc_checkout_path}/Package.swift"
  42. log "Updating ${package_manifest}"
  43. sed -i '' \
  44. -e 's/let grpcPackageName = "grpc-swift"/let grpcPackageName = "grpc-swift-v1"/g' \
  45. -e 's/protoc-gen-grpc-swift/protoc-gen-grpc-swift-v1/g' \
  46. "${package_manifest}"
  47. # Update all references to protoc-gen-grpc-swift.
  48. log "Updating references to protoc-gen-grpc-swift"
  49. find \
  50. "${grpc_checkout_path}/Sources" \
  51. "${grpc_checkout_path}/Tests" \
  52. "${grpc_checkout_path}/Plugins" \
  53. -type f \
  54. -name '*.swift' \
  55. -exec sed -i '' 's/protoc-gen-grpc-swift/protoc-gen-grpc-swift-v1/g' {} +
  56. # Update the path of the protoc plugin so it aligns with the target name.
  57. log "Updating directory name for protoc-gen-grpc-swift-v1"
  58. mv "${grpc_checkout_path}/Sources/protoc-gen-grpc-swift" "${grpc_checkout_path}/Sources/protoc-gen-grpc-swift-v1"
  59. log "Cloned and patched v1 to: ${grpc_checkout_path}"
  60. }
  61. # Recursively finds '*.grpc.swift' files in the given directory and renames them
  62. # to '*grpc.v1.swift'.
  63. function rename_generated_grpc_code {
  64. local directory=$1
  65. find "$directory" -type f -name "*.grpc.swift" \
  66. -exec bash -c 'mv "$0" "${0%.grpc.swift}.grpc.v1.swift"' {} \;
  67. }
  68. # Applies a number of textual replacements to migrate a service implementation
  69. # on the given file.
  70. function patch_service_code {
  71. local filename=$1
  72. sed -E -i '' \
  73. -e 's/import GRPC/import GRPCCore/g' \
  74. -e 's/GRPCAsyncServerCallContext/ServerContext/g' \
  75. -e 's/: ([A-Za-z_][A-Za-z0-9_]*)AsyncProvider/: \1.SimpleServiceProtocol/g' \
  76. -e 's/GRPCAsyncResponseStreamWriter/RPCWriter/g' \
  77. -e 's/GRPCAsyncRequestStream<([A-Za-z_][A-Za-z0-9_]*)>/RPCAsyncSequence<\1, any Error>/g' \
  78. -e 's/responseStream.send/responseStream.write/g' \
  79. -e 's/responseStream:/response responseStream:/g' \
  80. -e 's/requestStream:/request requestStream:/g' \
  81. "$filename"
  82. }
  83. function usage {
  84. echo "Usage:"
  85. echo " $0 clone-v1 DIRECTORY"
  86. echo " $0 rename-generated-code DIRECTORY"
  87. echo " $0 patch-service FILE"
  88. exit 1
  89. }
  90. if [[ $# -lt 2 ]]; then
  91. usage
  92. fi
  93. subcommand="$1"
  94. argument="$2"
  95. case "$subcommand" in
  96. "clone-v1")
  97. checkout_v1 "$argument"
  98. ;;
  99. "rename-generated-code")
  100. rename_generated_grpc_code "$argument"
  101. ;;
  102. "patch-service")
  103. patch_service_code "$argument"
  104. ;;
  105. *)
  106. usage
  107. ;;
  108. esac