2
0

plugin.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright 2022, 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 PackagePlugin
  18. @main
  19. struct GRPCSwiftPlugin {
  20. /// Errors thrown by the `GRPCSwiftPlugin`
  21. enum PluginError: Error, CustomStringConvertible {
  22. /// Indicates that the target where the plugin was applied to was not `SourceModuleTarget`.
  23. case invalidTarget(String)
  24. /// Indicates that the file extension of an input file was not `.proto`.
  25. case invalidInputFileExtension(String)
  26. /// Indicates that there was no configuration file at the required location.
  27. case noConfigFound(String)
  28. var description: String {
  29. switch self {
  30. case let .invalidTarget(target):
  31. return "Expected a SwiftSourceModuleTarget but got '\(target)'."
  32. case let .invalidInputFileExtension(path):
  33. return "The input file '\(path)' does not have a '.proto' extension."
  34. case let .noConfigFound(path):
  35. return """
  36. No configuration file found named '\(path)'. The file must not be listed in the \
  37. 'exclude:' argument for the target in Package.swift.
  38. """
  39. }
  40. }
  41. }
  42. /// The configuration of the plugin.
  43. struct Configuration: Codable {
  44. /// Encapsulates a single invocation of protoc.
  45. struct Invocation: Codable {
  46. /// The visibility of the generated files.
  47. enum Visibility: String, Codable {
  48. /// The generated files should have `internal` access level.
  49. case `internal`
  50. /// The generated files should have `public` access level.
  51. case `public`
  52. /// The generated files should have `package` access level.
  53. case `package`
  54. }
  55. /// An array of paths to `.proto` files for this invocation.
  56. var protoFiles: [String]
  57. /// The visibility of the generated files.
  58. var visibility: Visibility?
  59. /// Whether server code is generated.
  60. var server: Bool?
  61. /// Whether client code is generated.
  62. var client: Bool?
  63. /// Whether reflection data is generated.
  64. var reflectionData: Bool?
  65. /// Determines whether the casing of generated function names is kept.
  66. var keepMethodCasing: Bool?
  67. /// Whether the invocation is for `grpc-swift` v2.
  68. var _V2: Bool?
  69. }
  70. /// Specify the directory in which to search for
  71. /// imports. May be specified multiple times;
  72. /// directories will be searched in order.
  73. /// The target source directory is always appended
  74. /// to the import paths.
  75. var importPaths: [String]?
  76. /// The path to the `protoc` binary.
  77. ///
  78. /// If this is not set, SPM will try to find the tool itself.
  79. var protocPath: String?
  80. /// A list of invocations of `protoc` with the `GRPCSwiftPlugin`.
  81. var invocations: [Invocation]
  82. }
  83. static let configurationFileName = "grpc-swift-config.json"
  84. /// Create build commands for the given arguments
  85. /// - Parameters:
  86. /// - pluginWorkDirectory: The path of a writable directory into which the plugin or the build
  87. /// commands it constructs can write anything it wants.
  88. /// - sourceFiles: The input files that are associated with the target.
  89. /// - tool: The tool method from the context.
  90. /// - Returns: The build commands configured based on the arguments.
  91. func createBuildCommands(
  92. pluginWorkDirectory: PathLike,
  93. sourceFiles: FileList,
  94. tool: (String) throws -> PackagePlugin.PluginContext.Tool
  95. ) throws -> [Command] {
  96. let maybeConfigFile = sourceFiles.map { PathLike($0) }.first {
  97. $0.lastComponent == Self.configurationFileName
  98. }
  99. guard let configurationFilePath = maybeConfigFile else {
  100. throw PluginError.noConfigFound(Self.configurationFileName)
  101. }
  102. let data = try Data(contentsOf: URL(configurationFilePath))
  103. let configuration = try JSONDecoder().decode(Configuration.self, from: data)
  104. try self.validateConfiguration(configuration)
  105. let targetDirectory = configurationFilePath.removingLastComponent()
  106. var importPaths: [PathLike] = [targetDirectory]
  107. if let configuredImportPaths = configuration.importPaths {
  108. importPaths.append(contentsOf: configuredImportPaths.map { PathLike($0) })
  109. }
  110. // We need to find the path of protoc and protoc-gen-grpc-swift
  111. let protocPath: PathLike
  112. if let configuredProtocPath = configuration.protocPath {
  113. protocPath = PathLike(configuredProtocPath)
  114. } else if let environmentPath = ProcessInfo.processInfo.environment["PROTOC_PATH"] {
  115. // The user set the env variable, so let's take that
  116. protocPath = PathLike(environmentPath)
  117. } else {
  118. // The user didn't set anything so let's try see if SPM can find a binary for us
  119. protocPath = try PathLike(tool("protoc"))
  120. }
  121. let protocGenGRPCSwiftPath = try PathLike(tool("protoc-gen-grpc-swift"))
  122. return configuration.invocations.map { invocation in
  123. self.invokeProtoc(
  124. directory: targetDirectory,
  125. invocation: invocation,
  126. protocPath: protocPath,
  127. protocGenGRPCSwiftPath: protocGenGRPCSwiftPath,
  128. outputDirectory: pluginWorkDirectory,
  129. importPaths: importPaths
  130. )
  131. }
  132. }
  133. /// Invokes `protoc` with the given inputs
  134. ///
  135. /// - Parameters:
  136. /// - directory: The plugin's target directory.
  137. /// - invocation: The `protoc` invocation.
  138. /// - protocPath: The path to the `protoc` binary.
  139. /// - protocGenSwiftPath: The path to the `protoc-gen-swift` binary.
  140. /// - outputDirectory: The output directory for the generated files.
  141. /// - importPaths: List of paths to pass with "-I <path>" to `protoc`.
  142. /// - Returns: The build command configured based on the arguments
  143. private func invokeProtoc(
  144. directory: PathLike,
  145. invocation: Configuration.Invocation,
  146. protocPath: PathLike,
  147. protocGenGRPCSwiftPath: PathLike,
  148. outputDirectory: PathLike,
  149. importPaths: [PathLike]
  150. ) -> Command {
  151. // Construct the `protoc` arguments.
  152. var protocArgs = [
  153. "--plugin=protoc-gen-grpc-swift=\(protocGenGRPCSwiftPath)",
  154. "--grpc-swift_out=\(outputDirectory)",
  155. ]
  156. importPaths.forEach { path in
  157. protocArgs.append("-I")
  158. protocArgs.append("\(path)")
  159. }
  160. if let visibility = invocation.visibility {
  161. protocArgs.append("--grpc-swift_opt=Visibility=\(visibility.rawValue.capitalized)")
  162. }
  163. if let generateServerCode = invocation.server {
  164. protocArgs.append("--grpc-swift_opt=Server=\(generateServerCode)")
  165. }
  166. if let generateClientCode = invocation.client {
  167. protocArgs.append("--grpc-swift_opt=Client=\(generateClientCode)")
  168. }
  169. if let generateReflectionData = invocation.reflectionData {
  170. protocArgs.append("--grpc-swift_opt=ReflectionData=\(generateReflectionData)")
  171. }
  172. if let keepMethodCasingOption = invocation.keepMethodCasing {
  173. protocArgs.append("--grpc-swift_opt=KeepMethodCasing=\(keepMethodCasingOption)")
  174. }
  175. if let v2 = invocation._V2 {
  176. protocArgs.append("--grpc-swift_opt=_V2=\(v2)")
  177. }
  178. var inputFiles = [PathLike]()
  179. var outputFiles = [PathLike]()
  180. for var file in invocation.protoFiles {
  181. // Append the file to the protoc args so that it is used for generating
  182. protocArgs.append(file)
  183. inputFiles.append(directory.appending(file))
  184. // The name of the output file is based on the name of the input file.
  185. // We validated in the beginning that every file has the suffix of .proto
  186. // This means we can just drop the last 5 elements and append the new suffix
  187. file.removeLast(5)
  188. file.append("grpc.swift")
  189. let protobufOutputPath = outputDirectory.appending(file)
  190. // Add the outputPath as an output file
  191. outputFiles.append(protobufOutputPath)
  192. if invocation.reflectionData == true {
  193. // Remove .swift extension and add .reflection extension
  194. file.removeLast(5)
  195. file.append("reflection")
  196. let reflectionOutputPath = outputDirectory.appending(file)
  197. outputFiles.append(reflectionOutputPath)
  198. }
  199. }
  200. // Construct the command. Specifying the input and output paths lets the build
  201. // system know when to invoke the command. The output paths are passed on to
  202. // the rule engine in the build system.
  203. return Command.buildCommand(
  204. displayName: "Generating gRPC Swift files from proto files",
  205. executable: protocPath,
  206. arguments: protocArgs,
  207. inputFiles: inputFiles + [protocGenGRPCSwiftPath],
  208. outputFiles: outputFiles
  209. )
  210. }
  211. /// Validates the configuration file for various user errors.
  212. private func validateConfiguration(_ configuration: Configuration) throws {
  213. for invocation in configuration.invocations {
  214. for protoFile in invocation.protoFiles {
  215. if !protoFile.hasSuffix(".proto") {
  216. throw PluginError.invalidInputFileExtension(protoFile)
  217. }
  218. }
  219. }
  220. }
  221. }
  222. extension GRPCSwiftPlugin: BuildToolPlugin {
  223. func createBuildCommands(
  224. context: PluginContext,
  225. target: Target
  226. ) async throws -> [Command] {
  227. guard let swiftTarget = target as? SwiftSourceModuleTarget else {
  228. throw PluginError.invalidTarget("\(type(of: target))")
  229. }
  230. let workDirectory = PathLike(context.pluginWorkDirectory)
  231. return try self.createBuildCommands(
  232. pluginWorkDirectory: workDirectory,
  233. sourceFiles: swiftTarget.sourceFiles,
  234. tool: context.tool
  235. )
  236. }
  237. }
  238. // 'Path' was effectively deprecated in Swift 6 in favour of URL. ('Effectively' because all
  239. // methods, properties, and conformances have been deprecated but the type hasn't.) This type wraps
  240. // either depending on the compiler version.
  241. struct PathLike: CustomStringConvertible {
  242. typealias Value = Path
  243. private(set) var value: Value
  244. init(_ value: Value) {
  245. self.value = value
  246. }
  247. init(_ path: String) {
  248. self.value = Path(path)
  249. }
  250. init(_ element: FileList.Element) {
  251. self.value = element.path
  252. }
  253. init(_ element: PluginContext.Tool) {
  254. self.value = element.path
  255. }
  256. var description: String {
  257. return String(describing: self.value)
  258. }
  259. var lastComponent: String {
  260. return self.value.lastComponent
  261. }
  262. func removingLastComponent() -> Self {
  263. var copy = self
  264. copy.value = self.value.removingLastComponent()
  265. return copy
  266. }
  267. func appending(_ path: String) -> Self {
  268. var copy = self
  269. copy.value = self.value.appending(path)
  270. return copy
  271. }
  272. }
  273. extension Command {
  274. static func buildCommand(
  275. displayName: String?,
  276. executable: PathLike,
  277. arguments: [String],
  278. inputFiles: [PathLike],
  279. outputFiles: [PathLike]
  280. ) -> PackagePlugin.Command {
  281. return Self.buildCommand(
  282. displayName: displayName,
  283. executable: executable.value,
  284. arguments: arguments,
  285. inputFiles: inputFiles.map { $0.value },
  286. outputFiles: outputFiles.map { $0.value }
  287. )
  288. }
  289. }
  290. extension URL {
  291. init(_ pathLike: PathLike) {
  292. self = URL(fileURLWithPath: "\(pathLike.value)")
  293. }
  294. }
  295. #if canImport(XcodeProjectPlugin)
  296. import XcodeProjectPlugin
  297. extension GRPCSwiftPlugin: XcodeBuildToolPlugin {
  298. func createBuildCommands(
  299. context: XcodePluginContext,
  300. target: XcodeTarget
  301. ) throws -> [Command] {
  302. let workDirectory = PathLike(context.pluginWorkDirectory)
  303. return try self.createBuildCommands(
  304. pluginWorkDirectory: workDirectory,
  305. sourceFiles: target.inputFiles,
  306. tool: context.tool
  307. )
  308. }
  309. }
  310. #endif