generate.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. #
  3. # Copyright 2024, gRPC Authors All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. set -eu
  17. here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  18. root="$here/../.."
  19. protoc=$(which protoc)
  20. # Checkout and build the plugins.
  21. build_dir=$(mktemp -d)
  22. git clone https://github.com/grpc/grpc-swift-protobuf --depth 1 "$build_dir"
  23. swift build --package-path "$build_dir" --product protoc-gen-swift
  24. swift build --package-path "$build_dir" --product protoc-gen-grpc-swift
  25. # Grab the plugin paths.
  26. bin_path=$(swift build --package-path "$build_dir" --show-bin-path)
  27. protoc_gen_swift="$bin_path/protoc-gen-swift"
  28. protoc_gen_grpc_swift="$bin_path/protoc-gen-grpc-swift"
  29. # Generates gRPC by invoking protoc with the gRPC Swift plugin.
  30. # Parameters:
  31. # - $1: .proto file
  32. # - $2: proto path
  33. # - $3: output path
  34. # - $4 onwards: options to forward to the plugin
  35. function generate_grpc {
  36. local proto=$1
  37. local args=("--plugin=$protoc_gen_grpc_swift" "--proto_path=${2}" "--grpc-swift_out=${3}")
  38. for option in "${@:4}"; do
  39. args+=("--grpc-swift_opt=$option")
  40. done
  41. invoke_protoc "${args[@]}" "$proto"
  42. }
  43. # Generates messages by invoking protoc with the Swift plugin.
  44. # Parameters:
  45. # - $1: .proto file
  46. # - $2: proto path
  47. # - $3: output path
  48. # - $4 onwards: options to forward to the plugin
  49. function generate_message {
  50. local proto=$1
  51. local args=("--plugin=$protoc_gen_swift" "--proto_path=$2" "--swift_out=$3")
  52. for option in "${@:4}"; do
  53. args+=("--swift_opt=$option")
  54. done
  55. invoke_protoc "${args[@]}" "$proto"
  56. }
  57. function invoke_protoc {
  58. # Setting -x when running the script produces a lot of output, instead boil
  59. # just echo out the protoc invocations.
  60. echo "$protoc" "$@"
  61. "$protoc" "$@"
  62. }
  63. #- EXAMPLES -------------------------------------------------------------------
  64. function generate_echo_example {
  65. local proto="$here/examples/echo/echo.proto"
  66. local output="$root/Examples/echo/Sources/Generated"
  67. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  68. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  69. }
  70. function generate_helloworld_example {
  71. local proto="$here/upstream/grpc/examples/helloworld.proto"
  72. local output="$root/Examples/hello-world/Sources/Generated"
  73. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  74. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  75. }
  76. function generate_routeguide_example {
  77. local proto="$here/examples/route_guide/route_guide.proto"
  78. local output="$root/Examples/route-guide/Sources/Generated"
  79. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  80. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  81. }
  82. #- TESTS ----------------------------------------------------------------------
  83. function generate_service_config_for_tests {
  84. local protos=(
  85. "$here/upstream/grpc/service_config/service_config.proto"
  86. "$here/upstream/grpc/lookup/v1/rls.proto"
  87. "$here/upstream/grpc/lookup/v1/rls_config.proto"
  88. "$here/upstream/google/rpc/code.proto"
  89. )
  90. local output="$root/Tests/GRPCCoreTests/Configuration/Generated"
  91. for proto in "${protos[@]}"; do
  92. generate_message "$proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=DropPath"
  93. done
  94. }
  95. #------------------------------------------------------------------------------
  96. # Generate examples
  97. generate_echo_example
  98. generate_helloworld_example
  99. generate_routeguide_example
  100. # Tests
  101. generate_service_config_for_tests