From e102a0417a51afbf79f2edb05348b5a70fa3c456 Mon Sep 17 00:00:00 2001 From: Sami Suteria Date: Tue, 14 Feb 2023 07:19:49 -0800 Subject: [PATCH] Add import paths to SPM Plugin (#1373) * add import paths to config * add comment * Update Plugins/SwiftProtobufPlugin/plugin.swift Co-authored-by: Franz Busch * always include target.directory in import paths --------- Co-authored-by: Franz Busch --- Plugins/SwiftProtobufPlugin/plugin.swift | 27 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Plugins/SwiftProtobufPlugin/plugin.swift b/Plugins/SwiftProtobufPlugin/plugin.swift index 85bc30ea9..eb621549a 100644 --- a/Plugins/SwiftProtobufPlugin/plugin.swift +++ b/Plugins/SwiftProtobufPlugin/plugin.swift @@ -64,6 +64,13 @@ struct SwiftProtobufPlugin: BuildToolPlugin { var fileNaming: FileNaming? } + /// Specify the directory in which to search for + /// imports. May be specified multiple times; + /// directories will be searched in order. + /// The target source directory is always appended + /// to the import paths. + var importPaths: [String]? + /// The path to the `protoc` binary. /// /// If this is not set, SPM will try to find the tool itself. @@ -88,6 +95,11 @@ struct SwiftProtobufPlugin: BuildToolPlugin { try validateConfiguration(configuration) + var importPaths: [Path] = [target.directory] + if let configuredImportPaths = configuration.importPaths { + importPaths.append(contentsOf: configuredImportPaths.map { Path($0) }) + } + // We need to find the path of protoc and protoc-gen-swift let protocPath: Path if let configuredProtocPath = configuration.protocPath { @@ -111,7 +123,8 @@ struct SwiftProtobufPlugin: BuildToolPlugin { invocation: invocation, protocPath: protocPath, protocGenSwiftPath: protocGenSwiftPath, - outputDirectory: outputDirectory + outputDirectory: outputDirectory, + importPaths: importPaths ) } } @@ -124,23 +137,27 @@ struct SwiftProtobufPlugin: BuildToolPlugin { /// - protocPath: The path to the `protoc` binary. /// - protocGenSwiftPath: The path to the `protoc-gen-swift` binary. /// - outputDirectory: The output directory for the generated files. + /// - importPaths: List of paths to pass with "-I " to `protoc` /// - Returns: The build command. private func invokeProtoc( target: Target, invocation: Configuration.Invocation, protocPath: Path, protocGenSwiftPath: Path, - outputDirectory: Path + outputDirectory: Path, + importPaths: [Path] ) -> Command { // Construct the `protoc` arguments. var protocArgs = [ "--plugin=protoc-gen-swift=\(protocGenSwiftPath)", "--swift_out=\(outputDirectory)", - // We include the target directory as a proto search path - "-I", - "\(target.directory)", ] + importPaths.forEach { path in + protocArgs.append("-I") + protocArgs.append("\(path)") + } + // Add the visibility if it was set if let visibility = invocation.visibility { protocArgs.append("--swift_opt=Visibility=\(visibility.rawValue)")