Browse Source

Allow the gRPC module name to be customised when generating code (#945)

Motivation:

Some Cocoapods may find themselves in a situation where their dependency
graph contains both gRPC Swift and the gRPC core library. This is
problematic since both have the same module name ("GRPC"). Changing our
own module name is a very large breaking change and one we'd like to avoid.
As an escape valve, users who take a direct dependency on us may rename
our module via their Podfile; doing so would require their generated
code to depend on that module, rather than the currently hardcoded
'GRPC' module.

Modifications:

- Add an option to the codegen to specify the gRPC module name
- Remove a couple of imports which were previously used but are now no
  longer require
- Regenerate

Result:

It's possible for Cocoapods users to take a direct dependency on gRPC
Swift and libgrpc.
George Barnett 5 years ago
parent
commit
38f6fd8a29

+ 1 - 4
Sources/protoc-gen-grpc-swift/Generator.swift

@@ -88,11 +88,8 @@ class Generator {
     """)
 
     let moduleNames = [
-      "Foundation",
+      self.options.gRPCModuleName,
       "NIO",
-      "NIOHTTP1",
-      "GRPC",
-      "SwiftProtobuf",
     ]
 
     for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {

+ 8 - 0
Sources/protoc-gen-grpc-swift/options.swift

@@ -58,6 +58,7 @@ final class GeneratorOptions {
   private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
   private(set) var fileNaming = FileNaming.FullPath
   private(set) var extraModuleImports: [String] = []
+  private(set) var gRPCModuleName = "GRPC"
 
   init(parameter: String?) throws {
     for pair in GeneratorOptions.parseParameter(string: parameter) {
@@ -116,6 +117,13 @@ final class GeneratorOptions {
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
         }
 
+      case "GRPCModuleName":
+        if !pair.value.isEmpty {
+          self.gRPCModuleName = pair.value
+        } else {
+          throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+        }
+
       default:
         throw GenerationError.unknownParameter(name: pair.key)
       }