Skip to content

Commit

Permalink
fix(ClientUtilsConventions): apply dependency locking at time of plug…
Browse files Browse the repository at this point in the history
…in initialization (#577)

* fix(ClientUtilsConventions): apply before project evaluation

* lint is so aggressive

* align info messages and tests
  • Loading branch information
Cole Turner authored Jun 26, 2023
1 parent fef573a commit a66224a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,32 @@ object ClientUtilsConventions {

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

fun getDependencyString(version: String? = null): String {
if (version != null) {
return "$CLIENT_UTILS_ARTIFACT_GROUP:$CLIENT_UTILS_ARTIFACT_NAME:$version"
}

return "$CLIENT_UTILS_ARTIFACT_GROUP:$CLIENT_UTILS_ARTIFACT_NAME"
}

fun apply(
project: Project,
optionalCodeUtilsVersion: Optional<String> = Optional.empty(),
optionalCodeClientDependencyScope: Optional<String> = Optional.empty()
) {
clientCoreArtifact(optionalCodeUtilsVersion).ifPresent { dependencyString ->
val dependencyLockString = dependencyString.split(":").take(2).joinToString(":")
val dependencyLockString = getDependencyString()

val dependencyConfiguration = optionalCodeClientDependencyScope.orElse(GRADLE_CLASSPATH_CONFIGURATION)
val configurationDependencies = project.configurations.getByName(dependencyConfiguration).dependencies
configurationDependencies.add(project.dependencies.create(dependencyString))
logger.info("DGS CodeGen added [{}] to the {} dependencies.", dependencyString, dependencyConfiguration)

project.dependencyLocking.ignoredDependencies.add(dependencyLockString)
logger.info("DGS CodeGen added [{}] to the ignoredDependencies.", dependencyLockString, dependencyConfiguration)
logger.info("DGS CodeGen added dependency [{}] to {}.", dependencyString, dependencyConfiguration)

project.plugins.withId(CLIENT_UTILS_NEBULA_LOCK_ID) {
val extension = project.extensions.getByType(DependencyLockExtension::class.java)
if (extension != null) {
extension.skippedDependencies.add(dependencyLockString)
logger.info("DGS CodeGen added [{}] to the skippedDependencies.", dependencyLockString, dependencyConfiguration)
logger.info("DGS CodeGen added skipped dependency [{}].", dependencyLockString)
}
}
}
Expand All @@ -76,6 +81,6 @@ object ClientUtilsConventions {

private fun clientCoreArtifact(optionalVersion: Optional<String>): Optional<String> {
val version = if (optionalVersion.isPresent) optionalVersion else pluginMetaInfVersion
return version.map { "$CLIENT_UTILS_ARTIFACT_GROUP:$CLIENT_UTILS_ARTIFACT_NAME:$it" }
return version.map(::getDependencyString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CodegenPlugin : Plugin<Project> {
}

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

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

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

addDependencyLock(project, codegenExtension)

project.afterEvaluate { p ->
if (extensions.clientCoreConventionsEnabled.getOrElse(true)) {
if (codegenExtension.clientCoreConventionsEnabled.getOrElse(true)) {
logger.info("Applying CodegenPlugin Client Utils conventions.")
ClientUtilsConventions.apply(
p,
Optional.ofNullable(extensions.clientCoreVersion.orNull),
Optional.ofNullable(extensions.clientCoreScope.orNull)
Optional.ofNullable(codegenExtension.clientCoreVersion.orNull),
Optional.ofNullable(codegenExtension.clientCoreScope.orNull)
)
}
}
}

private fun addDependencyLock(project: Project, codegenExtension: CodegenPluginExtension) {
val dependencyLockString = ClientUtilsConventions.getDependencyString()
try {
if (codegenExtension.clientCoreConventionsEnabled.getOrElse(true)) {
project.dependencyLocking.ignoredDependencies.add(dependencyLockString)
logger.info(
"DGS CodeGen added ignored dependency [{}].",
dependencyLockString
)
}
} catch (e: Exception) {
// do nothing, this is supplemental and seems to work in certain contexts but not in others
logger.info(
"Failed to add DGS CodeGen to ignoredDependencies because: {}",
dependencyLockString,
e
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CodegenGradlePluginClientUtilsConventionsTest {
private val inferredVersion = ClientUtilsConventions.pluginMetaInfVersion

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

assertThat(result.output)
.doesNotContain("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
assertThat(result.output)
.doesNotContain("com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
.doesNotContain("DGS CodeGen added dependency")
}

@Test
Expand Down Expand Up @@ -91,7 +89,7 @@ dependencies { }
val result = runner.build()
// then we assert that the dependency was resolved to the proper version
assertThat(result.output)
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}")
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}")
assertThat(result.output)
.contains("- com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}")
}
Expand Down Expand Up @@ -128,7 +126,7 @@ dependencies { }
val result = runner.build()
// then we assert that the dependency was resolved to the higher version.
assertThat(result.output)
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}] to the $configuration dependencies.")
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()}] to $configuration.")
}

@Test
Expand Down Expand Up @@ -163,7 +161,7 @@ codegen {
val result = runner.build()
// then we assert that the dependency was resolved to the higher version.
assertThat(result.output)
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:$higherVersion")
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:$higherVersion")
assertThat(result.output)
.contains("- com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:$higherVersion FAILED")
}
Expand Down Expand Up @@ -198,7 +196,7 @@ dependencies {
val result = runner.build()
// then we assert that the dependency was resolved to the higher version.
assertThat(result.output)
.contains("DGS CodeGen added [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
.contains("DGS CodeGen added dependency [com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core")
assertThat(result.output)
.contains("- com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-shared-core:${inferredVersion.get()} -> $higherVersion FAILED")
}
Expand Down

0 comments on commit a66224a

Please sign in to comment.