Skip to content

Commit 97e6137

Browse files
committed
Let the WireCompiler handle multiple targets in the same command
1 parent bed43d5 commit 97e6137

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class WireCompiler internal constructor(
150150
emitDeclaredOptions = emitDeclaredOptions,
151151
emitAppliedOptions = emitAppliedOptions,
152152
)
153-
} else if (kotlinOut != null) {
153+
}
154+
if (kotlinOut != null) {
154155
targets += KotlinTarget(
155156
exclusive = kotlinExclusive,
156157
outDirectory = kotlinOut,
@@ -167,11 +168,13 @@ class WireCompiler internal constructor(
167168
escapeKotlinKeywords = kotlinEscapeKeywords,
168169
emitProtoReader32 = emitProtoReader32,
169170
)
170-
} else if (swiftOut != null) {
171+
}
172+
if (swiftOut != null) {
171173
targets += SwiftTarget(
172174
outDirectory = swiftOut,
173175
)
174-
} else if (customOut != null || schemaHandlerFactoryClass != null) {
176+
}
177+
if (customOut != null || schemaHandlerFactoryClass != null) {
175178
if (customOut == null || schemaHandlerFactoryClass == null) {
176179
throw IllegalArgumentException("Both custom_out and schema_handler_factory_class need to be set")
177180
}

wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,45 @@ class WireCompilerTest {
115115
assertThat(logs).containsExactly("artifactHandled(com.squareup.wire.protos.person.Person, Java)")
116116
}
117117

118+
@Test
119+
fun runMultipleTargets() {
120+
val sources = arrayOf("person.proto")
121+
122+
val args = ArrayList<String>()
123+
args.add(TargetLanguage.JAVA.protoPathArg())
124+
args.add(TargetLanguage.JAVA.outArg("/".toPath() / testDir))
125+
args.add(TargetLanguage.KOTLIN.protoPathArg())
126+
args.add(TargetLanguage.KOTLIN.outArg("/".toPath() / testDir))
127+
args.add("--no_kotlin_exclusive")
128+
args.add("--dry_run")
129+
args.addAll(sources)
130+
131+
val logs = mutableListOf<String>()
132+
val logger = object : WireLogger {
133+
override fun artifactHandled(outputPath: Path, qualifiedName: String, targetName: String) {
134+
logs.add("artifactHandled($qualifiedName, $targetName)")
135+
}
136+
override fun artifactSkipped(type: ProtoType, targetName: String) = Unit
137+
override fun unusedRoots(unusedRoots: Set<String>) = Unit
138+
override fun unusedPrunes(unusedPrunes: Set<String>) = Unit
139+
override fun unusedIncludesInTarget(unusedIncludes: Set<String>) = Unit
140+
override fun unusedExcludesInTarget(unusedExcludes: Set<String>) = Unit
141+
}
142+
val fs = fileSystem
143+
val compiler = WireCompiler.forArgs(fs, logger, *args.toTypedArray<String>())
144+
compiler.compile()
145+
146+
// We assert nothing has been generated.
147+
assertJavaOutputs(arrayOf())
148+
assertKotlinOutputs(arrayOf())
149+
assertSwiftOutputs(arrayOf())
150+
// But we logged things because we're dry-running.
151+
assertThat(logs).containsExactly(
152+
"artifactHandled(com.squareup.wire.protos.person.Person, Kotlin)",
153+
"artifactHandled(com.squareup.wire.protos.person.Person, Java)",
154+
)
155+
}
156+
118157
@Test
119158
fun testPersonAndroid() {
120159
val sources = arrayOf("person.proto")
@@ -726,6 +765,9 @@ class WireCompilerTest {
726765
private fun assertKotlinOutputs(outputs: Array<String>, suffix: String = "") =
727766
assertOutputs(TargetLanguage.KOTLIN, outputs, suffix)
728767

768+
private fun assertSwiftOutputs(outputs: Array<String>, suffix: String = "") =
769+
assertOutputs(TargetLanguage.SWIFT, outputs, suffix)
770+
729771
private fun assertOutputs(target: TargetLanguage, outputs: Array<String>, suffix: String = "") {
730772
val filesAfter = paths
731773
assertThat(filesAfter.size)
@@ -770,6 +812,11 @@ class WireCompilerTest {
770812
override fun outArg(testDirPath: Path) = "--kotlin_out=$testDirPath"
771813
override fun protoFolderSuffix() = "kotlin"
772814
},
815+
SWIFT {
816+
override fun protoPathArg() = "--proto_path=../wire-tests/src/commonTest/proto/kotlin"
817+
override fun outArg(testDirPath: Path) = "--swift_out=$testDirPath"
818+
override fun protoFolderSuffix() = "swift"
819+
},
773820
;
774821

775822
abstract fun protoPathArg(): String

0 commit comments

Comments
 (0)