main.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Stencil
  15. import Foundation
  16. import SwiftProtobuf
  17. import PluginLibrary
  18. func main() throws {
  19. let fileSystemLoader = Stencil.FileSystemLoader(paths: ["templates/"])
  20. let templateEnvironment = Environment(loader: fileSystemLoader)
  21. var response = Google_Protobuf_Compiler_CodeGeneratorResponse()
  22. var log = ""
  23. let rawRequest = try Stdin.readall()
  24. let request = try Google_Protobuf_Compiler_CodeGeneratorRequest(protobuf: rawRequest)
  25. for protoFile in request.protoFile {
  26. if let package = protoFile.package {
  27. log += "File \(protoFile.name!)\n"
  28. for service in protoFile.service {
  29. log += "Service \(service.name!)\n"
  30. for method in service.method {
  31. log += " Method \(method.name!)\n"
  32. log += " input \(method.inputType!)\n"
  33. log += " output \(method.outputType!)\n"
  34. log += " client_streaming \(method.clientStreaming!)\n"
  35. log += " server_streaming \(method.serverStreaming!)\n"
  36. }
  37. log += " Options \(service.options)\n"
  38. }
  39. let context = ["protoFile": protoFile]
  40. let clientcode = try templateEnvironment.renderTemplate(name: "PACKAGE.client.pb.swift", context: context)
  41. let servercode = try templateEnvironment.renderTemplate(name: "PACKAGE.server.pb.swift", context: context)
  42. var clientfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  43. clientfile.name = package + ".client.pb.swift"
  44. clientfile.content = clientcode
  45. response.file.append(clientfile)
  46. var serverfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  47. serverfile.name = package + ".server.pb.swift"
  48. serverfile.content = servercode
  49. response.file.append(serverfile)
  50. } else {
  51. print("ERROR: no package")
  52. }
  53. }
  54. log += "\n\n\n\(request)"
  55. var logfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  56. logfile.name = "swiftgrpc.log"
  57. logfile.content = log
  58. response.file.append(logfile)
  59. let serializedResponse = try response.serializeProtobuf()
  60. Stdout.write(bytes: serializedResponse)
  61. }
  62. try main()