generate.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/bash
  2. ## Copyright 2024, 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. here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  17. root="$here/../.."
  18. protoc=$(which protoc)
  19. # Checkout and build the plugins.
  20. swift build --package-path "$root" --product protoc-gen-swift
  21. swift build --package-path "$root" --product protoc-gen-grpc-swift
  22. # Grab the plugin paths.
  23. bin_path=$(swift build --package-path "$root" --show-bin-path)
  24. protoc_gen_swift="$bin_path/protoc-gen-swift"
  25. protoc_gen_grpc_swift="$bin_path/protoc-gen-grpc-swift"
  26. # Generates gRPC by invoking protoc with the gRPC Swift plugin.
  27. # Parameters:
  28. # - $1: .proto file
  29. # - $2: proto path
  30. # - $3: output path
  31. # - $4 onwards: options to forward to the plugin
  32. function generate_grpc {
  33. local proto=$1
  34. local args=("--plugin=$protoc_gen_grpc_swift" "--proto_path=${2}" "--grpc-swift_out=${3}")
  35. for option in "${@:4}"; do
  36. args+=("--grpc-swift_opt=$option")
  37. done
  38. invoke_protoc "${args[@]}" "$proto"
  39. }
  40. # Generates messages by invoking protoc with the Swift plugin.
  41. # Parameters:
  42. # - $1: .proto file
  43. # - $2: proto path
  44. # - $3: output path
  45. # - $4 onwards: options to forward to the plugin
  46. function generate_message {
  47. local proto=$1
  48. local args=("--plugin=$protoc_gen_swift" "--proto_path=$2" "--swift_out=$3")
  49. for option in "${@:4}"; do
  50. args+=("--swift_opt=$option")
  51. done
  52. invoke_protoc "${args[@]}" "$proto"
  53. }
  54. function invoke_protoc {
  55. # Setting -x when running the script produces a lot of output, instead boil
  56. # just echo out the protoc invocations.
  57. echo "$protoc" "$@"
  58. "$protoc" "$@"
  59. }
  60. #- DETAILED ERROR -------------------------------------------------------------
  61. function generate_rpc_error_details {
  62. local protos output
  63. protos=(
  64. "$here/upstream/google/rpc/status.proto"
  65. "$here/upstream/google/rpc/code.proto"
  66. "$here/upstream/google/rpc/error_details.proto"
  67. )
  68. output="$root/Sources/GRPCProtobuf/Errors/Generated"
  69. for proto in "${protos[@]}"; do
  70. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "UseAccessLevelOnImports=true"
  71. done
  72. }
  73. #- DETAILED ERROR Tests -------------------------------------------------------
  74. function generate_error_service {
  75. local proto output
  76. proto="$here/local/error-service.proto"
  77. output="$root/Tests/GRPCProtobufTests/Errors/Generated"
  78. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "UseAccessLevelOnImports=true"
  79. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "UseAccessLevelOnImports=true"
  80. }
  81. #- DESCRIPTOR SETS ------------------------------------------------------------
  82. function generate_test_service_descriptor_set {
  83. local proto proto_path output
  84. proto="$here/local/test-service.proto"
  85. proto_path="$(dirname "$proto")"
  86. output="$root/Tests/GRPCProtobufCodeGenTests/Generated/test-service.pb"
  87. invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
  88. --include_imports \
  89. --include_source_info
  90. }
  91. function generate_foo_service_descriptor_set {
  92. local proto proto_path output
  93. proto="$here/local/foo-service.proto"
  94. proto_path="$(dirname "$proto")"
  95. output="$root/Tests/GRPCProtobufCodeGenTests/Generated/foo-service.pb"
  96. invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
  97. --include_source_info \
  98. --include_imports
  99. }
  100. function generate_foo_messages_descriptor_set {
  101. local proto proto_path output
  102. proto="$here/local/foo-messages.proto"
  103. proto_path="$(dirname "$proto")"
  104. output="$root/Tests/GRPCProtobufCodeGenTests/Generated/foo-messages.pb"
  105. invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
  106. --include_source_info \
  107. --include_imports
  108. }
  109. function generate_bar_service_descriptor_set {
  110. local proto proto_path output
  111. proto="$here/local/bar-service.proto"
  112. proto_path="$(dirname "$proto")"
  113. output="$root/Tests/GRPCProtobufCodeGenTests/Generated/bar-service.pb"
  114. invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
  115. --include_source_info \
  116. --include_imports
  117. }
  118. function generate_wkt_service_descriptor_set {
  119. local proto proto_path output
  120. proto="$here/local/wkt-service.proto"
  121. proto_path="$(dirname "$proto")"
  122. output="$root/Tests/GRPCProtobufCodeGenTests/Generated/wkt-service.pb"
  123. invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
  124. --include_source_info \
  125. --include_imports
  126. }
  127. #------------------------------------------------------------------------------
  128. # Detailed error model
  129. generate_rpc_error_details
  130. # Service for testing error details
  131. generate_error_service
  132. # Descriptor sets for tests
  133. generate_test_service_descriptor_set
  134. generate_foo_service_descriptor_set
  135. generate_foo_messages_descriptor_set
  136. generate_bar_service_descriptor_set
  137. generate_wkt_service_descriptor_set