Skip to content

Commit a66224a

Browse files
author
Cole Turner
authored
fix(ClientUtilsConventions): apply dependency locking at time of plugin initialization (#577)
* fix(ClientUtilsConventions): apply before project evaluation * lint is so aggressive * align info messages and tests
1 parent fef573a commit a66224a

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,32 @@ object ClientUtilsConventions {
3434

3535
private val logger = Logging.getLogger(ClientUtilsConventions::class.java)
3636

37+
fun getDependencyString(version: String? = null): String {
38+
if (version != null) {
39+
return "$CLIENT_UTILS_ARTIFACT_GROUP:$CLIENT_UTILS_ARTIFACT_NAME:$version"
40+
}
41+
42+
return "$CLIENT_UTILS_ARTIFACT_GROUP:$CLIENT_UTILS_ARTIFACT_NAME"
43+
}
44+
3745
fun apply(
3846
project: Project,
3947
optionalCodeUtilsVersion: Optional<String> = Optional.empty(),
4048
optionalCodeClientDependencyScope: Optional<String> = Optional.empty()
4149
) {
4250
clientCoreArtifact(optionalCodeUtilsVersion).ifPresent { dependencyString ->
43-
val dependencyLockString = dependencyString.split(":").take(2).joinToString(":")
51+
val dependencyLockString = getDependencyString()
4452

4553
val dependencyConfiguration = optionalCodeClientDependencyScope.orElse(GRADLE_CLASSPATH_CONFIGURATION)
4654
val configurationDependencies = project.configurations.getByName(dependencyConfiguration).dependencies
4755
configurationDependencies.add(project.dependencies.create(dependencyString))
48-
logger.info("DGS CodeGen added [{}] to the {} dependencies.", dependencyString, dependencyConfiguration)
49-
50-
project.dependencyLocking.ignoredDependencies.add(dependencyLockString)
51-
logger.info("DGS CodeGen added [{}] to the ignoredDependencies.", dependencyLockString, dependencyConfiguration)
56+
logger.info("DGS CodeGen added dependency [{}] to {}.", dependencyString, dependencyConfiguration)
5257

5358
project.plugins.withId(CLIENT_UTILS_NEBULA_LOCK_ID) {
5459
val extension = project.extensions.getByType(DependencyLockExtension::class.java)
5560
if (extension != null) {
5661
extension.skippedDependencies.add(dependencyLockString)
57-
logger.info("DGS CodeGen added [{}] to the skippedDependencies.", dependencyLockString, dependencyConfiguration)
62+
logger.info("DGS CodeGen added skipped dependency [{}].", dependencyLockString)
5863
}
5964
}
6065
}
@@ -76,6 +81,6 @@ object ClientUtilsConventions {
7681

7782
private fun clientCoreArtifact(optionalVersion: Optional<String>): Optional<String> {
7883
val version = if (optionalVersion.isPresent) optionalVersion else pluginMetaInfVersion
79-
return version.map { "$CLIENT_UTILS_ARTIFACT_GROUP:$CLIENT_UTILS_ARTIFACT_NAME:$it" }
84+
return version.map(::getDependencyString)
8085
}
8186
}

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

+26-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CodegenPlugin : Plugin<Project> {
3636
}
3737

3838
override fun apply(project: Project) {
39-
val extensions = project.extensions.create("codegen", CodegenPluginExtension::class.java)
39+
val codegenExtension = project.extensions.create("codegen", CodegenPluginExtension::class.java)
4040

4141
project.plugins.apply(JavaPlugin::class.java)
4242

@@ -54,15 +54,37 @@ class CodegenPlugin : Plugin<Project> {
5454
project.configurations.create("dgsCodegen")
5555
project.configurations.findByName("dgsCodegen")?.isCanBeResolved = true
5656

57+
addDependencyLock(project, codegenExtension)
58+
5759
project.afterEvaluate { p ->
58-
if (extensions.clientCoreConventionsEnabled.getOrElse(true)) {
60+
if (codegenExtension.clientCoreConventionsEnabled.getOrElse(true)) {
5961
logger.info("Applying CodegenPlugin Client Utils conventions.")
6062
ClientUtilsConventions.apply(
6163
p,
62-
Optional.ofNullable(extensions.clientCoreVersion.orNull),
63-
Optional.ofNullable(extensions.clientCoreScope.orNull)
64+
Optional.ofNullable(codegenExtension.clientCoreVersion.orNull),
65+
Optional.ofNullable(codegenExtension.clientCoreScope.orNull)
66+
)
67+
}
68+
}
69+
}
70+
71+
private fun addDependencyLock(project: Project, codegenExtension: CodegenPluginExtension) {
72+
val dependencyLockString = ClientUtilsConventions.getDependencyString()
73+
try {
74+
if (codegenExtension.clientCoreConventionsEnabled.getOrElse(true)) {
75+
project.dependencyLocking.ignoredDependencies.add(dependencyLockString)
76+
logger.info(
77+
"DGS CodeGen added ignored dependency [{}].",
78+
dependencyLockString
6479
)
6580
}
81+
} catch (e: Exception) {
82+
// do nothing, this is supplemental and seems to work in certain contexts but not in others
83+
logger.info(
84+
"Failed to add DGS CodeGen to ignoredDependencies because: {}",
85+
dependencyLockString,
86+
e
87+
)
6688
}
6789
}
6890
}

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CodegenGradlePluginClientUtilsConventionsTest {
3333
private val inferredVersion = ClientUtilsConventions.pluginMetaInfVersion
3434

3535
@Test
36-
fun `If disabled, will not add the graphql-dgs-codegen-shared-core`() {
36+
fun `If disabled, will not add the graphql-dgs-codegen-shared-core to dependencies`() {
3737
prepareBuildGradleFile(
3838
"""
3939
plugins {
@@ -59,9 +59,7 @@ codegen.clientCoreConventionsEnabled = false
5959
val result = runner.build()
6060

6161
assertThat(result.output)
62-
.doesNotContain("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
63-
assertThat(result.output)
64-
.doesNotContain("com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
62+
.doesNotContain("DGS CodeGen added dependency")
6563
}
6664

6765
@Test
@@ -91,7 +89,7 @@ dependencies { }
9189
val result = runner.build()
9290
// then we assert that the dependency was resolved to the proper version
9391
assertThat(result.output)
94-
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}")
92+
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}")
9593
assertThat(result.output)
9694
.contains("- com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}")
9795
}
@@ -128,7 +126,7 @@ dependencies { }
128126
val result = runner.build()
129127
// then we assert that the dependency was resolved to the higher version.
130128
assertThat(result.output)
131-
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}] to the $configuration dependencies.")
129+
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}] to $configuration.")
132130
}
133131

134132
@Test
@@ -163,7 +161,7 @@ codegen {
163161
val result = runner.build()
164162
// then we assert that the dependency was resolved to the higher version.
165163
assertThat(result.output)
166-
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:$higherVersion")
164+
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:$higherVersion")
167165
assertThat(result.output)
168166
.contains("- com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:$higherVersion FAILED")
169167
}
@@ -198,7 +196,7 @@ dependencies {
198196
val result = runner.build()
199197
// then we assert that the dependency was resolved to the higher version.
200198
assertThat(result.output)
201-
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
199+
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
202200
assertThat(result.output)
203201
.contains("- com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()} -> $higherVersion FAILED")
204202
}

0 commit comments

Comments
 (0)