Skip to content

Commit

Permalink
Add import paths to SPM Plugin (apple#1373)
Browse files Browse the repository at this point in the history
* add import paths to config

* add comment

* Update Plugins/SwiftProtobufPlugin/plugin.swift

Co-authored-by: Franz Busch <privat@franz-busch.de>

* always include target.directory in import paths

---------

Co-authored-by: Franz Busch <privat@franz-busch.de>
  • Loading branch information
2 people authored and thomasvl committed Feb 14, 2023
1 parent aaa267c commit e102a04
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Plugins/SwiftProtobufPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -111,7 +123,8 @@ struct SwiftProtobufPlugin: BuildToolPlugin {
invocation: invocation,
protocPath: protocPath,
protocGenSwiftPath: protocGenSwiftPath,
outputDirectory: outputDirectory
outputDirectory: outputDirectory,
importPaths: importPaths
)
}
}
Expand All @@ -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 <path>" 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)")
Expand Down

0 comments on commit e102a04

Please sign in to comment.