Skip to content

Commit a857067

Browse files
Encapsulate forcing all artifacts of a dependency
1 parent 1f6a081 commit a857067

3 files changed

Lines changed: 56 additions & 25 deletions

File tree

buildSrc/src/main/kotlin/io/spine/dependency/Dependency.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
package io.spine.dependency
2828

29+
import org.gradle.api.Project
30+
import org.gradle.api.artifacts.Configuration
31+
import org.gradle.api.artifacts.ResolutionStrategy
32+
2933
/**
3034
* A dependency is a software component we use in a project.
3135
*
@@ -63,6 +67,19 @@ abstract class Dependency {
6367
fun artifact(module: String): String = artifacts[module] ?: error(
6468
"The dependency `${this::class.simpleName}` does not declare a module `$module`."
6569
)
70+
71+
/**
72+
* Forces all artifacts of this dependency using the given resolution strategy.
73+
*
74+
* @param project The project in which the artifacts are forced. Used for logging.
75+
* @param cfg The configuration for which the artifacts are forced. Used for logging.
76+
* @param rs The resolution strategy which forces the artifacts.
77+
*/
78+
fun forceArtifacts(project: Project, cfg: Configuration, rs: ResolutionStrategy) {
79+
artifacts.values.forEach {
80+
rs.forceWithLogging(project, cfg, it)
81+
}
82+
}
6683
}
6784

6885
/**
@@ -80,3 +97,30 @@ abstract class DependencyWithBom : Dependency() {
8097
*/
8198
abstract val bom: String
8299
}
100+
101+
/**
102+
* Logs the result of the function using the project logger at `INFO` level.
103+
*/
104+
fun Project.log(message: () -> String) {
105+
if (logger.isInfoEnabled) {
106+
logger.info(message.invoke())
107+
}
108+
}
109+
110+
/**
111+
* Returns the suffix of diagnostic messages for this configuration in the given project.
112+
*/
113+
fun Configuration.diagSuffix(project: Project): String =
114+
"the configuration `$name` in the project: `${project.path}`."
115+
116+
117+
private fun ResolutionStrategy.forceWithLogging(
118+
project: Project,
119+
configuration: Configuration,
120+
artifact: String
121+
) {
122+
force(artifact)
123+
project.log { "Forced the version of `$artifact` in " + configuration.diagSuffix(project) }
124+
}
125+
126+

buildSrc/src/main/kotlin/io/spine/dependency/boms/BomsPlugin.kt

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
package io.spine.dependency.boms
2828

2929
import io.gitlab.arturbosch.detekt.getSupportedKotlinVersion
30+
import io.spine.dependency.log
31+
import io.spine.dependency.diagSuffix
3032
import io.spine.dependency.DependencyWithBom
3133
import io.spine.dependency.kotlinx.Coroutines
3234
import io.spine.dependency.lib.Kotlin
@@ -103,9 +105,6 @@ class BomsPlugin : Plugin<Project> {
103105
}
104106
}
105107

106-
private fun Configuration.diagSuffix(project: Project): String =
107-
"the configuration `$name` in the project: `${project.path}`."
108-
109108
private fun Configuration.applyBoms(project: Project, deps: List<DependencyWithBom>) {
110109
deps.forEach { dep ->
111110
withDependencies {
@@ -134,12 +133,6 @@ private fun ConfigurationContainer.selectKotlinCompilerForDetekt() =
134133
}
135134
}
136135

137-
private fun Project.log(message: () -> String) {
138-
if (logger.isInfoEnabled) {
139-
logger.info(message.invoke())
140-
}
141-
}
142-
143136
private fun isCompilationConfig(name: String) =
144137
name.contains("compile", ignoreCase = true) &&
145138
// `compileProtoPath` or `compileTestProtoPath`.
@@ -177,20 +170,14 @@ private fun supportsBom(name: String) =
177170
private fun Project.forceArtifacts() =
178171
configurations.all {
179172
resolutionStrategy {
180-
fun forceWithLogging(artifact: String) {
181-
force(artifact)
182-
log { "Forced the version of `$artifact` in " + this@all.diagSuffix(project) }
183-
}
184-
185-
fun forceAll(artifacts: Map<String, String>) = artifacts.values.forEach { artifact ->
186-
forceWithLogging(artifact)
187-
}
188-
189173
if (!isDetekt) {
190-
forceAll(Kotlin.artifacts)
191-
forceAll(Kotlin.StdLib.artifacts)
192-
forceAll(Coroutines.artifacts)
193-
forceAll(JUnit.Jupiter.artifacts) /*
174+
val rs = this@resolutionStrategy
175+
val project = this@forceArtifacts
176+
val cfg = this@all
177+
Kotlin.forceArtifacts(project, cfg, rs)
178+
Kotlin.StdLib.forceArtifacts(project, cfg, rs)
179+
Coroutines.forceArtifacts(project, cfg, rs)
180+
JUnit.Jupiter.forceArtifacts(project, cfg, rs) /*
194181
for configurations like `testFixturesCompileProtoPath`.
195182
*/
196183
}

tests/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ buildscript {
7979
val logging = io.spine.dependency.local.Logging
8080
all {
8181
resolutionStrategy {
82-
io.spine.dependency.lib.Kotlin.StdLib.artifacts.values.forEach {
83-
force(it)
84-
}
82+
val cfg = this@all
83+
val rs = this@resolutionStrategy
84+
io.spine.dependency.lib.Kotlin.StdLib.forceArtifacts(project, cfg, rs)
8585
force(
8686
"io.spine:protodata:${protoData.version}",
8787
io.spine.dependency.local.Reflect.lib,

0 commit comments

Comments
 (0)