Skip to content
Merged
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"
}
]
}
12 changes: 11 additions & 1 deletion 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 @@ -15,7 +16,16 @@ final class ExampleTests: XCTestCase {
}

func testImport() {
let foo = Foo.with { $0.bar = .with { $0.name = "Bar" } }
let foo = Import.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")
}
}
25 changes: 21 additions & 4 deletions Plugins/SwiftProtobufPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ struct SwiftProtobufPlugin {
}

/// An array of paths to `.proto` files for this invocation.
///
/// If the `protoPath` parameter is specified, the files must be specified
/// relative to that directory. Otherwise, relative to the target source directory.
var protoFiles: [String]
/// The visibility of the generated files.
var visibility: Visibility?
Expand All @@ -86,6 +89,15 @@ struct SwiftProtobufPlugin {
var implementationOnlyImports: Bool?
/// Whether import statements should be preceded with visibility.
var useAccessLevelOnImports: Bool?
/// Overrides the base directory used to find protobuf files.
///
/// This must be specified as a path relative to the target source directory.
/// For example, if you are storing the protofiles at `MyLibrary/Sources/MyLib/protos`,
/// you should specify `protos` as the value for this parameter.
///
/// If you have multiple subdirectories you wish to include,
/// you should specify multiple `invocations` instead.
var protoPath: String?
}

/// The path to the `protoc` binary.
Expand Down Expand Up @@ -171,10 +183,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 +219,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