Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the WireCompiler handle multiple targets in the same command #3106

Merged
merged 1 commit into from
Sep 16, 2024
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
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: 15.4.0

# 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: 15.4.0

# 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: 15.4.0

# 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