Skip to content

Commit e5185ac

Browse files
Merge pull request #564 from Sineaggi/add-support-for-configuration-cache
Fix configuration cache issue
2 parents 127a6d2 + ef2b804 commit e5185ac

File tree

5 files changed

+236
-5
lines changed

5 files changed

+236
-5
lines changed

graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/CodegenPlugin.kt

+16-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,22 @@ class CodegenPlugin : Plugin<Project> {
5151
val outputDir = generateJavaTaskProvider.map(GenerateJavaTask::getOutputDir)
5252
mainSourceSet.java.srcDirs(project.files(outputDir).builtBy(generateJavaTaskProvider))
5353

54-
project.configurations.create("dgsCodegen")
55-
project.configurations.findByName("dgsCodegen")?.isCanBeResolved = true
54+
val dgsCodegen = project.configurations.create("dgsCodegen")
55+
dgsCodegen.isCanBeResolved = true
56+
generateJavaTaskProvider.configure {
57+
it.dependencies.addAll(
58+
dgsCodegen.incoming.dependencies.map { dependency ->
59+
InternalSimpleDependency(dependency.name, dependency.group)
60+
}
61+
)
62+
if (GradleVersion.current() >= GradleVersion.version("7.4")) {
63+
it.schemaJarArtifacts.addAll(dgsCodegen.incoming.artifacts.resolvedArtifacts.map { it.map { it.id } })
64+
it.schemaJarFiles.addAll(dgsCodegen.incoming.artifacts.resolvedArtifacts.map { it.map { it.file } })
65+
} else {
66+
it.schemaJarArtifacts.addAll(project.provider { dgsCodegen.incoming.artifacts.artifacts.map { it.id } })
67+
it.schemaJarFiles.addAll(project.provider { dgsCodegen.incoming.artifacts.artifacts.map { it.file } })
68+
}
69+
}
5670

5771
project.afterEvaluate { p ->
5872
if (extensions.clientCoreConventionsEnabled.getOrElse(true)) {

graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/GenerateJavaTask.kt

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.netflix.graphql.dgs.codegen.CodeGen
2222
import com.netflix.graphql.dgs.codegen.CodeGenConfig
2323
import com.netflix.graphql.dgs.codegen.Language
2424
import org.gradle.api.DefaultTask
25+
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
2526
import org.gradle.api.tasks.*
2627
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
2728
import java.io.File
@@ -148,13 +149,23 @@ open class GenerateJavaTask : DefaultTask() {
148149
@Input
149150
var includeClassImports = mutableMapOf<String, MutableMap<String, String>>()
150151

152+
@Input
153+
val dependencies = project.objects.setProperty(InternalSimpleDependency::class.java)
154+
155+
@Input
156+
val schemaJarArtifacts = project.objects.listProperty(ComponentArtifactIdentifier::class.java)
157+
158+
@InputFiles
159+
val schemaJarFiles = project.objects.listProperty(File::class.java)
160+
151161
@TaskAction
152162
fun generate() {
153163
val schemaJarFilesFromDependencies = emptyList<File>().toMutableList()
154-
val dgsCodegenConfig = project.configurations.findByName("dgsCodegen")
155-
dgsCodegenConfig?.incoming?.dependencies?.forEach { dependency ->
164+
dependencies.get().forEach { dependency ->
156165
logger.info("Found DgsCodegen Dependendency: ${dependency.name}")
157-
val found = dgsCodegenConfig.incoming.artifacts.resolvedArtifacts.get().find { it.id.componentIdentifier.displayName.contains(dependency.group + ":" + dependency.name) }
166+
data class InternalArtifactResult(val id: ComponentArtifactIdentifier, val file: File)
167+
val resolvedArtifacts = schemaJarArtifacts.get().zip(schemaJarFiles.get()) { id, file -> InternalArtifactResult(id, file) }
168+
val found = resolvedArtifacts.find { it.id.componentIdentifier.displayName.contains(dependency.group + ":" + dependency.name) }
158169
if (found != null) {
159170
logger.info("Found DgsCodegen Artifact: ${found.id.displayName}")
160171
schemaJarFilesFromDependencies.add(found.file)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
*
3+
* Copyright 2020 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package com.netflix.graphql.dgs.codegen.gradle
20+
21+
import java.io.Serializable
22+
23+
data class InternalSimpleDependency(val name: String, val group: String?) : Serializable

graphql-dgs-codegen-gradle/src/test/kotlin/com/netflix/graphql/dgs/CodegenGradlePluginCompatibilityTest.kt

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package com.netflix.graphql.dgs
2323
import org.assertj.core.api.Assertions.assertThat
2424
import org.gradle.testkit.runner.GradleRunner
2525
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
26+
import org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE
2627
import org.junit.jupiter.api.io.TempDir
2728
import org.junit.jupiter.params.ParameterizedTest
2829
import org.junit.jupiter.params.provider.ValueSource
@@ -106,6 +107,7 @@ class CodegenGradlePluginCompatibilityTest {
106107
.withPluginClasspath()
107108
.withDebug(true)
108109
.withArguments(
110+
"--configuration-cache",
109111
"--stacktrace",
110112
"--info",
111113
"generateJava",
@@ -114,6 +116,24 @@ class CodegenGradlePluginCompatibilityTest {
114116

115117
assertThat(result.task(":generateJava")).extracting { it?.outcome }.isEqualTo(SUCCESS)
116118
assertThat(result.task(":build")).extracting { it?.outcome }.isEqualTo(SUCCESS)
119+
assertThat(result.output).contains("Configuration cache entry stored.")
120+
121+
val rerunResult = GradleRunner.create()
122+
.withGradleVersion(gradleVersion)
123+
.withProjectDir(projectDir)
124+
.withPluginClasspath()
125+
.withDebug(true)
126+
.withArguments(
127+
"--configuration-cache",
128+
"--stacktrace",
129+
"--info",
130+
"generateJava",
131+
"build"
132+
).build()
133+
134+
assertThat(rerunResult.task(":generateJava")).extracting { it?.outcome }.isEqualTo(UP_TO_DATE)
135+
assertThat(rerunResult.task(":build")).extracting { it?.outcome }.isEqualTo(UP_TO_DATE)
136+
assertThat(rerunResult.output).contains("Configuration cache entry reused.")
117137
}
118138

119139
private fun prepareBuildGradleFile(content: String) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
*
3+
* Copyright 2020 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package com.netflix.graphql.dgs
20+
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.gradle.testkit.runner.GradleRunner
23+
import org.junit.jupiter.api.Test
24+
import org.junit.jupiter.api.io.TempDir
25+
import java.io.File
26+
import java.net.URI
27+
import java.nio.file.*
28+
import kotlin.io.path.createDirectories
29+
import kotlin.io.path.writeText
30+
31+
class CodegenGradlePluginConfigurationCacheTest {
32+
33+
@TempDir
34+
lateinit var projectDir: File
35+
36+
@Test
37+
fun `Test if configuration cache can be reused successfully`() {
38+
prepareBuildGraphQLSchema(
39+
"""
40+
type Query {
41+
test: String
42+
}
43+
""".trimMargin()
44+
)
45+
46+
prepareBuildGradleFile(
47+
"""
48+
plugins {
49+
id 'java'
50+
id 'com.netflix.dgs.codegen'
51+
}
52+
53+
repositories {
54+
mavenCentral()
55+
}
56+
sourceCompatibility = 1.8
57+
targetCompatibility = 1.8
58+
generateJava {
59+
packageName = 'com.netflix.testproject.graphql'
60+
generateClient = true
61+
}
62+
// Need to disable the core conventions since the artifacts are not yet visible.
63+
codegen.clientCoreConventionsEnabled = false
64+
""".trimMargin()
65+
)
66+
67+
val runner = GradleRunner.create()
68+
.withProjectDir(projectDir)
69+
.withPluginClasspath()
70+
.withDebug(true)
71+
.withArguments(
72+
"--stacktrace",
73+
"--info",
74+
"--configuration-cache",
75+
"generateJava",
76+
"build"
77+
)
78+
79+
runner.build() // First build, warm up cache
80+
val result = runner.build() // Second build, should use cache
81+
82+
assertThat(result.output).contains("Reusing configuration cache.")
83+
}
84+
85+
@Test
86+
fun `Test if configuration cache can be reused successfully with external schemas`() {
87+
prepareSchemaJar(
88+
"""
89+
type Query {
90+
test: String
91+
}
92+
""".trimMargin()
93+
)
94+
95+
prepareBuildGradleFile(
96+
"""
97+
plugins {
98+
id 'java'
99+
id 'com.netflix.dgs.codegen'
100+
}
101+
102+
repositories {
103+
mavenCentral()
104+
}
105+
106+
dependencies {
107+
// other dependencies
108+
dgsCodegen files("$projectDir/schema.jar")
109+
}
110+
111+
sourceCompatibility = 1.8
112+
targetCompatibility = 1.8
113+
generateJava {
114+
packageName = 'com.netflix.testproject.graphql'
115+
generateClient = true
116+
}
117+
// Need to disable the core conventions since the artifacts are not yet visible.
118+
codegen.clientCoreConventionsEnabled = false
119+
""".trimMargin()
120+
)
121+
122+
val runner = GradleRunner.create()
123+
.withProjectDir(projectDir)
124+
.withPluginClasspath()
125+
.withDebug(true)
126+
.withArguments(
127+
"--stacktrace",
128+
"--info",
129+
"--configuration-cache",
130+
"generateJava",
131+
"build"
132+
)
133+
134+
runner.build() // First build, warm up cache
135+
val result = runner.build() // Second build, should use cache
136+
137+
assertThat(result.output).contains("Reusing configuration cache.")
138+
}
139+
140+
private fun prepareSchemaJar(content: String) {
141+
val env = mapOf("create" to "true")
142+
val uri: URI = URI.create("jar:file:$projectDir/schema.jar")
143+
FileSystems.newFileSystem(uri, env).use { zipfs ->
144+
val pathInZipfile: Path = zipfs.getPath("/schema/schema.graphql")
145+
pathInZipfile.parent.createDirectories()
146+
pathInZipfile.writeText(content)
147+
}
148+
}
149+
150+
private fun prepareBuildGradleFile(content: String) {
151+
writeProjectFile("build.gradle", content)
152+
}
153+
154+
private fun prepareBuildGraphQLSchema(content: String) {
155+
writeProjectFile("src/main/resources/schema/schema.graphql", content)
156+
}
157+
158+
private fun writeProjectFile(relativePath: String, content: String) {
159+
val file = File(projectDir, relativePath)
160+
file.parentFile.mkdirs()
161+
file.writeText(content)
162+
}
163+
}

0 commit comments

Comments
 (0)