Generator.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. )
  32. self.printMain()
  33. }
  34. public var code: String {
  35. return self.printer.content
  36. }
  37. internal func println(_ text: String = "", newline: Bool = true) {
  38. self.printer.print(text)
  39. if newline {
  40. self.printer.print("\n")
  41. }
  42. }
  43. internal func indent() {
  44. self.printer.indent()
  45. }
  46. internal func outdent() {
  47. self.printer.outdent()
  48. }
  49. internal func withIndentation(body: () -> Void) {
  50. self.indent()
  51. body()
  52. self.outdent()
  53. }
  54. private func printMain() {
  55. self.printer.print("""
  56. //
  57. // DO NOT EDIT.
  58. //
  59. // Generated by the protocol buffer compiler.
  60. // Source: \(self.file.name)
  61. //
  62. //
  63. // Copyright 2018, gRPC Authors All rights reserved.
  64. //
  65. // Licensed under the Apache License, Version 2.0 (the "License");
  66. // you may not use this file except in compliance with the License.
  67. // You may obtain a copy of the License at
  68. //
  69. // http://www.apache.org/licenses/LICENSE-2.0
  70. //
  71. // Unless required by applicable law or agreed to in writing, software
  72. // distributed under the License is distributed on an "AS IS" BASIS,
  73. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  74. // See the License for the specific language governing permissions and
  75. // limitations under the License.
  76. //\n
  77. """)
  78. let moduleNames = [
  79. self.options.gRPCModuleName,
  80. "NIO",
  81. self.options.swiftProtobufModuleName,
  82. ]
  83. for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {
  84. self.println("import \(moduleName)")
  85. }
  86. // Add imports for required modules
  87. let moduleMappings = self.options.protoToModuleMappings
  88. for importedProtoModuleName in moduleMappings.neededModules(forFile: self.file) ?? [] {
  89. self.println("import \(importedProtoModuleName)")
  90. }
  91. self.println()
  92. // We defer the check for printing clients to `printClient()` since this could be the 'real'
  93. // client or the test client.
  94. for service in self.file.services {
  95. self.service = service
  96. self.printClient()
  97. }
  98. self.println()
  99. if self.options.generateServer {
  100. for service in self.file.services {
  101. self.service = service
  102. printServer()
  103. }
  104. }
  105. }
  106. }