Skip to content

Commit ba29bd2

Browse files
authored
Merge pull request #3302 from staktrace/kartikaya.lazy
Avoid eagerly resolving the configuration
2 parents cb4cfd7 + a476264 commit ba29bd2

File tree

7 files changed

+63
-1
lines changed

7 files changed

+63
-1
lines changed

wire-gradle-plugin/src/main/kotlin/com/squareup/wire/gradle/WirePlugin.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,18 @@ class WirePlugin : Plugin<Project> {
186186
task.group = GROUP
187187
task.description = "Generate protobuf implementation for ${source.name}"
188188

189+
var addedSourcesDependencies = false
189190
// Flatten all the input files here. Changes to any of them will cause the task to re-run.
190191
for (rootSet in protoSourceProtoRootSets) {
191192
task.source(rootSet.configuration)
193+
if (!rootSet.isEmpty) {
194+
// Use the isEmpty flag to avoid resolving the configuration eagerly
195+
addedSourcesDependencies = true
196+
}
192197
}
193198
// We only want to add ProtoPath sources if we have other sources already. The WireTask
194199
// would otherwise run even through we have no sources.
195-
if (!task.source.isEmpty) {
200+
if (addedSourcesDependencies) {
196201
for (rootSet in protoPathProtoRootSets) {
197202
task.source(rootSet.configuration)
198203
}

wire-gradle-plugin/src/test/kotlin/com/squareup/wire/gradle/WirePluginTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,16 @@ class WirePluginTest {
13981398
assertThat(File(outputRoot, "Period.kt")).exists()
13991399
}
14001400

1401+
@Test
1402+
fun lazyConfigurationResolution() {
1403+
val fixtureRoot = File("src/test/projects/lazy-configuration-resolution")
1404+
1405+
val result = gradleRunner.runFixture(fixtureRoot) {
1406+
withArguments("wire-project:generateProtos", "--stacktrace", "--info").build()
1407+
}
1408+
assertThat(result.task(":wire-project:generateProtos")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
1409+
}
1410+
14011411
private fun GradleRunner.runFixture(
14021412
root: File,
14031413
action: GradleRunner.() -> BuildResult,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id("org.jetbrains.kotlin.jvm") version "1.9.22"
3+
}
4+
5+
// The below is redundant since the kotlin plugin above does it too
6+
// but some future version of the kotlin plugin may not, and we still
7+
// want to capture this explicitly. The projectsEvaluated call fails
8+
// if a gradle mutation guard is in place, which means it fails if
9+
// wire-project forces this project to be configured while in the middle
10+
// of applying the wire plugin (i.e. because the wire plugin evaluates
11+
// the project dependency). Wire should not evaluate the project
12+
// dependency eagerly.
13+
gradle.projectsEvaluated {
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.gradle.configureondemand=true
2+
VERSION_NAME=5.4.0-SNAPSHOT
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include ':wire-project'
2+
include ':dep-project'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id("org.jetbrains.kotlin.jvm") version "1.9.22"
3+
id("com.squareup.wire")
4+
}
5+
6+
dependencies {
7+
protoSource(project(":dep-project"))
8+
}
9+
10+
wire {
11+
kotlin {
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto2";
2+
3+
package squareup.geology;
4+
5+
option java_package = "com.squareup.geology";
6+
7+
enum Period {
8+
/** 145.5 million years ago — 66.0 million years ago. */
9+
CRETACEOUS = 1;
10+
11+
/** 201.3 million years ago — 145.0 million years ago. */
12+
JURASSIC = 2;
13+
14+
/** 252.17 million years ago — 201.3 million years ago. */
15+
TRIASSIC = 3;
16+
}

0 commit comments

Comments
 (0)