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

build: track tool versions as input of generateProto task #101

Merged
merged 1 commit into from
Feb 3, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 0.3.3

* Fix 'generateProto' caching issue
* JPMS - add patching rule: org.apache.logging.log4j:log4j-api (for compile time checks)
* JPMS - add patching rule: org.apache.logging.log4j:log4j-api (for compile time checks)

Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/org.hiero.gradle.feature.protobuf.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ protobuf {
// Add GRPC plugin as we need to generate GRPC services
plugins { register("grpc") { artifact = "io.grpc:protoc-gen-grpc-java" } }
generateProtoTasks {
all().configureEach { plugins.register("grpc") { option("@generated=omit") } }
all().configureEach {
plugins.register("grpc") { option("@generated=omit") }
// Track all tools as input to react if version changes for the tools
inputs.files(configurations["protobufToolsLocator_protoc"])
inputs.files(configurations["protobufToolsLocator_grpc"])
}
}
}

Expand Down
85 changes: 85 additions & 0 deletions src/test/kotlin/org/hiero/gradle/test/ProtobufTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: Apache-2.0
package org.hiero.gradle.test

import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.TaskOutcome
import org.hiero.gradle.test.fixtures.GradleProject
import org.junit.jupiter.api.Test

class ProtobufTest {

Check warning on line 9 in src/test/kotlin/org/hiero/gradle/test/ProtobufTest.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/kotlin/org/hiero/gradle/test/ProtobufTest.kt#L9

ProtobufTest is missing required documentation.

@Test
fun `task re-runs if protobuf plugin version changes`() {

Check warning on line 12 in src/test/kotlin/org/hiero/gradle/test/ProtobufTest.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/kotlin/org/hiero/gradle/test/ProtobufTest.kt#L12

The function task re-runs if protobuf plugin version changes is missing documentation.
val testProto =
"""
syntax = "proto3";
package org.hiero.product.module.a;
message Test {
bytes test_id = 1;
}
"""
.trimIndent()
val moduleInfo =
"""
module org.hiero.product.module.a {
requires com.google.protobuf;
}
"""
.trimIndent()
val aggregation =
"""
plugins {
id("java")
id("org.hiero.gradle.base.lifecycle")
}
dependencies { implementation(project(":module-a")) }
"""
.trimIndent()

val push = GradleProject().withMinimalStructure()
val pull = GradleProject().withMinimalStructure()
push.aggregationBuildFile(aggregation)
pull.aggregationBuildFile(aggregation)
push.settingsFile.appendText(
"""buildCache.local.directory = File("${push.file("build-cache").absolutePath}")"""
)
pull.settingsFile.appendText(
"""buildCache.local.directory = File("${push.file("build-cache").absolutePath}")"""
)
push.moduleBuildFile("""plugins { id("org.hiero.gradle.feature.protobuf") }""")
pull.moduleBuildFile("""plugins { id("org.hiero.gradle.feature.protobuf") }""")

push.moduleInfoFile(moduleInfo)
pull.moduleInfoFile(moduleInfo)

push.file("product/module-a/src/main/proto/test.proto", testProto)
pull.file("product/module-a/src/main/proto/test.proto", testProto)

push.dependencyVersionsFile(dependencyVersions("4.29.0"))
pull.dependencyVersionsFile(dependencyVersions("4.29.3"))

val pushResult = push.run("assemble --build-cache")
val pullResult = pull.run("assemble --build-cache")

// make sure second run is not FROM-CACHE
assertThat(pushResult.task(":module-a:generateProto")?.outcome)
.isEqualTo(TaskOutcome.SUCCESS)
assertThat(pushResult.task(":module-a:compileJava")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(pullResult.task(":module-a:generateProto")?.outcome)
.isEqualTo(TaskOutcome.SUCCESS)
assertThat(pullResult.task(":module-a:compileJava")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

private fun dependencyVersions(protocVersion: String) =
"""
plugins {
id("org.hiero.gradle.base.lifecycle")
id("org.hiero.gradle.base.jpms-modules")
}
dependencies.constraints {
api("com.google.protobuf:protobuf-java:4.29.0")
api("com.google.protobuf:protoc:$protocVersion")
api("io.grpc:protoc-gen-grpc-java:1.69.0")
}"""
.trimIndent()
}