Skip to content

Commit

Permalink
Task has SKIPPED state if there are no fields to generate
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Jan 7, 2024
1 parent c0a7e62 commit da4aec9
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -59,4 +73,9 @@ abstract class BuildConfigTask : DefaultTask() {
}
}

private object HasFields : Spec<Task> {
override fun isSatisfiedBy(task: Task): Boolean =
(task as BuildConfigTask).specs.get().any { fields -> fields.buildConfigFields.isNotEmpty() }
}

}

0 comments on commit da4aec9

Please sign in to comment.