From 12439350ab0ca264e81c364aa71f36d7b0a084db Mon Sep 17 00:00:00 2001 From: Till Krullmann Date: Tue, 6 Oct 2020 12:18:39 +0200 Subject: [PATCH] Gradle compatibility: don't use ObjectFactory.fileCollection() in Gradle pre 5.3 --- .../plugins/helm/command/HelmValueOptions.kt | 31 ++++++++++++------- .../AbstractHelmInstallationCommandTask.kt | 9 +++++- .../plugins/helm/command/tasks/HelmLint.kt | 9 +++++- .../gradle/plugins/helm/dsl/Linting.kt | 16 ++++++++-- .../plugins/helm/release/dsl/HelmRelease.kt | 26 +++++++--------- .../helm/release/dsl/HelmReleaseTarget.kt | 6 ++-- .../plugins/helm/util/GradleVersions.kt | 1 + 7 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/HelmValueOptions.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/HelmValueOptions.kt index 62d6aba7..5fe1f26a 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/HelmValueOptions.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/HelmValueOptions.kt @@ -4,11 +4,14 @@ import groovy.lang.Closure import org.gradle.api.Project import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.FileCollection +import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.MapProperty import org.gradle.api.provider.Provider import org.gradle.api.resources.TextResource +import org.gradle.util.GradleVersion import org.slf4j.LoggerFactory +import org.unbrokendome.gradle.plugins.helm.util.GRADLE_VERSION_5_3 import org.unbrokendome.gradle.plugins.helm.util.mapProperty import java.util.concurrent.Callable @@ -72,11 +75,15 @@ internal data class HelmValueOptionsHolder( override val valueFiles: ConfigurableFileCollection ) : ConfigurableHelmValueOptions { - constructor(objects: ObjectFactory) - : this( + constructor(objects: ObjectFactory, layout: ProjectLayout) : this( values = objects.mapProperty(), fileValues = objects.mapProperty(), - valueFiles = objects.fileCollection() + valueFiles = if (GradleVersion.current() >= GRADLE_VERSION_5_3) { + objects.fileCollection() + } else { + @Suppress("DEPRECATION") + layout.configurableFiles() + } ) } @@ -120,17 +127,17 @@ internal object HelmValueOptionsApplier : HelmOptionsApplier { private fun buildFileValuesArg(options: HelmValueOptions): String = options.fileValues.getOrElse(emptyMap()) - .entries - .joinToString(separator = ",") { (key, value) -> - val valueRepresentation = when (val resolvedValue = resolveValue(value)) { - is FileCollection -> resolvedValue.singleFile - is TextResource -> resolvedValue.asFile() - else -> resolvedValue - } - - "$key=$valueRepresentation" + .entries + .joinToString(separator = ",") { (key, value) -> + val valueRepresentation = when (val resolvedValue = resolveValue(value)) { + is FileCollection -> resolvedValue.singleFile + is TextResource -> resolvedValue.asFile() + else -> resolvedValue } + "$key=$valueRepresentation" + } + private fun resolveValue(value: Any?): Any? = when (value) { is Provider<*> -> resolveValue(value.orNull) diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/AbstractHelmInstallationCommandTask.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/AbstractHelmInstallationCommandTask.kt index 3967802b..36d7c5b8 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/AbstractHelmInstallationCommandTask.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/AbstractHelmInstallationCommandTask.kt @@ -12,12 +12,14 @@ import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal import org.gradle.api.tasks.Optional +import org.gradle.util.GradleVersion import org.unbrokendome.gradle.plugins.helm.command.ConfigurableHelmInstallFromRepositoryOptions import org.unbrokendome.gradle.plugins.helm.command.ConfigurableHelmValueOptions import org.unbrokendome.gradle.plugins.helm.command.HelmExecProviderSupport import org.unbrokendome.gradle.plugins.helm.command.HelmInstallFromRepositoryOptionsApplier import org.unbrokendome.gradle.plugins.helm.command.HelmInstallationOptionsApplier import org.unbrokendome.gradle.plugins.helm.command.HelmValueOptionsApplier +import org.unbrokendome.gradle.plugins.helm.util.GRADLE_VERSION_5_3 import org.unbrokendome.gradle.plugins.helm.util.mapProperty import org.unbrokendome.gradle.plugins.helm.util.property import java.io.File @@ -206,7 +208,12 @@ abstract class AbstractHelmInstallationCommandTask : */ @get:InputFiles final override val valueFiles: ConfigurableFileCollection = - project.objects.fileCollection() + if (GradleVersion.current() >= GRADLE_VERSION_5_3) { + project.objects.fileCollection() + } else { + @Suppress("DEPRECATION") + project.layout.configurableFiles() + } /** diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/HelmLint.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/HelmLint.kt index 666962c6..8a66d95e 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/HelmLint.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/command/tasks/HelmLint.kt @@ -15,9 +15,11 @@ import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.TaskAction import org.gradle.util.GFileUtils +import org.gradle.util.GradleVersion import org.unbrokendome.gradle.plugins.helm.command.ConfigurableHelmValueOptions import org.unbrokendome.gradle.plugins.helm.command.HelmExecProviderSupport import org.unbrokendome.gradle.plugins.helm.command.HelmValueOptionsApplier +import org.unbrokendome.gradle.plugins.helm.util.GRADLE_VERSION_5_3 import org.unbrokendome.gradle.plugins.helm.util.ifPresent import org.unbrokendome.gradle.plugins.helm.util.mapProperty import org.unbrokendome.gradle.plugins.helm.util.property @@ -81,7 +83,12 @@ open class HelmLint : AbstractHelmCommandTask(), ConfigurableHelmValueOptions { */ @get:InputFiles final override val valueFiles: ConfigurableFileCollection = - project.objects.fileCollection() + if (GradleVersion.current() >= GRADLE_VERSION_5_3) { + project.objects.fileCollection() + } else { + @Suppress("DEPRECATION") + project.layout.configurableFiles() + } /** diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/dsl/Linting.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/dsl/Linting.kt index a41c2244..eeb17b96 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/dsl/Linting.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/dsl/Linting.kt @@ -2,10 +2,13 @@ package org.unbrokendome.gradle.plugins.helm.dsl import org.gradle.api.Project import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.MapProperty import org.gradle.api.provider.Property import org.gradle.api.provider.Provider +import org.gradle.util.GradleVersion +import org.unbrokendome.gradle.plugins.helm.util.GRADLE_VERSION_5_3 import org.unbrokendome.gradle.plugins.helm.util.mapProperty import org.unbrokendome.gradle.plugins.helm.util.property import javax.inject.Inject @@ -70,7 +73,10 @@ private interface LintingInternal : Linting, Hierarchical * Default implementation of [Linting]. */ private open class DefaultLinting -@Inject constructor(objectFactory: ObjectFactory) : LintingInternal { +@Inject constructor( + objectFactory: ObjectFactory, + projectLayout: ProjectLayout +) : LintingInternal { final override val enabled: Property = objectFactory.property() @@ -86,7 +92,13 @@ private open class DefaultLinting objectFactory.mapProperty() final override val valueFiles: ConfigurableFileCollection = - objectFactory.fileCollection() + if (GradleVersion.current() >= GRADLE_VERSION_5_3) { + objectFactory.fileCollection() + } else { + @Suppress("DEPRECATION") + projectLayout.configurableFiles() + } + final override fun setParent(parent: Linting) { enabled.set(parent.enabled) diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmRelease.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmRelease.kt index e8d4c819..aca1a4a9 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmRelease.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmRelease.kt @@ -8,7 +8,6 @@ import org.gradle.api.Task import org.gradle.api.file.Directory import org.gradle.api.file.FileCollection import org.gradle.api.file.RegularFile -import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.ExtensionAware import org.gradle.api.plugins.ExtensionContainer import org.gradle.api.provider.ListProperty @@ -434,24 +433,23 @@ internal interface HelmReleaseInternal { private abstract class AbstractHelmRelease( - objects: ObjectFactory, private val name: String, protected val project: Project ) : Named, HelmReleaseProperties, - ConfigurableHelmInstallFromRepositoryOptions by HelmInstallFromRepositoryOptionsHolder(objects), - ConfigurableHelmValueOptions by HelmValueOptionsHolder(objects) { + ConfigurableHelmInstallFromRepositoryOptions by HelmInstallFromRepositoryOptionsHolder(project.objects), + ConfigurableHelmValueOptions by HelmValueOptionsHolder(project.objects, project.layout) { override fun getName(): String = name final override val releaseName: Property = - objects.property() + project.objects.property() .convention(name) final override val chart: Property = - objects.property() + project.objects.property() final override fun from(notation: Any) { @@ -497,18 +495,18 @@ private abstract class AbstractHelmRelease( final override val replace: Property = - objects.property() + project.objects.property() .convention(false) final override val keepHistoryOnUninstall: Property = - objects.property() + project.objects.property() .convention(false) @Suppress("OverridingDeprecatedMember") final override val dependsOn: SetProperty = - objects.setProperty() + project.objects.setProperty() final override val installDependsOn: MutableSet = mutableSetOf() @@ -521,10 +519,9 @@ private abstract class AbstractHelmRelease( private open class DefaultHelmRelease @Inject constructor( - objects: ObjectFactory, name: String, project: Project -) : AbstractHelmRelease(objects, name, project), HelmRelease, HelmReleaseInternal { +) : AbstractHelmRelease(name, project), HelmRelease, HelmReleaseInternal { private val targetSpecificActions = mutableMapOf>() private val targetSpecificCache: MutableMap = ConcurrentHashMap() @@ -534,7 +531,7 @@ private open class DefaultHelmRelease override val valuesDirs: ListProperty = - objects.listProperty() + project.objects.listProperty() override fun valuesDirs(directories: Iterable) { @@ -592,7 +589,7 @@ private open class DefaultHelmRelease logger.info("Constructing target-specific release \"{}\" for target \"{}\"", this.name, target.name) - return TargetSpecific(project.objects, name, project, target).also { targetSpecific -> + return TargetSpecific(name, project, target).also { targetSpecific -> // Call setFrom(HelmInstallFromRepositoryOptions) to assign all the options properties that only exist // on HelmRelease, but not on HelmReleaseTarget @@ -646,11 +643,10 @@ private open class DefaultHelmRelease private class TargetSpecific( - objects: ObjectFactory, name: String, project: Project, override val target: HelmReleaseTarget - ) : AbstractHelmRelease(objects, name, project), HelmRelease.TargetSpecific + ) : AbstractHelmRelease(name, project), HelmRelease.TargetSpecific } diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmReleaseTarget.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmReleaseTarget.kt index 9643739a..566d7840 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmReleaseTarget.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/release/dsl/HelmReleaseTarget.kt @@ -4,6 +4,7 @@ import org.gradle.api.Named import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.ExtensionAware import org.gradle.api.plugins.ExtensionContainer @@ -84,10 +85,11 @@ private open class DefaultHelmReleaseTarget @Inject constructor( private val name: String, private val globalSelectTagsExpression: TagExpression, - objects: ObjectFactory + objects: ObjectFactory, + layout: ProjectLayout ) : HelmReleaseTarget, HelmReleaseTargetInternal, ConfigurableHelmInstallationOptions by HelmInstallationOptionsHolder(objects), - ConfigurableHelmValueOptions by HelmValueOptionsHolder(objects) { + ConfigurableHelmValueOptions by HelmValueOptionsHolder(objects, layout) { private var localSelectTagsExpression: TagExpression = TagExpression.alwaysMatch() diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/util/GradleVersions.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/util/GradleVersions.kt index 31e5e9bd..70535798 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/util/GradleVersions.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/util/GradleVersions.kt @@ -3,6 +3,7 @@ package org.unbrokendome.gradle.plugins.helm.util import org.gradle.util.GradleVersion +internal val GRADLE_VERSION_5_3 = GradleVersion.version("5.3") internal val GRADLE_VERSION_5_6 = GradleVersion.version("5.6") internal val GRADLE_VERSION_6_0 = GradleVersion.version("6.0") internal val GRADLE_VERSION_6_2 = GradleVersion.version("6.2")