Skip to content

Commit

Permalink
Allow plugin to be included in sub-folders of a Gradle Project.
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelgbanks committed Mar 30, 2021
1 parent 01feab9 commit 0854dd6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
30 changes: 15 additions & 15 deletions src/main/kotlin/tasks/DockerBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ open class DockerBuild : DefaultTask() {

options.run {
// Get project properties used to set defaults.
val buildPlatforms: Set<String> by project.rootProject.extra
val cacheFromEnabled: Boolean by project.rootProject.extra
val cacheToEnabled: Boolean by project.rootProject.extra
val cacheFromRepositories: Set<String> by project.rootProject.extra
val cacheToRepositories: Set<String> by project.rootProject.extra
val cacheToMode: String by project.rootProject.extra
val noBuildCache: Boolean by project.rootProject.extra
val isDockerBuild: Boolean by project.rootProject.extra
val buildPlatforms: Set<String> by project.extra
val cacheFromEnabled: Boolean by project.extra
val cacheToEnabled: Boolean by project.extra
val cacheFromRepositories: Set<String> by project.extra
val cacheToRepositories: Set<String> by project.extra
val cacheToMode: String by project.extra
val noBuildCache: Boolean by project.extra
val isDockerBuild: Boolean by project.extra

// Assume docker file is in the project directory.
dockerFile.convention(project.layout.projectDirectory.file("Dockerfile"))
Expand Down Expand Up @@ -249,7 +249,7 @@ open class DockerBuild : DefaultTask() {
}

// Get list of all DockerBuild tasks with the given name.
private fun dockerBuildTasks(name: String) = project.rootProject.allprojects
private fun dockerBuildTasks(name: String) = project.allprojects
.filter { it.projectDir.resolve("Dockerfile").exists() }
.map { project ->
project.name to project.tasks.named<DockerBuild>(name)
Expand All @@ -258,7 +258,7 @@ open class DockerBuild : DefaultTask() {

// Checks if all images denoted by the given tag(s) exists locally.
private fun imagesExist(): Boolean {
val dockerClient: DockerClient by project.rootProject.extra
val dockerClient: DockerClient by project.extra
return options.tags.get().all { tag ->
try {
dockerClient.inspectImageCmd(tag).exec()
Expand Down Expand Up @@ -287,8 +287,8 @@ open class DockerBuild : DefaultTask() {
// "push and load may not be set together at the moment", so we must manually pull after building.
// Only applies to when driver is not set to `docker`.
private fun pull() {
val isDockerBuild: Boolean by project.rootProject.extra
val isLocalRepository: Boolean by project.rootProject.extra
val isDockerBuild: Boolean by project.extra
val isLocalRepository: Boolean by project.extra
if (!isDockerBuild) {
project.exec {
workingDir = context.dir
Expand All @@ -309,8 +309,8 @@ open class DockerBuild : DefaultTask() {
// Due to https://github.com/docker/buildx/issues/420 we cannot rely on the imageId file to be populated
// correctly so we take matters into our own hands.
private fun updateImageFile() {
val isDockerBuild: Boolean by project.rootProject.extra
val dockerClient: DockerClient by project.rootProject.extra
val isDockerBuild: Boolean by project.extra
val dockerClient: DockerClient by project.extra
if (!isDockerBuild) {
dockerClient.inspectImageCmd(options.tags.get().first()).exec().run {
options.imageIdFile.get().asFile.writeText(id)
Expand All @@ -321,7 +321,7 @@ open class DockerBuild : DefaultTask() {
// We generate an approximate digest to prevent rebuilding downstream images as this will be used as an input to
// those images.
private fun updateDigest() {
val dockerClient: DockerClient by project.rootProject.extra
val dockerClient: DockerClient by project.extra
dockerClient.inspectImageCmd(options.tags.get().first()).exec().run {
digest.get().asFile.writeText(jacksonObjectMapper().writeValueAsString(ApproximateDigest(config, rootFS)))
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/tasks/DockerCompose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.mapProperty
import org.gradle.kotlin.dsl.provideDelegate
import utils.isDockerProject
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.OutputStream
Expand Down Expand Up @@ -151,7 +152,7 @@ abstract class DockerCompose : DockerClient() {
fun pull() = dockerCompose.services.keys.mapNotNull { name ->
// Find services that do not match any projects and pull them as they must refer to an external image.
// Other images will be provided by dependency on the image digests.
if (project.rootProject.findProject(":$name") == null) name else null
if (project.allprojects.none { it.isDockerProject && it.name == name }) name else null
}.let { services ->
if (services.isNotEmpty()) {
invoke("pull", *services.toTypedArray())
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/utils/ProjectExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.gradle.kotlin.dsl.provideDelegate

// Computes a set of image tags for the given repository.
fun Project.imageTags(repository: String): Set<String> {
val tags: Set<String> by project.rootProject.extra
val tags: Set<String> by project.extra
return tags.map { "$repository/$name:$it" }.toSet()
}

Expand Down

0 comments on commit 0854dd6

Please sign in to comment.