Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import paths to SPM Plugin #1373

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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