From 3844028ebbb57dddd1d582d39dba38725714b49d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Quenaudon?= Date: Mon, 16 Sep 2024 14:46:13 +0100 Subject: [PATCH] all target can be non-exclusive via CLI --- wire-compiler/api/wire-compiler.api | 2 + .../java/com/squareup/wire/WireCompiler.kt | 12 +++ .../com/squareup/wire/WireCompilerTest.kt | 97 ++++++++++++++----- 3 files changed, 85 insertions(+), 26 deletions(-) diff --git a/wire-compiler/api/wire-compiler.api b/wire-compiler/api/wire-compiler.api index 1a1f8818be..670ea6770b 100644 --- a/wire-compiler/api/wire-compiler.api +++ b/wire-compiler/api/wire-compiler.api @@ -23,6 +23,7 @@ public final class com/squareup/wire/WireCompiler { public final fun getEmitProtoReader32 ()Z public final fun getEventListenerFactoryClasses ()Ljava/util/List; public final fun getFs ()Lokio/FileSystem; + public final fun getJavaExclusive ()Z public final fun getJavaInterop ()Z public final fun getJavaOut ()Ljava/lang/String; public final fun getKotlinBoxOneOfsMinSize ()I @@ -40,6 +41,7 @@ public final class com/squareup/wire/WireCompiler { public final fun getProtoPaths ()Ljava/util/List; public final fun getSchemaHandlerFactoryClass ()Ljava/lang/String; public final fun getSourceFileNames ()Ljava/util/List; + public final fun getSwiftExclusive ()Z public final fun getSwiftOut ()Ljava/lang/String; public final fun getTreeShakingRoots ()Ljava/util/List; public final fun getTreeShakingRubbish ()Ljava/util/List; diff --git a/wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt b/wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt index 07faec05a3..c9c7dfa4b9 100644 --- a/wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt +++ b/wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt @@ -126,7 +126,9 @@ class WireCompiler internal constructor( val permitPackageCycles: Boolean, val javaInterop: Boolean, val kotlinBoxOneOfsMinSize: Int, + val javaExclusive: Boolean, val kotlinExclusive: Boolean, + val swiftExclusive: Boolean, val kotlinRpcCallStyle: RpcCallStyle, val kotlinRpcRole: RpcRole, val kotlinSingleMethodServices: Boolean, @@ -149,6 +151,7 @@ class WireCompiler internal constructor( compact = emitCompact, emitDeclaredOptions = emitDeclaredOptions, emitAppliedOptions = emitAppliedOptions, + exclusive = javaExclusive, ) } if (kotlinOut != null) { @@ -172,6 +175,7 @@ class WireCompiler internal constructor( if (swiftOut != null) { targets += SwiftTarget( outDirectory = swiftOut, + exclusive = swiftExclusive, ) } if (customOut != null || schemaHandlerFactoryClass != null) { @@ -263,7 +267,9 @@ class WireCompiler internal constructor( private const val JAVA_INTEROP = "--java_interop" private const val DRY_RUN = "--dry_run" private const val KOTLIN_BOX_ONEOFS_MIN_SIZE = "--kotlin_box_oneofs_min_size=" + private const val NO_JAVA_EXCLUSIVE = "--no_java_exclusive" private const val NO_KOTLIN_EXCLUSIVE = "--no_kotlin_exclusive" + private const val NO_SWIFT_EXCLUSIVE = "--no_swift_exclusive" private const val KOTLIN_RPC_CALL_STYLE = "--kotlin_rpc_call_style=" private const val KOTLIN_RPC_ROLE = "--kotlin_rpc_role=" private const val KOTLIN_SINGLE_METHOD_SERVICES = "--kotlin_single_method_services" @@ -325,7 +331,9 @@ class WireCompiler internal constructor( var permitPackageCycles = false var javaInterop = false var kotlinBoxOneOfsMinSize = 5_000 + var javaExclusive = true var kotlinExclusive = true + var swiftExclusive = true var kotlinRpcCallStyle = RpcCallStyle.SUSPENDING var kotlinRpcRole = RpcRole.CLIENT var kotlinSingleMethodServices = false @@ -422,7 +430,9 @@ class WireCompiler internal constructor( modules = parseManifestModules(yaml) } + arg == NO_JAVA_EXCLUSIVE -> javaExclusive = false arg == NO_KOTLIN_EXCLUSIVE -> kotlinExclusive = false + arg == NO_SWIFT_EXCLUSIVE -> swiftExclusive = false arg == KOTLIN_SINGLE_METHOD_SERVICES -> kotlinSingleMethodServices = true arg == KOTLIN_GRPC_SERVER_COMPATIBLE -> { throw IllegalArgumentException( @@ -481,7 +491,9 @@ class WireCompiler internal constructor( permitPackageCycles = permitPackageCycles, javaInterop = javaInterop, kotlinBoxOneOfsMinSize = kotlinBoxOneOfsMinSize, + javaExclusive = javaExclusive, kotlinExclusive = kotlinExclusive, + swiftExclusive = swiftExclusive, kotlinRpcCallStyle = kotlinRpcCallStyle, kotlinRpcRole = kotlinRpcRole, kotlinSingleMethodServices = kotlinSingleMethodServices, diff --git a/wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt b/wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt index 85a5a9aa12..9c6be27b85 100644 --- a/wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt +++ b/wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt @@ -88,10 +88,11 @@ class WireCompilerTest { fun testPersonDryRun() { val sources = arrayOf("person.proto") - val args = ArrayList() - args.add(TargetLanguage.JAVA.protoPathArg()) - args.add(TargetLanguage.JAVA.outArg("/".toPath() / testDir)) - args.add("--dry_run") + val args = mutableListOf( + TargetLanguage.JAVA.protoPathArg(), + TargetLanguage.JAVA.outArg("/".toPath() / testDir), + "--dry_run", + ) args.addAll(sources) val logs = mutableListOf() @@ -105,8 +106,7 @@ class WireCompilerTest { override fun unusedIncludesInTarget(unusedIncludes: Set) = Unit override fun unusedExcludesInTarget(unusedExcludes: Set) = Unit } - val fs = fileSystem - val compiler = WireCompiler.forArgs(fs, logger, *args.toTypedArray()) + val compiler = WireCompiler.forArgs(fileSystem, logger, *args.toTypedArray()) compiler.compile() // We assert nothing has been generated. @@ -119,13 +119,56 @@ class WireCompilerTest { fun runMultipleTargets() { val sources = arrayOf("person.proto") - val args = ArrayList() - 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") + val args = mutableListOf( + TargetLanguage.JAVA.protoPathArg(), + TargetLanguage.JAVA.outArg("/".toPath() / testDir), + TargetLanguage.KOTLIN.protoPathArg(), + TargetLanguage.KOTLIN.outArg("/".toPath() / testDir), + "--no_kotlin_exclusive", + "--dry_run", + ) + args.addAll(sources) + + val logs = mutableListOf() + 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) = Unit + override fun unusedPrunes(unusedPrunes: Set) = Unit + override fun unusedIncludesInTarget(unusedIncludes: Set) = Unit + override fun unusedExcludesInTarget(unusedExcludes: Set) = Unit + } + val compiler = WireCompiler.forArgs(fileSystem, logger, *args.toTypedArray()) + 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 notExclusiveTargets() { + val sources = arrayOf("person.proto") + + val args = mutableListOf( + TargetLanguage.JAVA.protoPathArg(), + TargetLanguage.JAVA.outArg("/".toPath() / testDir), + TargetLanguage.KOTLIN.protoPathArg(), + TargetLanguage.KOTLIN.outArg("/".toPath() / testDir), + TargetLanguage.SWIFT.protoPathArg(), + TargetLanguage.SWIFT.outArg("/".toPath() / testDir), + "--no_kotlin_exclusive", + "--no_swift_exclusive", + "--dry_run", + ) args.addAll(sources) val logs = mutableListOf() @@ -139,8 +182,7 @@ class WireCompilerTest { override fun unusedIncludesInTarget(unusedIncludes: Set) = Unit override fun unusedExcludesInTarget(unusedExcludes: Set) = Unit } - val fs = fileSystem - val compiler = WireCompiler.forArgs(fs, logger, *args.toTypedArray()) + val compiler = WireCompiler.forArgs(fileSystem, logger, *args.toTypedArray()) compiler.compile() // We assert nothing has been generated. @@ -150,6 +192,7 @@ class WireCompilerTest { // But we logged things because we're dry-running. assertThat(logs).containsExactly( "artifactHandled(com.squareup.wire.protos.person.Person, Kotlin)", + "artifactHandled(.Person declared in person.proto, Swift)", "artifactHandled(com.squareup.wire.protos.person.Person, Java)", ) } @@ -495,9 +538,10 @@ class WireCompilerTest { @Test fun sourceInJar() { val sources = arrayOf("squareup/geology/period.proto", "squareup/dinosaurs/dinosaur.proto") - val args = ArrayList() - args.add("--proto_path=../wire-tests/src/commonTest/proto/kotlin/protos.jar") - args.add(TargetLanguage.KOTLIN.outArg("/".toPath() / testDir)) + val args = mutableListOf( + "--proto_path=../wire-tests/src/commonTest/proto/kotlin/protos.jar", + TargetLanguage.KOTLIN.outArg("/".toPath() / testDir), + ) Collections.addAll(args, *sources) logger = StringWireLogger() val compiler = WireCompiler.forArgs(fileSystem, logger!!, *args.toTypedArray()) @@ -514,9 +558,10 @@ class WireCompilerTest { fun sourceDependsOnJar() { // `dinosaur.proto` depends on `period.proto` which both are in `protos.jar`. val sources = arrayOf("squareup/dinosaurs/dinosaur.proto") - val args = ArrayList() - args.add("--proto_path=../wire-tests/src/commonTest/proto/kotlin/protos.jar") - args.add(TargetLanguage.KOTLIN.outArg("/".toPath() / testDir)) + val args = mutableListOf( + "--proto_path=../wire-tests/src/commonTest/proto/kotlin/protos.jar", + TargetLanguage.KOTLIN.outArg("/".toPath() / testDir), + ) Collections.addAll(args, *sources) logger = StringWireLogger() val compiler = WireCompiler.forArgs(fileSystem, logger!!, *args.toTypedArray()) @@ -747,15 +792,15 @@ class WireCompilerTest { sources: Array, vararg extraArgs: String, ) { - val args = ArrayList() - args.add(target.protoPathArg()) - args.add(target.outArg("/".toPath() / testDir)) + val args = mutableListOf( + target.protoPathArg(), + target.outArg("/".toPath() / testDir), + ) Collections.addAll(args, *extraArgs) Collections.addAll(args, *sources) logger = StringWireLogger() - val fs = fileSystem - val compiler = WireCompiler.forArgs(fs, logger!!, *args.toTypedArray()) + val compiler = WireCompiler.forArgs(fileSystem, logger!!, *args.toTypedArray()) compiler.compile() }