Skip to content

Commit

Permalink
Let the WireCompiler handle multiple targets in the same command
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Sep 16, 2024
1 parent bed43d5 commit 9bcadc1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Setup Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
xcode-version: macos-14

# We set `SDKROOT` as a workaround for https://github.com/gradle/gradle/pull/29227
- name: Check test files
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
- name: Setup Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
xcode-version: macos-14

# We set `SDKROOT` as a workaround for https://github.com/gradle/gradle/pull/29227
- name: Test Swift Runtime
Expand Down Expand Up @@ -206,7 +206,7 @@ jobs:
- name: Setup Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
xcode-version: macos-14

# We set `SDKROOT` as a workaround for https://github.com/gradle/gradle/pull/29227
- name: Upload Artifacts
Expand Down
9 changes: 6 additions & 3 deletions wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ class WireCompiler internal constructor(
emitDeclaredOptions = emitDeclaredOptions,
emitAppliedOptions = emitAppliedOptions,
)
} else if (kotlinOut != null) {
}
if (kotlinOut != null) {
targets += KotlinTarget(
exclusive = kotlinExclusive,
outDirectory = kotlinOut,
Expand All @@ -167,11 +168,13 @@ class WireCompiler internal constructor(
escapeKotlinKeywords = kotlinEscapeKeywords,
emitProtoReader32 = emitProtoReader32,
)
} else if (swiftOut != null) {
}
if (swiftOut != null) {
targets += SwiftTarget(
outDirectory = swiftOut,
)
} else if (customOut != null || schemaHandlerFactoryClass != null) {
}
if (customOut != null || schemaHandlerFactoryClass != null) {
if (customOut == null || schemaHandlerFactoryClass == null) {
throw IllegalArgumentException("Both custom_out and schema_handler_factory_class need to be set")
}
Expand Down
47 changes: 47 additions & 0 deletions wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,45 @@ class WireCompilerTest {
assertThat(logs).containsExactly("artifactHandled(com.squareup.wire.protos.person.Person, Java)")
}

@Test
fun runMultipleTargets() {
val sources = arrayOf("person.proto")

val args = ArrayList<String>()
args.add(TargetLanguage.JAVA.protoPathArg())
args.add(TargetLanguage.JAVA.outArg("/".toPath() / testDir))
args.add(TargetLanguage.KOTLIN.protoPathArg())
args.add(TargetLanguage.KOTLIN.outArg("/".toPath() / testDir))
args.add("--no_kotlin_exclusive")
args.add("--dry_run")
args.addAll(sources)

val logs = mutableListOf<String>()
val logger = object : WireLogger {
override fun artifactHandled(outputPath: Path, qualifiedName: String, targetName: String) {
logs.add("artifactHandled($qualifiedName, $targetName)")
}
override fun artifactSkipped(type: ProtoType, targetName: String) = Unit
override fun unusedRoots(unusedRoots: Set<String>) = Unit
override fun unusedPrunes(unusedPrunes: Set<String>) = Unit
override fun unusedIncludesInTarget(unusedIncludes: Set<String>) = Unit
override fun unusedExcludesInTarget(unusedExcludes: Set<String>) = Unit
}
val fs = fileSystem
val compiler = WireCompiler.forArgs(fs, logger, *args.toTypedArray<String>())
compiler.compile()

// We assert nothing has been generated.
assertJavaOutputs(arrayOf())
assertKotlinOutputs(arrayOf())
assertSwiftOutputs(arrayOf())
// But we logged things because we're dry-running.
assertThat(logs).containsExactly(
"artifactHandled(com.squareup.wire.protos.person.Person, Kotlin)",
"artifactHandled(com.squareup.wire.protos.person.Person, Java)",
)
}

@Test
fun testPersonAndroid() {
val sources = arrayOf("person.proto")
Expand Down Expand Up @@ -726,6 +765,9 @@ class WireCompilerTest {
private fun assertKotlinOutputs(outputs: Array<String>, suffix: String = "") =
assertOutputs(TargetLanguage.KOTLIN, outputs, suffix)

private fun assertSwiftOutputs(outputs: Array<String>, suffix: String = "") =
assertOutputs(TargetLanguage.SWIFT, outputs, suffix)

private fun assertOutputs(target: TargetLanguage, outputs: Array<String>, suffix: String = "") {
val filesAfter = paths
assertThat(filesAfter.size)
Expand Down Expand Up @@ -770,6 +812,11 @@ class WireCompilerTest {
override fun outArg(testDirPath: Path) = "--kotlin_out=$testDirPath"
override fun protoFolderSuffix() = "kotlin"
},
SWIFT {
override fun protoPathArg() = "--proto_path=../wire-tests/src/commonTest/proto/kotlin"
override fun outArg(testDirPath: Path) = "--swift_out=$testDirPath"
override fun protoFolderSuffix() = "swift"
},
;

abstract fun protoPathArg(): String
Expand Down

0 comments on commit 9bcadc1

Please sign in to comment.