import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.RegularFileProperty 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 // Generates the `IconXxx()` composables for `:library:ui` from the checked-in // manifest `library/ui/icons.tsv` (one tab-separated line per wrapper; see the // header of that file for the format). The output is a build-generated Kotlin // source directory, so the wrappers never exist as hand-written source: a // wrapper that is not in the manifest does not exist, and call sites naming it // fail compilation — there is no runtime fallback. Driven from // `library/ui/build.gradle.kts` via `variant.sources.kotlin`, mirroring the // launcher-icon generation in `common-conventions-app`. abstract class GenerateIconsTask : DefaultTask() { @get:InputFile @get:PathSensitive(PathSensitivity.RELATIVE) abstract val manifest: RegularFileProperty @get:OutputDirectory abstract val outputDir: DirectoryProperty @TaskAction fun generate() { val file = manifest.get().asFile val entries = mutableListOf() file.readLines().forEachIndexed { index, raw -> val line = raw.trimEnd() if (line.isEmpty() || line.startsWith("#")) return@forEachIndexed val parts = line.split("\t") if (parts.size !in 4..5) { throw GradleException( "icons.tsv:${index + 1}: expected 4-5 tab-separated fields, got ${parts.size}: $line", ) } entries += Entry( line = index + 1, name = parts[0].trim(), kind = parts[1].trim(), source = parts[2].trim(), description = parts[3], doc = parts.getOrNull(4)?.trim().orEmpty(), ) } if (entries.isEmpty()) throw GradleException("icons.tsv: no icon entries found in $file") // Duplicate wrappers (same name AND same parameter shape) would not // compile; overloads differing by kind (e.g. IconShuffle S + F) are // the existing API and stay legal. val seen = mutableSetOf() for (entry in entries) { val key = "${entry.name}:${entry.kind}" if (!seen.add(key)) { throw GradleException("icons.tsv:${entry.line}: duplicate wrapper $key") } entry.validate() } val imports = sortedSetOf( "androidx.compose.material.icons.Icons", "androidx.compose.material3.LocalContentColor", "androidx.compose.runtime.Composable", "androidx.compose.ui.Modifier", "androidx.compose.ui.graphics.Color", ) for (entry in entries) { when { entry.source.startsWith("Filled.") -> imports += "androidx.compose.material.icons.filled.${entry.source.removePrefix("Filled.")}" entry.source.startsWith("Outlined.") -> imports += "androidx.compose.material.icons.outlined.${entry.source.removePrefix("Outlined.")}" entry.source.startsWith("AutoMirrored.Filled.") -> imports += "androidx.compose.material.icons.automirrored.filled.${entry.source.removePrefix("AutoMirrored.Filled.")}" entry.source.startsWith("AutoMirrored.Outlined.") -> imports += "androidx.compose.material.icons.automirrored.outlined.${entry.source.removePrefix("AutoMirrored.Outlined.")}" entry.source.startsWith("drawable:") -> { imports += "androidx.compose.material3.Icon" imports += "androidx.compose.ui.res.painterResource" imports += "com.vayunmathur.library.R" } entry.source.startsWith("vector:") -> Unit // same package: IconsCustom.kt else -> throw GradleException("icons.tsv:${entry.line}: unknown source '${entry.source}'") } } val out = StringBuilder() out.appendLine("package com.vayunmathur.library.ui") out.appendLine() imports.forEach { out.appendLine("import $it") } out.appendLine() out.appendLine("// GENERATED from library/ui/icons.tsv by GenerateIconsTask — Do not hand-edit.") out.appendLine("// Add an icon: add one line to library/ui/icons.tsv and rebuild.") for (entry in entries) { out.appendLine() if (entry.doc.isNotEmpty()) out.appendLine("/** ${entry.doc} */") out.appendLine("@Composable") out.append(entry.render()) out.appendLine() } val target = outputDir.get().asFile.resolve("IconsGenerated.kt") target.parentFile.mkdirs() target.writeText(out.toString()) } private data class Entry( val line: Int, val name: String, val kind: String, val source: String, val description: String, val doc: String, ) { fun validate() { if (!name.matches(Regex("Icon[A-Z][A-Za-z0-9]*"))) { throw GradleException("icons.tsv:$line: bad wrapper name '$name'") } if (kind !in setOf("S", "T", "F")) { throw GradleException("icons.tsv:$line: bad kind '$kind' (expected S, T or F)") } if (kind == "F" && description.isNotEmpty()) { throw GradleException("icons.tsv:$line: kind F takes no description (the caller supplies it)") } if (kind != "F" && description.isEmpty()) { throw GradleException("icons.tsv:$line: kind $kind needs a description") } if (doc.contains("*/")) { throw GradleException("icons.tsv:$line: KDoc must not contain '*/'") } } private fun kotlinString(value: String): String = "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$") + "\"" private fun call(): String = when { source.startsWith("drawable:") -> "Icon(painterResource(R.drawable.${source.removePrefix("drawable:")}), " + "${kotlinString(description)}, modifier = modifier, tint = tint)" kind == "S" -> "AppIcon(Icons.$source, ${kotlinString(description)}, modifier, tint)" else -> "AppIcon(${ if (source.startsWith("vector:")) source.removePrefix("vector:") else "Icons.$source" }, contentDescription, modifier, tint)" } private fun vectorCall(): String = if (source.startsWith("vector:")) { "AppIcon(${source.removePrefix("vector:")}, ${kotlinString(description)}, modifier, tint)" } else { call() } fun render(): String = when (kind) { "S" -> "fun $name(modifier: Modifier = Modifier, tint: Color = LocalContentColor.current) =\n ${vectorCall()}\n" "T" -> "fun $name(\n" + " modifier: Modifier = Modifier,\n" + " tint: Color = LocalContentColor.current,\n" + " contentDescription: String? = ${kotlinString(description)},\n" + ") = ${call()}\n" else -> "fun $name(\n" + " contentDescription: String?,\n" + " modifier: Modifier = Modifier,\n" + " tint: Color = LocalContentColor.current,\n" + ") = ${call()}\n" } } }