main.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 SwiftProtobufPluginLibrary
  19. func Log(_ message: String) {
  20. FileHandle.standardError.write((message + "\n").data(using: .utf8)!)
  21. }
  22. // from apple/swift-protobuf/Sources/protoc-gen-swift/StringUtils.swift
  23. func splitPath(pathname: String) -> (dir: String, base: String, suffix: String) {
  24. var dir = ""
  25. var base = ""
  26. var suffix = ""
  27. #if swift(>=3.2)
  28. let pathnameChars = pathname
  29. #else
  30. let pathnameChars = pathname.characters
  31. #endif
  32. for c in pathnameChars {
  33. if c == "/" {
  34. dir += base + suffix + String(c)
  35. base = ""
  36. suffix = ""
  37. } else if c == "." {
  38. base += suffix
  39. suffix = String(c)
  40. } else {
  41. suffix += String(c)
  42. }
  43. }
  44. #if swift(>=3.2)
  45. let validSuffix = suffix.isEmpty || suffix.first == "."
  46. #else
  47. let validSuffix = suffix.isEmpty || suffix.characters.first == "."
  48. #endif
  49. if !validSuffix {
  50. base += suffix
  51. suffix = ""
  52. }
  53. return (dir: dir, base: base, suffix: suffix)
  54. }
  55. enum FileNaming: String {
  56. case FullPath
  57. case PathToUnderscores
  58. case DropPath
  59. }
  60. func outputFileName(
  61. component: String,
  62. fileDescriptor: FileDescriptor,
  63. fileNamingOption: FileNaming
  64. ) -> String {
  65. let ext = "." + component + ".swift"
  66. let pathParts = splitPath(pathname: fileDescriptor.name)
  67. switch fileNamingOption {
  68. case .FullPath:
  69. return pathParts.dir + pathParts.base + ext
  70. case .PathToUnderscores:
  71. let dirWithUnderscores =
  72. pathParts.dir.replacingOccurrences(of: "/", with: "_")
  73. return dirWithUnderscores + pathParts.base + ext
  74. case .DropPath:
  75. return pathParts.base + ext
  76. }
  77. }
  78. func uniqueOutputFileName(
  79. component: String,
  80. fileDescriptor: FileDescriptor,
  81. fileNamingOption: FileNaming,
  82. generatedFiles: inout [String: Int]
  83. ) -> String {
  84. let defaultName = outputFileName(
  85. component: component,
  86. fileDescriptor: fileDescriptor,
  87. fileNamingOption: fileNamingOption
  88. )
  89. if let count = generatedFiles[defaultName] {
  90. generatedFiles[defaultName] = count + 1
  91. return outputFileName(
  92. component: "\(count)." + component,
  93. fileDescriptor: fileDescriptor,
  94. fileNamingOption: fileNamingOption
  95. )
  96. } else {
  97. generatedFiles[defaultName] = 1
  98. return defaultName
  99. }
  100. }
  101. func main() throws {
  102. // initialize responses
  103. var response = Google_Protobuf_Compiler_CodeGeneratorResponse(
  104. files: [],
  105. supportedFeatures: [.proto3Optional]
  106. )
  107. // read plugin input
  108. let rawRequest = FileHandle.standardInput.readDataToEndOfFile()
  109. let request = try Google_Protobuf_Compiler_CodeGeneratorRequest(serializedData: rawRequest)
  110. let options = try GeneratorOptions(parameter: request.parameter)
  111. // Build the SwiftProtobufPluginLibrary model of the plugin input
  112. let descriptorSet = DescriptorSet(protos: request.protoFile)
  113. // A count of generated files by desired name (actual name may differ to avoid collisions).
  114. var generatedFiles: [String: Int] = [:]
  115. // Only generate output for services.
  116. for name in request.fileToGenerate {
  117. let fileDescriptor = descriptorSet.lookupFileDescriptor(protoName: name)
  118. if !fileDescriptor.services.isEmpty {
  119. let grpcFileName = uniqueOutputFileName(
  120. component: "grpc",
  121. fileDescriptor: fileDescriptor,
  122. fileNamingOption: options.fileNaming,
  123. generatedFiles: &generatedFiles
  124. )
  125. let grpcGenerator = Generator(fileDescriptor, options: options)
  126. var grpcFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
  127. grpcFile.name = grpcFileName
  128. grpcFile.content = grpcGenerator.code
  129. response.file.append(grpcFile)
  130. }
  131. }
  132. // return everything to the caller
  133. let serializedResponse = try response.serializedData()
  134. FileHandle.standardOutput.write(serializedResponse)
  135. }
  136. do {
  137. try main()
  138. } catch {
  139. Log("ERROR: \(error)")
  140. }