Skip to content

Commit ebc75fc

Browse files
committed
Lazily evaluate dockerRun configuration
Authored-by: Leonhardt Koepsell <[email protected]>
1 parent c3ce1b1 commit ebc75fc

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixed
1515

1616
- Simplify the path evaluation for docker
17+
- Lazily evaluate dockerRun configuration
1718

1819
## [0.1.0] - 2024-11-07
1920

src/main/kotlin/dev/codebandits/container/gradle/tasks/ContainerRunTask.kt

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,34 @@ public abstract class ContainerRunTask : ContainerExecTask() {
1818
}
1919

2020
public fun dockerRun(configure: DockerRunSpec.() -> Unit) {
21-
val spec = DockerRunSpec(project.objects).apply(configure)
22-
val image = spec.image.get()
23-
val options = mutableListOf<String>()
24-
val user = spec.user.orNull
25-
if (user != null) {
26-
options.addAll(listOf("--user", user))
27-
}
28-
if (spec.privileged.get()) {
29-
options.add("--privileged")
30-
}
31-
if (spec.autoRemove.get()) {
32-
options.add("--rm")
33-
}
34-
spec.volumes.get().forEach { volume ->
35-
options.addAll(listOf("--volume", volume))
36-
}
37-
val entrypoint = spec.entrypoint.orNull
38-
if (entrypoint != null) {
39-
options.addAll(listOf("--entrypoint", entrypoint))
40-
}
41-
val workdir = spec.workdir.orNull
42-
if (workdir != null) {
43-
options.addAll(listOf("--workdir", workdir))
44-
}
45-
val dockerArgs = arrayOf("run", *options.toTypedArray(), image, *spec.args.get())
4621
actionSteps.add(
4722
ExecutionStep(
4823
execAction = {
24+
val spec = DockerRunSpec(project.objects).apply(configure)
25+
val image = spec.image.get()
26+
val options = mutableListOf<String>()
27+
val user = spec.user.orNull
28+
if (user != null) {
29+
options.addAll(listOf("--user", user))
30+
}
31+
if (spec.privileged.get()) {
32+
options.add("--privileged")
33+
}
34+
if (spec.autoRemove.get()) {
35+
options.add("--rm")
36+
}
37+
spec.volumes.get().forEach { volume ->
38+
options.addAll(listOf("--volume", volume))
39+
}
40+
val entrypoint = spec.entrypoint.orNull
41+
if (entrypoint != null) {
42+
options.addAll(listOf("--entrypoint", entrypoint))
43+
}
44+
val workdir = spec.workdir.orNull
45+
if (workdir != null) {
46+
options.addAll(listOf("--workdir", workdir))
47+
}
48+
val dockerArgs = arrayOf("run", *options.toTypedArray(), image, *spec.args.get())
4949
executable = "docker"
5050
args(*dockerArgs)
5151
val dockerHost = spec.dockerHost.orNull
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.codebandits.container.gradle
2+
3+
import dev.codebandits.container.gradle.helpers.appendLine
4+
import org.gradle.testkit.runner.GradleRunner
5+
import org.gradle.testkit.runner.TaskOutcome
6+
import org.junit.jupiter.api.Test
7+
import strikt.api.expectThat
8+
import strikt.assertions.isEqualTo
9+
import strikt.assertions.isNotNull
10+
11+
class DockerRunDeferredConfigurationEvaluationTest : GradleProjectTest() {
12+
13+
@Test
14+
fun `dockerRun configuration evaluation is deferred`() {
15+
buildGradleKtsFile.appendLine(
16+
"""
17+
import dev.codebandits.container.gradle.tasks.ContainerRunTask
18+
19+
plugins {
20+
id("dev.codebandits.container")
21+
}
22+
23+
tasks {
24+
register<ContainerRunTask>("notReady") {
25+
val imageProvider = project.provider<String> { TODO() }
26+
dockerRun {
27+
image = imageProvider.get()
28+
}
29+
}
30+
}
31+
""".trimIndent()
32+
)
33+
34+
val result = GradleRunner.create()
35+
.withPluginClasspath()
36+
.withProjectDir(projectDirectory.toFile())
37+
.withArguments("tasks")
38+
.build()
39+
40+
expectThat(result).get { task(":tasks") }.isNotNull().get { outcome }.isEqualTo(TaskOutcome.SUCCESS)
41+
}
42+
}

0 commit comments

Comments
 (0)