main.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2017, 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 Foundation
  17. import SwiftProtobuf
  18. import PluginLibrary
  19. import Stencil
  20. import PathKit
  21. func Log(_ message : String) {
  22. FileHandle.standardError.write((message + "\n").data(using:.utf8)!)
  23. }
  24. // Code templates use "//-" prefixes to comment-out template operators
  25. // to keep them from interfering with Swift code formatting tools.
  26. // Use this to remove them after templates have been expanded.
  27. func stripMarkers(_ code:String) -> String {
  28. let inputLines = code.components(separatedBy:"\n")
  29. var outputLines : [String] = []
  30. for line in inputLines {
  31. if line.contains("//-") {
  32. let removed = line.replacingOccurrences(of:"//-", with:"")
  33. if (removed.trimmingCharacters(in:CharacterSet.whitespaces) != "") {
  34. outputLines.append(removed)
  35. }
  36. } else {
  37. outputLines.append(line)
  38. }
  39. }
  40. return outputLines.joined(separator:"\n")
  41. }
  42. func main() throws {
  43. // initialize template engine and add custom filters
  44. let templateEnvironment = Environment(loader: InternalLoader(),
  45. extensions:[GRPCFilterExtension()])
  46. // initialize responses
  47. var response = Google_Protobuf_Compiler_CodeGeneratorResponse()
  48. var log = ""
  49. // read plugin input
  50. let rawRequest = try Stdin.readall()
  51. let request = try Google_Protobuf_Compiler_CodeGeneratorRequest(serializedData: rawRequest)
  52. var generatedFileNames = Set<String>()
  53. // process each .proto file separately
  54. for protoFile in request.protoFile {
  55. let file = FileDescriptor(proto:protoFile)
  56. // a package declaration is required
  57. let package = file.package
  58. guard package != "" else {
  59. print("ERROR: no package for \(file.name)")
  60. continue
  61. }
  62. // log info about the service
  63. log += "File \(file.name)\n"
  64. for service in file.service {
  65. log += "Service \(service.name)\n"
  66. for method in service.method {
  67. log += " Method \(method.name)\n"
  68. log += " input \(method.inputType)\n"
  69. log += " output \(method.outputType)\n"
  70. log += " client_streaming \(method.clientStreaming)\n"
  71. log += " server_streaming \(method.serverStreaming)\n"
  72. }
  73. }
  74. if file.service.count > 0 {
  75. // generate separate implementation files for client and server
  76. let context : [String:Any] = ["file": file, "access": "internal"]
  77. do {
  78. let clientFileName = package + ".client.pb.swift"
  79. if !generatedFileNames.contains(clientFileName) {
  80. generatedFileNames.insert(clientFileName)
  81. let clientcode = try templateEnvironment.renderTemplate(name:"client.pb.swift",
  82. context: context)
  83. var clientfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  84. clientfile.name = clientFileName
  85. clientfile.content = stripMarkers(clientcode)
  86. response.file.append(clientfile)
  87. }
  88. let serverFileName = package + ".server.pb.swift"
  89. if !generatedFileNames.contains(serverFileName) {
  90. generatedFileNames.insert(serverFileName)
  91. let servercode = try templateEnvironment.renderTemplate(name:"server.pb.swift",
  92. context: context)
  93. var serverfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  94. serverfile.name = serverFileName
  95. serverfile.content = stripMarkers(servercode)
  96. response.file.append(serverfile)
  97. }
  98. } catch (let error) {
  99. Log("ERROR \(error)")
  100. log += "ERROR: \(error)\n"
  101. }
  102. }
  103. }
  104. log += "\(request)"
  105. // add the logfile to the code generation response
  106. var logfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  107. logfile.name = "swiftgrpc.log"
  108. logfile.content = log
  109. response.file.append(logfile)
  110. // return everything to the caller
  111. let serializedResponse = try response.serializedData()
  112. Stdout.write(bytes: serializedResponse)
  113. }
  114. do {
  115. try main()
  116. } catch (let error) {
  117. Log("ERROR: \(error)")
  118. }