Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / build-logic / src / main / kotlin / IconsGen.kt
IconsGen.kt170 lines · 7.6 KiBRaw
| 1 | import org.gradle.api.DefaultTask |
| 2 | import org.gradle.api.GradleException |
| 3 | import org.gradle.api.file.DirectoryProperty |
| 4 | import org.gradle.api.file.RegularFileProperty |
| 5 | import org.gradle.api.tasks.InputFile |
| 6 | import org.gradle.api.tasks.OutputDirectory |
| 7 | import org.gradle.api.tasks.PathSensitive |
| 8 | import org.gradle.api.tasks.PathSensitivity |
| 9 | import org.gradle.api.tasks.TaskAction |
| 10 | |
| 11 | // Generates the `IconXxx()` composables for `:library:ui` from the checked-in |
| 12 | // manifest `library/ui/icons.tsv` (one tab-separated line per wrapper; see the |
| 13 | // header of that file for the format). The output is a build-generated Kotlin |
| 14 | // source directory, so the wrappers never exist as hand-written source: a |
| 15 | // wrapper that is not in the manifest does not exist, and call sites naming it |
| 16 | // fail compilation — there is no runtime fallback. Driven from |
| 17 | // `library/ui/build.gradle.kts` via `variant.sources.kotlin`, mirroring the |
| 18 | // launcher-icon generation in `common-conventions-app`. |
| 19 | abstract class GenerateIconsTask : DefaultTask() { |
| 20 | @get:InputFile |
| 21 | @get:PathSensitive(PathSensitivity.RELATIVE) |
| 22 | abstract val manifest: RegularFileProperty |
| 23 | |
| 24 | @get:OutputDirectory |
| 25 | abstract val outputDir: DirectoryProperty |
| 26 | |
| 27 | @TaskAction |
| 28 | fun generate() { |
| 29 | val file = manifest.get().asFile |
| 30 | val entries = mutableListOf<Entry>() |
| 31 | file.readLines().forEachIndexed { index, raw -> |
| 32 | val line = raw.trimEnd() |
| 33 | if (line.isEmpty() || line.startsWith("#")) return@forEachIndexed |
| 34 | val parts = line.split("\t") |
| 35 | if (parts.size !in 4..5) { |
| 36 | throw GradleException( |
| 37 | "icons.tsv:${index + 1}: expected 4-5 tab-separated fields, got ${parts.size}: $line", |
| 38 | ) |
| 39 | } |
| 40 | entries += Entry( |
| 41 | line = index + 1, |
| 42 | name = parts[0].trim(), |
| 43 | kind = parts[1].trim(), |
| 44 | source = parts[2].trim(), |
| 45 | description = parts[3], |
| 46 | doc = parts.getOrNull(4)?.trim().orEmpty(), |
| 47 | ) |
| 48 | } |
| 49 | if (entries.isEmpty()) throw GradleException("icons.tsv: no icon entries found in $file") |
| 50 | |
| 51 | // Duplicate wrappers (same name AND same parameter shape) would not |
| 52 | // compile; overloads differing by kind (e.g. IconShuffle S + F) are |
| 53 | // the existing API and stay legal. |
| 54 | val seen = mutableSetOf<String>() |
| 55 | for (entry in entries) { |
| 56 | val key = "${entry.name}:${entry.kind}" |
| 57 | if (!seen.add(key)) { |
| 58 | throw GradleException("icons.tsv:${entry.line}: duplicate wrapper $key") |
| 59 | } |
| 60 | entry.validate() |
| 61 | } |
| 62 | |
| 63 | val imports = sortedSetOf( |
| 64 | "androidx.compose.material.icons.Icons", |
| 65 | "androidx.compose.material3.LocalContentColor", |
| 66 | "androidx.compose.runtime.Composable", |
| 67 | "androidx.compose.ui.Modifier", |
| 68 | "androidx.compose.ui.graphics.Color", |
| 69 | ) |
| 70 | for (entry in entries) { |
| 71 | when { |
| 72 | entry.source.startsWith("Filled.") -> |
| 73 | imports += "androidx.compose.material.icons.filled.${entry.source.removePrefix("Filled.")}" |
| 74 | entry.source.startsWith("Outlined.") -> |
| 75 | imports += "androidx.compose.material.icons.outlined.${entry.source.removePrefix("Outlined.")}" |
| 76 | entry.source.startsWith("AutoMirrored.Filled.") -> |
| 77 | imports += "androidx.compose.material.icons.automirrored.filled.${entry.source.removePrefix("AutoMirrored.Filled.")}" |
| 78 | entry.source.startsWith("AutoMirrored.Outlined.") -> |
| 79 | imports += "androidx.compose.material.icons.automirrored.outlined.${entry.source.removePrefix("AutoMirrored.Outlined.")}" |
| 80 | entry.source.startsWith("drawable:") -> { |
| 81 | imports += "androidx.compose.material3.Icon" |
| 82 | imports += "androidx.compose.ui.res.painterResource" |
| 83 | imports += "com.vayunmathur.library.R" |
| 84 | } |
| 85 | entry.source.startsWith("vector:") -> Unit // same package: IconsCustom.kt |
| 86 | else -> throw GradleException("icons.tsv:${entry.line}: unknown source '${entry.source}'") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | val out = StringBuilder() |
| 91 | out.appendLine("package com.vayunmathur.library.ui") |
| 92 | out.appendLine() |
| 93 | imports.forEach { out.appendLine("import $it") } |
| 94 | out.appendLine() |
| 95 | out.appendLine("// GENERATED from library/ui/icons.tsv by GenerateIconsTask — Do not hand-edit.") |
| 96 | out.appendLine("// Add an icon: add one line to library/ui/icons.tsv and rebuild.") |
| 97 | for (entry in entries) { |
| 98 | out.appendLine() |
| 99 | if (entry.doc.isNotEmpty()) out.appendLine("/** ${entry.doc} */") |
| 100 | out.appendLine("@Composable") |
| 101 | out.append(entry.render()) |
| 102 | out.appendLine() |
| 103 | } |
| 104 | |
| 105 | val target = outputDir.get().asFile.resolve("IconsGenerated.kt") |
| 106 | target.parentFile.mkdirs() |
| 107 | target.writeText(out.toString()) |
| 108 | } |
| 109 | |
| 110 | private data class Entry( |
| 111 | val line: Int, |
| 112 | val name: String, |
| 113 | val kind: String, |
| 114 | val source: String, |
| 115 | val description: String, |
| 116 | val doc: String, |
| 117 | ) { |
| 118 | fun validate() { |
| 119 | if (!name.matches(Regex("Icon[A-Z][A-Za-z0-9]*"))) { |
| 120 | throw GradleException("icons.tsv:$line: bad wrapper name '$name'") |
| 121 | } |
| 122 | if (kind !in setOf("S", "T", "F")) { |
| 123 | throw GradleException("icons.tsv:$line: bad kind '$kind' (expected S, T or F)") |
| 124 | } |
| 125 | if (kind == "F" && description.isNotEmpty()) { |
| 126 | throw GradleException("icons.tsv:$line: kind F takes no description (the caller supplies it)") |
| 127 | } |
| 128 | if (kind != "F" && description.isEmpty()) { |
| 129 | throw GradleException("icons.tsv:$line: kind $kind needs a description") |
| 130 | } |
| 131 | if (doc.contains("*/")) { |
| 132 | throw GradleException("icons.tsv:$line: KDoc must not contain '*/'") |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private fun kotlinString(value: String): String = |
| 137 | "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$") + "\"" |
| 138 | |
| 139 | private fun call(): String = when { |
| 140 | source.startsWith("drawable:") -> |
| 141 | "Icon(painterResource(R.drawable.${source.removePrefix("drawable:")}), " + |
| 142 | "${kotlinString(description)}, modifier = modifier, tint = tint)" |
| 143 | kind == "S" -> "AppIcon(Icons.$source, ${kotlinString(description)}, modifier, tint)" |
| 144 | else -> "AppIcon(${ |
| 145 | if (source.startsWith("vector:")) source.removePrefix("vector:") else "Icons.$source" |
| 146 | }, contentDescription, modifier, tint)" |
| 147 | } |
| 148 | |
| 149 | private fun vectorCall(): String = |
| 150 | if (source.startsWith("vector:")) { |
| 151 | "AppIcon(${source.removePrefix("vector:")}, ${kotlinString(description)}, modifier, tint)" |
| 152 | } else { |
| 153 | call() |
| 154 | } |
| 155 | |
| 156 | fun render(): String = when (kind) { |
| 157 | "S" -> "fun $name(modifier: Modifier = Modifier, tint: Color = LocalContentColor.current) =\n ${vectorCall()}\n" |
| 158 | "T" -> "fun $name(\n" + |
| 159 | " modifier: Modifier = Modifier,\n" + |
| 160 | " tint: Color = LocalContentColor.current,\n" + |
| 161 | " contentDescription: String? = ${kotlinString(description)},\n" + |
| 162 | ") = ${call()}\n" |
| 163 | else -> "fun $name(\n" + |
| 164 | " contentDescription: String?,\n" + |
| 165 | " modifier: Modifier = Modifier,\n" + |
| 166 | " tint: Color = LocalContentColor.current,\n" + |
| 167 | ") = ${call()}\n" |
| 168 | } |
| 169 | } |
| 170 | } |