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

all target can be non-exclusive via CLI #3107

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
2 changes: 2 additions & 0 deletions wire-compiler/api/wire-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions wire-compiler/src/main/java/com/squareup/wire/WireCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -149,6 +151,7 @@ class WireCompiler internal constructor(
compact = emitCompact,
emitDeclaredOptions = emitDeclaredOptions,
emitAppliedOptions = emitAppliedOptions,
exclusive = javaExclusive,
)
}
if (kotlinOut != null) {
Expand All @@ -172,6 +175,7 @@ class WireCompiler internal constructor(
if (swiftOut != null) {
targets += SwiftTarget(
outDirectory = swiftOut,
exclusive = swiftExclusive,
)
}
if (customOut != null || schemaHandlerFactoryClass != null) {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
97 changes: 71 additions & 26 deletions wire-compiler/src/test/java/com/squareup/wire/WireCompilerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ class WireCompilerTest {
fun testPersonDryRun() {
val sources = arrayOf("person.proto")

val args = ArrayList<String>()
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<String>()
Expand All @@ -105,8 +106,7 @@ class WireCompilerTest {
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>())
val compiler = WireCompiler.forArgs(fileSystem, logger, *args.toTypedArray<String>())
compiler.compile()

// We assert nothing has been generated.
Expand All @@ -119,13 +119,56 @@ class WireCompilerTest {
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")
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<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 compiler = WireCompiler.forArgs(fileSystem, 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 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<String>()
Expand All @@ -139,8 +182,7 @@ class WireCompilerTest {
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>())
val compiler = WireCompiler.forArgs(fileSystem, logger, *args.toTypedArray<String>())
compiler.compile()

// We assert nothing has been generated.
Expand All @@ -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)",
)
}
Expand Down Expand Up @@ -495,9 +538,10 @@ class WireCompilerTest {
@Test
fun sourceInJar() {
val sources = arrayOf("squareup/geology/period.proto", "squareup/dinosaurs/dinosaur.proto")
val args = ArrayList<String>()
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())
Expand All @@ -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<String>()
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())
Expand Down Expand Up @@ -747,15 +792,15 @@ class WireCompilerTest {
sources: Array<String>,
vararg extraArgs: String,
) {
val args = ArrayList<String>()
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()
}

Expand Down