run-plugin-tests.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 -eux
  16. HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  17. GRPC_PATH="${HERE}/.."
  18. function generate_package_manifest {
  19. local version=$1
  20. local grpc_path=$2
  21. echo "// swift-tools-version: $version"
  22. echo "import PackageDescription"
  23. echo ""
  24. echo "let package = Package("
  25. echo " name: \"Foo\","
  26. echo " dependencies: ["
  27. echo " .package(path: \"$grpc_path\"),"
  28. echo " .package(url: \"https://github.com/apple/swift-protobuf\", from: \"1.26.0\")"
  29. echo " ],"
  30. echo " targets: ["
  31. echo " .executableTarget("
  32. echo " name: \"Foo\","
  33. echo " dependencies: ["
  34. echo " .product(name: \"GRPC\", package: \"grpc-swift\"),"
  35. echo " ],"
  36. echo " path: \"Sources/Foo\","
  37. echo " plugins: ["
  38. echo " .plugin(name: \"GRPCSwiftPlugin\", package: \"grpc-swift\"),"
  39. echo " .plugin(name: \"SwiftProtobufPlugin\", package: \"swift-protobuf\"),"
  40. echo " ]"
  41. echo " ),"
  42. echo " ]"
  43. echo ")"
  44. }
  45. function generate_grpc_plugin_config {
  46. cat <<EOF
  47. {
  48. "invocations": [
  49. {
  50. "protoFiles": ["Foo.proto"],
  51. "visibility": "internal"
  52. }
  53. ]
  54. }
  55. EOF
  56. }
  57. function generate_protobuf_plugin_config {
  58. generate_grpc_plugin_config
  59. }
  60. function generate_proto {
  61. cat <<EOF
  62. syntax = "proto3";
  63. service Foo {
  64. rpc Bar(Baz) returns (Baz) {}
  65. }
  66. message Baz {}
  67. EOF
  68. }
  69. function generate_main {
  70. echo "// This file was intentionally left blank."
  71. }
  72. function generate_and_build {
  73. local version=$1
  74. local grpc_path=$2
  75. local protoc_path dir
  76. protoc_path=$(which protoc)
  77. dir=$(mktemp -d)
  78. echo "Generating package in $dir ..."
  79. echo "Swift tools version: $version"
  80. echo "grpc-swift path: $grpc_path"
  81. echo "protoc path: $protoc_path"
  82. mkdir -p "$dir/Sources/Foo"
  83. generate_package_manifest "$version" "$grpc_path" > "$dir/Package.swift"
  84. generate_grpc_plugin_config > "$dir/Sources/Foo/grpc-swift-config.json"
  85. generate_protobuf_plugin_config > "$dir/Sources/Foo/swift-protobuf-config.json"
  86. generate_proto > "$dir/Sources/Foo/Foo.proto"
  87. generate_main > "$dir/Sources/Foo/main.swift"
  88. PROTOC_PATH=$protoc_path swift build --package-path "$dir"
  89. }
  90. if [[ $# -lt 1 ]]; then
  91. echo "Usage: $0 SWIFT_TOOLS_VERSION"
  92. exit 1
  93. fi
  94. generate_and_build "$1" "${GRPC_PATH}"