فهرست منبع

Add import paths to SPM Plugin (#1568)

Sami Suteria 2 سال پیش
والد
کامیت
3e4c8e393c
1فایلهای تغییر یافته به همراه22 افزوده شده و 5 حذف شده
  1. 22 5
      Plugins/GRPCSwiftPlugin/plugin.swift

+ 22 - 5
Plugins/GRPCSwiftPlugin/plugin.swift

@@ -51,6 +51,13 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
       var keepMethodCasing: Bool?
     }
 
+    /// 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.
@@ -75,6 +82,11 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
 
     try self.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-grpc-swift
     let protocPath: Path
     if let configuredProtocPath = configuration.protocPath {
@@ -97,7 +109,8 @@ struct GRPCSwiftPlugin: BuildToolPlugin {
         invocation: invocation,
         protocPath: protocPath,
         protocGenGRPCSwiftPath: protocGenGRPCSwiftPath,
-        outputDirectory: outputDirectory
+        outputDirectory: outputDirectory,
+        importPaths: importPaths
       )
     }
   }
@@ -110,23 +123,27 @@ struct GRPCSwiftPlugin: 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,
     protocGenGRPCSwiftPath: Path,
-    outputDirectory: Path
+    outputDirectory: Path,
+    importPaths: [Path]
   ) -> Command {
     // Construct the `protoc` arguments.
     var protocArgs = [
       "--plugin=protoc-gen-grpc-swift=\(protocGenGRPCSwiftPath)",
       "--grpc-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)")
+    }
+
     if let visibility = invocation.visibility {
       protocArgs.append("--grpc-swift_opt=Visibility=\(visibility.rawValue.capitalized)")
     }