Skip to content

Commit 560d66f

Browse files
samisuteriaWendellXY
authored andcommitted
Add import paths to SPM Plugin (grpc#1568)
1 parent 1d21877 commit 560d66f

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Diff for: Plugins/GRPCSwiftPlugin/plugin.swift

+22-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
5151
var keepMethodCasing: Bool?
5252
}
5353

54+
/// Specify the directory in which to search for
55+
/// imports. May be specified multiple times;
56+
/// directories will be searched in order.
57+
/// The target source directory is always appended
58+
/// to the import paths.
59+
var importPaths: [String]?
60+
5461
/// The path to the `protoc` binary.
5562
///
5663
/// If this is not set, SPM will try to find the tool itself.
@@ -75,6 +82,11 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
7582

7683
try self.validateConfiguration(configuration)
7784

85+
var importPaths: [Path] = [target.directory]
86+
if let configuredImportPaths = configuration.importPaths {
87+
importPaths.append(contentsOf: configuredImportPaths.map { Path($0) })
88+
}
89+
7890
// We need to find the path of protoc and protoc-gen-grpc-swift
7991
let protocPath: Path
8092
if let configuredProtocPath = configuration.protocPath {
@@ -97,7 +109,8 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
97109
invocation: invocation,
98110
protocPath: protocPath,
99111
protocGenGRPCSwiftPath: protocGenGRPCSwiftPath,
100-
outputDirectory: outputDirectory
112+
outputDirectory: outputDirectory,
113+
importPaths: importPaths
101114
)
102115
}
103116
}
@@ -110,23 +123,27 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
110123
/// - protocPath: The path to the `protoc` binary.
111124
/// - protocGenSwiftPath: The path to the `protoc-gen-swift` binary.
112125
/// - outputDirectory: The output directory for the generated files.
126+
/// - importPaths: List of paths to pass with "-I <path>" to `protoc`
113127
/// - Returns: The build command.
114128
private func invokeProtoc(
115129
target: Target,
116130
invocation: Configuration.Invocation,
117131
protocPath: Path,
118132
protocGenGRPCSwiftPath: Path,
119-
outputDirectory: Path
133+
outputDirectory: Path,
134+
importPaths: [Path]
120135
) -> Command {
121136
// Construct the `protoc` arguments.
122137
var protocArgs = [
123138
"--plugin=protoc-gen-grpc-swift=\(protocGenGRPCSwiftPath)",
124139
"--grpc-swift_out=\(outputDirectory)",
125-
// We include the target directory as a proto search path
126-
"-I",
127-
"\(target.directory)",
128140
]
129141

142+
importPaths.forEach { path in
143+
protocArgs.append("-I")
144+
protocArgs.append("\(path)")
145+
}
146+
130147
if let visibility = invocation.visibility {
131148
protocArgs.append("--grpc-swift_opt=Visibility=\(visibility.rawValue.capitalized)")
132149
}

0 commit comments

Comments
 (0)