Skip to content

Commit

Permalink
opaque configuration via the Gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Oct 10, 2023
1 parent 27dfd3a commit d4066ac
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ open class WireExtension(project: Project) {
internal val roots = mutableSetOf<String>()
internal val prunes = mutableSetOf<String>()
internal val moves = mutableListOf<Move>()
internal val opaques = mutableSetOf<String>()
internal val eventListenerFactories = mutableSetOf<EventListener.Factory>()
internal val eventListenerFactoryClasses = mutableSetOf<String>()
internal var onlyVersion: String? = null
Expand Down Expand Up @@ -291,12 +292,24 @@ open class WireExtension(project: Project) {
outputs += customOutput
}

@Input
@Optional
fun moves() = moves.toList()

fun move(action: Action<Move>) {
val move = objectFactory.newInstance(Move::class.java)
action.execute(move)
moves += move
}

@Input
@Optional
fun opaques() = opaques.toSet()

fun opaque(vararg opaques: String) {
this.opaques.addAll(opaques)
}

// TODO(Benoit) See how we can make this class better, it's a mess and doesn't scale nicely.
open class ProtoRootSet {
val srcDirs = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class WirePlugin : Plugin<Project> {
task.roots.set(extension.roots.toList())
task.prunes.set(extension.prunes.toList())
task.moves.set(extension.moves.toList())
task.opaques.set(extension.opaques.toList())
task.sinceVersion.set(extension.sinceVersion)
task.untilVersion.set(extension.untilVersion)
task.onlyVersion.set(extension.onlyVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.squareup.wire.VERSION
import com.squareup.wire.gradle.internal.GradleWireLogger
import com.squareup.wire.schema.EventListener
import com.squareup.wire.schema.Location
import com.squareup.wire.schema.ProtoType
import com.squareup.wire.schema.Target
import com.squareup.wire.schema.WireRun
import java.io.File
Expand Down Expand Up @@ -77,6 +78,9 @@ abstract class WireTask @Inject constructor(objects: ObjectFactory) : SourceTask
@get:Input
abstract val moves: ListProperty<Move>

@get:Input
abstract val opaques: ListProperty<String>

@get:Input
@get:Optional
abstract val sinceVersion: Property<String>
Expand Down Expand Up @@ -171,6 +175,7 @@ abstract class WireTask @Inject constructor(objects: ObjectFactory) : SourceTask
permitPackageCycles = permitPackageCycles.get(),
rejectUnusedRootsOrPrunes = rejectUnusedRootsOrPrunes.get(),
eventListeners = eventListenerFactories.get().map(EventListener.Factory::create),
opaqueTypes = opaques.get(),
)

val buildDir = buildDirProperty.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,25 @@ class WirePluginTest {
assertThat(geologyProto).contains("enum Period {")
}

@Test
fun opaqueMessage() {
val fixtureRoot = File("src/test/projects/opaque-message")

val result = gradleRunner.runFixture(fixtureRoot) { build() }

val task = result.task(":generateProtos")
assertThat(task).isNotNull
assertThat(result.output)
.contains("Writing cafe/cafe.proto")

val outputRoot = File(fixtureRoot, "build/generated/source/wire")

val cafeProto = File(outputRoot, "cafe/cafe.proto").readText()
assertThat(cafeProto)
.contains("repeated bytes shots")
.doesNotContain("repeated EspressoShot shots")
}

@Test
fun emitJavaOptions() {
val fixtureRoot = File("src/test/projects/emit-java-options")
Expand Down
11 changes: 11 additions & 0 deletions wire-gradle-plugin/src/test/projects/opaque-message/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id 'application'
id 'org.jetbrains.kotlin.jvm'
id 'com.squareup.wire'
}

wire {
opaque("cafe.EspressoShot")

proto {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto2";

package cafe;

message CafeDrink {
optional int32 size_ounces = 1;
repeated EspressoShot shots = 2;
}

message EspressoShot {
optional Roast roast = 1;
optional bool decaf = 2;
}

enum Roast {
MEDIUM = 1;
DARK = 2;
}

0 comments on commit d4066ac

Please sign in to comment.