Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions PluginExamples/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ let package = Package(
.plugin(name: "SwiftProtobufPlugin", package: "swift-protobuf")
]
),
.target(
name: "CustomProtoPath",
dependencies: [
.product(name: "SwiftProtobuf", package: "swift-protobuf")
],
plugins: [
.plugin(name: "SwiftProtobufPlugin", package: "swift-protobuf")
]
),
.testTarget(
name: "ExampleTests",
dependencies: [
.target(name: "Simple"),
.target(name: "Nested"),
.target(name: "Import"),
.target(name: "AccessLevelOnImport"),
.target(name: "CustomProtoPath"),
]
),
],
Expand Down
3 changes: 3 additions & 0 deletions PluginExamples/Sources/CustomProtoPath/empty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// DO NOT DELETE.
///
/// We need to keep this file otherwise the plugin is not running.
5 changes: 5 additions & 0 deletions PluginExamples/Sources/CustomProtoPath/protos/Bar/Bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

message Bar {
string name = 1;
}
7 changes: 7 additions & 0 deletions PluginExamples/Sources/CustomProtoPath/protos/Foo/Foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

import "Bar/Bar.proto";

message Foo {
Bar bar = 1;
}
9 changes: 9 additions & 0 deletions PluginExamples/Sources/CustomProtoPath/protos/Main.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

import "Bar/Bar.proto";
import "Foo/Foo.proto";

message Main {
Bar bar = 1;
Foo foo = 2;
}
13 changes: 13 additions & 0 deletions PluginExamples/Sources/CustomProtoPath/swift-protobuf-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"invocations": [
{
"protoFiles": [
"Bar/Bar.proto",
"Foo/Foo.proto",
"Main.proto"
],
"visibility": "public",
"protoPath": "protos"
}
]
}
10 changes: 10 additions & 0 deletions PluginExamples/Sources/ExampleTests/ExampleTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CustomProtoPath
import Import
import Nested
import Simple
Expand All @@ -18,4 +19,13 @@ final class ExampleTests: XCTestCase {
let foo = Foo.with { $0.bar = .with { $0.name = "Bar" } }
XCTAssertEqual(foo.bar.name, "Bar")
}

func testCustomProtoPath() {
let main = CustomProtoPath.Main.with {
$0.bar = .with { $0.name = "Bar" }
$0.foo = .with { $0.bar = .with { $0.name = "BarInFoo" } }
}
XCTAssertEqual(main.bar.name, "Bar")
XCTAssertEqual(main.foo.bar.name, "BarInFoo")
}
}
15 changes: 11 additions & 4 deletions Plugins/SwiftProtobufPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ struct SwiftProtobufPlugin {
var implementationOnlyImports: Bool?
/// Whether import statements should be preceded with visibility.
var useAccessLevelOnImports: Bool?
/// Overrides the path to look for protobuf files
var protoPath: String?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely be an array rather than a String as you can specify -I/--proto_path multiple times to protoc.

Given the example in the PR description it's not hard to imagine multiple git submodules being added.

}

/// The path to the `protoc` binary.
Expand Down Expand Up @@ -171,10 +173,15 @@ struct SwiftProtobufPlugin {
"--swift_out=\(outputDirectory)",
]

// We need to add the target directory as a search path since we require the user to specify
// the proto files relative to it.
let protoDirectory =
if let protoPath = invocation.protoPath {
directory.appending(protoPath)
} else {
directory
}

protocArgs.append("-I")
protocArgs.append("\(directory)")
protocArgs.append("\(protoDirectory)")

// Add the visibility if it was set
if let visibility = invocation.visibility {
Expand Down Expand Up @@ -202,7 +209,7 @@ struct SwiftProtobufPlugin {
for var file in invocation.protoFiles {
// Append the file to the protoc args so that it is used for generating
protocArgs.append("\(file)")
inputFiles.append(directory.appending(file))
inputFiles.append(protoDirectory.appending(file))

// The name of the output file is based on the name of the input file.
// We validated in the beginning that every file has the suffix of .proto
Expand Down