Skip to content

Commit

Permalink
Allow the gRPC module name to be customised when generating code (#945)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glbrntt authored Aug 19, 2020
1 parent a59c5e5 commit 8b5d7d9
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 40 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ the following line to your `Podfile`:
Then, run `pod install` from command line and use your project's generated
`.xcworkspace` file. You might also need to add `use_frameworks!` to your `Podfile`.

*⚠️ If you have conficting modules as a result of having a transitive
dependency on '[gRPC-Core][grpc-core-pod]' see [grpc/grpc-swift#945][grpc-swift-945].*

### Getting the `protoc` Plugins

Binary releases of `protoc`, the Protocol Buffer Compiler, are available on
Expand Down Expand Up @@ -171,6 +174,8 @@ Please get involved! See our [guidelines for contributing](CONTRIBUTING.md).
[docs-keepalive]: ./docs/keepalive.md
[docs-tutorial]: ./docs/basic-tutorial.md
[grpc]: https://github.com/grpc/grpc
[grpc-core-pod]: https://cocoapods.org/pods/gRPC-Core
[grpc-swift-945]: https://github.com/grpc/grpc-swift/pull/945
[protobuf-releases]: https://github.com/protocolbuffers/protobuf/releases
[swift-nio-platforms]: https://github.com/apple/swift-nio#supported-platforms
[swift-nio]: https://github.com/apple/swift-nio
Expand Down
3 changes: 0 additions & 3 deletions Sources/Examples/Echo/Model/echo.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Echo_EchoClient, then call methods of this protocol to make API calls.
Expand Down
3 changes: 0 additions & 3 deletions Sources/Examples/HelloWorld/Model/helloworld.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Helloworld_GreeterClient, then call methods of this protocol to make API calls.
Expand Down
3 changes: 0 additions & 3 deletions Sources/Examples/RouteGuide/Model/route_guide.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Routeguide_RouteGuideClient, then call methods of this protocol to make API calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Grpc_Testing_TestServiceClient, then call methods of this protocol to make API calls.
Expand Down
5 changes: 1 addition & 4 deletions Sources/protoc-gen-grpc-swift/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ class Generator {
""")

let moduleNames = [
"Foundation",
self.options.gRPCModuleName,
"NIO",
"NIOHTTP1",
"GRPC",
"SwiftProtobuf",
]

for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {
Expand Down
8 changes: 8 additions & 0 deletions Sources/protoc-gen-grpc-swift/options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 0 additions & 3 deletions dev/codegen-tests/01-echo/golden/echo.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Echo_EchoClient, then call methods of this protocol to make API calls.
Expand Down
3 changes: 0 additions & 3 deletions dev/codegen-tests/02-multifile/golden/a.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate A_ServiceAClient, then call methods of this protocol to make API calls.
Expand Down
3 changes: 0 additions & 3 deletions dev/codegen-tests/02-multifile/golden/b.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate B_ServiceBClient, then call methods of this protocol to make API calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf
import ModuleB


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate B_ServiceBClient, then call methods of this protocol to make API calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Codegentest_FooClient, then call methods of this protocol to make API calls.
Expand Down
3 changes: 0 additions & 3 deletions dev/codegen-tests/05-service-only/golden/test.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


/// Usage: instantiate Codegentest_FooClient, then call methods of this protocol to make API calls.
Expand Down
3 changes: 0 additions & 3 deletions dev/codegen-tests/06-test-client-only/golden/test.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import GRPC
import NIO
import NIOHTTP1
import SwiftProtobuf


internal final class Codegentest_FooTestClient: Codegentest_FooClientProtocol {
Expand Down
9 changes: 9 additions & 0 deletions docs/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ The **FileNaming** option determines how generated source files should be named.
The **ProtoPathModuleMappings** option allows module mappings to be specified.
See the [SwiftProtobuf documentation][swift-protobuf-module-mappings].

### GRPCModuleName

The **GRPCModuleName** option allows the name of the gRPC Swift runtime module
to be specified. The value, if not specified, defaults to "GRPC".

*Note: most users will not need to use this option. It is intended as a
workaround for CocoaPods users who may end up with a transitive dependency on
the gRPC core C library whose module name is also "GRPC".*

## Specifying Options

To pass extra parameters to the plugin, use a comma-separated parameter list
Expand Down

0 comments on commit 8b5d7d9

Please sign in to comment.