From da4aec90cc8ea446a244e4757b6bab5cff5332de Mon Sep 17 00:00:00 2001 From: Guillermo Mazzola Date: Sun, 7 Jan 2024 21:33:56 +0100 Subject: [PATCH] Task has `SKIPPED` state if there are no fields to generate --- .../gmazzo/buildconfig/BuildConfigTask.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt index 557df53..d0df70b 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/BuildConfigTask.kt @@ -1,15 +1,20 @@ +@file:Suppress("LeakingThis") + package com.github.gmazzo.buildconfig import com.github.gmazzo.buildconfig.generators.BuildConfigGenerator import com.github.gmazzo.buildconfig.generators.BuildConfigGeneratorSpec import org.gradle.api.DefaultTask +import org.gradle.api.Task import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property import org.gradle.api.provider.SetProperty +import org.gradle.api.specs.Spec import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Nested import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction +import org.gradle.util.GradleVersion import java.io.File @CacheableTask @@ -24,6 +29,15 @@ abstract class BuildConfigTask : DefaultTask() { @get:OutputDirectory abstract val outputDir: DirectoryProperty + init { + if (GradleVersion.current() >= GradleVersion.version("7.6")) { + onlyIf("There are build config fields to generate", HasFields) + + } else { + onlyIf(HasFields) + } + } + @TaskAction fun generateBuildConfigFile() { val dir = outputDir.get().asFile @@ -59,4 +73,9 @@ abstract class BuildConfigTask : DefaultTask() { } } + private object HasFields : Spec { + override fun isSatisfiedBy(task: Task): Boolean = + (task as BuildConfigTask).specs.get().any { fields -> fields.buildConfigFields.isNotEmpty() } + } + }