Generator.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2018, 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. */
  16. import SwiftProtobufPluginLibrary
  17. class Generator {
  18. internal var options: GeneratorOptions
  19. private var printer: CodePrinter
  20. internal var file: FileDescriptor
  21. internal var service: ServiceDescriptor! // context during generation
  22. internal var method: MethodDescriptor! // context during generation
  23. internal let protobufNamer: SwiftProtobufNamer
  24. init(_ file: FileDescriptor, options: GeneratorOptions) {
  25. self.file = file
  26. self.options = options
  27. self.printer = CodePrinter()
  28. self.protobufNamer = SwiftProtobufNamer(
  29. currentFile: file,
  30. protoFileToModuleMappings: options.protoToModuleMappings)
  31. printMain()
  32. }
  33. public var code: String {
  34. return printer.content
  35. }
  36. internal func println(_ text: String = "") {
  37. printer.print(text)
  38. printer.print("\n")
  39. }
  40. internal func indent() {
  41. printer.indent()
  42. }
  43. internal func outdent() {
  44. printer.outdent()
  45. }
  46. private func printMain() {
  47. printer.print("""
  48. //
  49. // DO NOT EDIT.
  50. //
  51. // Generated by the protocol buffer compiler.
  52. // Source: \(file.name)
  53. //
  54. //
  55. // Copyright 2018, gRPC Authors All rights reserved.
  56. //
  57. // Licensed under the Apache License, Version 2.0 (the "License");
  58. // you may not use this file except in compliance with the License.
  59. // You may obtain a copy of the License at
  60. //
  61. // http://www.apache.org/licenses/LICENSE-2.0
  62. //
  63. // Unless required by applicable law or agreed to in writing, software
  64. // distributed under the License is distributed on an "AS IS" BASIS,
  65. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  66. // See the License for the specific language governing permissions and
  67. // limitations under the License.
  68. //\n
  69. """)
  70. let moduleNames: [String]
  71. if options.generateNIOImplementation {
  72. moduleNames = [
  73. "Foundation",
  74. "NIO",
  75. "NIOHTTP1",
  76. "SwiftGRPCNIO",
  77. "SwiftProtobuf"]
  78. } else {
  79. moduleNames = [
  80. "Foundation",
  81. "Dispatch",
  82. "SwiftGRPC",
  83. "SwiftProtobuf"]
  84. }
  85. for moduleName in (moduleNames + options.extraModuleImports).sorted() {
  86. println("import \(moduleName)")
  87. }
  88. // Build systems like Bazel will generate the Swift service definitions in a different module
  89. // than the rest of the protos defined in the same file (because they are generated by separate
  90. // build rules that invoke the separate generator plugins). So, we need to specify module
  91. // mappings to import the service protos as well as and any other proto modules that the file
  92. // imports.
  93. let moduleMappings = options.protoToModuleMappings
  94. if let serviceProtoModuleName = moduleMappings.moduleName(forFile: file) {
  95. println("import \(serviceProtoModuleName)")
  96. }
  97. for importedProtoModuleName in moduleMappings.neededModules(forFile: file) ?? [] {
  98. println("import \(importedProtoModuleName)")
  99. }
  100. println()
  101. if options.generateClient {
  102. if options.generateImplementations {
  103. for service in file.services {
  104. self.service = service
  105. printClient(asynchronousCode: options.generateAsynchronous,
  106. synchronousCode: options.generateSynchronous)
  107. }
  108. }
  109. if options.generateTestStubs {
  110. if options.generateNIOImplementation {
  111. fatalError("Generating test stubs is not yet supported for SwiftGRPC-NIO.")
  112. }
  113. for service in file.services {
  114. self.service = service
  115. printCGRPCClientTestStubs(asynchronousCode: options.generateAsynchronous,
  116. synchronousCode: options.generateSynchronous)
  117. }
  118. }
  119. }
  120. println()
  121. if options.generateServer {
  122. if options.generateTestStubs && options.generateNIOImplementation {
  123. fatalError("Generating test stubs is not yet supported for SwiftGRPC-NIO.")
  124. }
  125. if options.generateTestStubs && !options.generateImplementations {
  126. fatalError("Generating server test stubs without an implementation is not supported.")
  127. }
  128. for service in file.services {
  129. self.service = service
  130. printServer()
  131. }
  132. }
  133. }
  134. }