Android overhaul: one CopyCategoryKeywords task in build-logic instead of a copy in :data and :core:fakes

The task class and its wiring were duplicated verbatim in both build scripts; bundleCategoryKeywords(resourcePath)
registers it once per module. Resource paths and inputs are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TQd3pEP9UeN2uifbHknQ7B
This commit is contained in:
2026-10-18 22:53:44 +02:00
parent 48fe119ae1
commit e4e6e5d792
11 changed files with 76 additions and 368 deletions
@@ -0,0 +1,59 @@
package se.hagstedt.kvitt.buildlogic
import com.android.build.api.variant.AndroidComponentsExtension
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
/** Copies the keyword table to [resourcePath] under [outputDir]. A missing table fails the build. */
abstract class CopyCategoryKeywords : DefaultTask() {
@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
abstract val table: RegularFileProperty
@get:Input
abstract val resourcePath: Property<String>
@get:OutputDirectory
abstract val outputDir: DirectoryProperty
@TaskAction
fun copy() {
val source = table.get().asFile
if (!source.isFile) throw GradleException("missing ${source.path}")
val root = outputDir.get().asFile
root.deleteRecursively()
val target = root.resolve(resourcePath.get())
target.parentFile.mkdirs()
source.copyTo(target)
}
}
/** `contracts/category-keywords.csv`, the keyword table of the category suggestion (UX-3, SPEC §9.11 CS-6). */
fun Project.categoryKeywordsTable(): RegularFile = rootProject.layout.projectDirectory.file("../contracts/category-keywords.csv")
/**
* Bundles the keyword table as the Java resource [resourcePath] of every variant, copied from contracts/ at build time
* (never hand-copied). Used by `:data` and `:core:fakes`.
*/
fun Project.bundleCategoryKeywords(resourcePath: String) {
val copy = tasks.register("copyCategoryKeywords", CopyCategoryKeywords::class.java) {
description = "Copies contracts/category-keywords.csv into the main Java resources."
table.set(categoryKeywordsTable())
this.resourcePath.set(resourcePath)
outputDir.set(layout.buildDirectory.dir("generated/category-keywords"))
}
extensions.getByType(AndroidComponentsExtension::class.java).onVariants { variant ->
variant.sources.resources?.addGeneratedSourceDirectory(copy, CopyCategoryKeywords::outputDir)
}
}