package buildsrc.tasks import buildsrc.settings.DokkatooExampleProjectsSettings.DokkatooExampleProjectSpec import javax.inject.Inject import org.gradle.api.DefaultTask import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.model.ObjectFactory import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Internal import org.gradle.api.tasks.Nested import org.gradle.api.tasks.TaskAction /** * Utility for updating the `gradle.properties` of projects used in automated tests. */ @CacheableTask abstract class UpdateDokkatooExampleProjects @Inject constructor( @get:Internal val objects: ObjectFactory ) : DefaultTask() { @get:Nested abstract val exampleProjects: NamedDomainObjectContainer private val taskPath: String = path // renamed for clarity @TaskAction fun update() { exampleProjects.forEach { exampleProject -> updateGradleProperties(exampleProject) } } private fun updateGradleProperties(exampleProject: DokkatooExampleProjectSpec) { val gradlePropertiesContent = exampleProject.gradlePropertiesContent.orNull?.sorted() ?: return val content = buildString { appendLine("# DO NOT EDIT - Generated by $taskPath") appendLine() gradlePropertiesContent.forEach { appendLine(it) } } exampleProject.gradlePropertiesFile.get().asFile.writeText(content) } }