Generator.swift 4.1 KB

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