Browse Source

Add reflection support to plugin (#1835)

Sami Suteria 1 year ago
parent
commit
0da4b47775

+ 14 - 0
Plugins/GRPCSwiftPlugin/plugin.swift

@@ -65,6 +65,8 @@ struct GRPCSwiftPlugin {
       var server: Bool?
       var server: Bool?
       /// Whether client code is generated.
       /// Whether client code is generated.
       var client: Bool?
       var client: Bool?
+      /// Whether reflection data is generated.
+      var reflectionData: Bool?
       /// Determines whether the casing of generated function names is kept.
       /// Determines whether the casing of generated function names is kept.
       var keepMethodCasing: Bool?
       var keepMethodCasing: Bool?
     }
     }
@@ -186,6 +188,10 @@ struct GRPCSwiftPlugin {
       protocArgs.append("--grpc-swift_opt=Client=\(generateClientCode)")
       protocArgs.append("--grpc-swift_opt=Client=\(generateClientCode)")
     }
     }
 
 
+    if let generateReflectionData = invocation.reflectionData {
+      protocArgs.append("--grpc-swift_opt=ReflectionData=\(generateReflectionData)")
+    }
+
     if let keepMethodCasingOption = invocation.keepMethodCasing {
     if let keepMethodCasingOption = invocation.keepMethodCasing {
       protocArgs.append("--grpc-swift_opt=KeepMethodCasing=\(keepMethodCasingOption)")
       protocArgs.append("--grpc-swift_opt=KeepMethodCasing=\(keepMethodCasingOption)")
     }
     }
@@ -207,6 +213,14 @@ struct GRPCSwiftPlugin {
 
 
       // Add the outputPath as an output file
       // Add the outputPath as an output file
       outputFiles.append(protobufOutputPath)
       outputFiles.append(protobufOutputPath)
+
+      if invocation.reflectionData == true {
+        // Remove .swift extension and add .reflection extension
+        file.removeLast(5)
+        file.append("reflection")
+        let reflectionOutputPath = outputDirectory.appending(file)
+        outputFiles.append(reflectionOutputPath)
+      }
     }
     }
 
 
     // Construct the command. Specifying the input and output paths lets the build
     // Construct the command. Specifying the input and output paths lets the build

+ 33 - 0
Sources/GRPCReflectionService/Documentation.docc/ReflectionServiceTutorial.md

@@ -102,6 +102,38 @@ let reflectionService = try ReflectionService(
 )
 )
 ```
 ```
 
 
+### Swift Package Manager Plugin
+
+Reflection data can also be generated via the SPM plugin by including `"reflectionData": true` in `grpc-swift-config.json`. This will generate the same reflection data as running `protoc` above. The generated reflection files are added to your module Bundle and can be accessed at runtime. More about [spm-plugin][spm-plugin] can be found here.
+
+```json
+{
+    "invocations": [
+        {
+            "protoFiles": [
+                "helloworld.proto"
+            ],
+            "visibility": "public",
+            "server": true,
+            "reflectionData": true
+        }
+    ]
+}
+```
+
+To instantiate the `ReflectionService` you can search for files with the extension `reflection` in your module Bundle.
+
+```swift
+let reflectionDataFilePaths = Bundle.module.paths(
+  forResourcesOfType: "reflection",
+  inDirectory: nil
+)
+let reflectionService = try ReflectionService(
+  reflectionDataFilePaths: reflectionDataFilePaths,
+  version: .v1Alpha
+)
+```
+
 ### Running the server
 ### Running the server
 
 
 In our example the server isn't configured with TLS and listens on localhost port 1234.
 In our example the server isn't configured with TLS and listens on localhost port 1234.
@@ -192,3 +224,4 @@ Note that when specifying a service, a method or a symbol, we have to use the fu
 [echo-proto]: ../../Examples/Echo/Model/echo.proto
 [echo-proto]: ../../Examples/Echo/Model/echo.proto
 [grpcurl-v188]: https://github.com/fullstorydev/grpcurl/releases/tag/v1.8.8
 [grpcurl-v188]: https://github.com/fullstorydev/grpcurl/releases/tag/v1.8.8
 [swiftpm-resources]: https://github.com/apple/swift-package-manager/blob/main/Documentation/PackageDescription.md#resource
 [swiftpm-resources]: https://github.com/apple/swift-package-manager/blob/main/Documentation/PackageDescription.md#resource
+[spm-plugin]: ../../protoc-gen-grpc-swift/Docs.docc/spm-plugin.md

+ 2 - 1
Sources/protoc-gen-grpc-swift/Docs.docc/spm-plugin.md

@@ -81,7 +81,8 @@ Sources
             ],
             ],
             "visibility": "public",
             "visibility": "public",
             "client": false,
             "client": false,
-            "keepMethodCasing": false
+            "keepMethodCasing": false,
+            "reflectionData": true
         }
         }
     ]
     ]
 }
 }