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