Generator.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 var observedMessages: Set<String>
  24. internal let protobufNamer: SwiftProtobufNamer
  25. init(_ file: FileDescriptor, options: GeneratorOptions, observedMessages: Set<String>) {
  26. self.file = file
  27. self.options = options
  28. self.observedMessages = observedMessages
  29. self.printer = CodePrinter()
  30. self.protobufNamer = SwiftProtobufNamer(
  31. currentFile: file,
  32. protoFileToModuleMappings: options.protoToModuleMappings)
  33. printMain()
  34. }
  35. public var code: String {
  36. return printer.content
  37. }
  38. internal func println(_ text: String = "", newline: Bool = true) {
  39. printer.print(text)
  40. if newline {
  41. printer.print("\n")
  42. }
  43. }
  44. internal func indent() {
  45. printer.indent()
  46. }
  47. internal func outdent() {
  48. printer.outdent()
  49. }
  50. private func printMain() {
  51. printer.print("""
  52. //
  53. // DO NOT EDIT.
  54. //
  55. // Generated by the protocol buffer compiler.
  56. // Source: \(file.name)
  57. //
  58. //
  59. // Copyright 2018, gRPC Authors All rights reserved.
  60. //
  61. // Licensed under the Apache License, Version 2.0 (the "License");
  62. // you may not use this file except in compliance with the License.
  63. // You may obtain a copy of the License at
  64. //
  65. // http://www.apache.org/licenses/LICENSE-2.0
  66. //
  67. // Unless required by applicable law or agreed to in writing, software
  68. // distributed under the License is distributed on an "AS IS" BASIS,
  69. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70. // See the License for the specific language governing permissions and
  71. // limitations under the License.
  72. //\n
  73. """)
  74. let moduleNames = [
  75. "Foundation",
  76. "NIO",
  77. "NIOHTTP1",
  78. "GRPC",
  79. "SwiftProtobuf"
  80. ]
  81. for moduleName in (moduleNames + options.extraModuleImports).sorted() {
  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. for service in file.services {
  99. self.service = service
  100. printClient()
  101. }
  102. }
  103. println()
  104. if options.generateServer {
  105. for service in file.services {
  106. self.service = service
  107. printServer()
  108. }
  109. }
  110. self.println()
  111. self.printProtobufExtensions()
  112. }
  113. }