From 0dc8f216386013b63719427dd9b71434796bb05b Mon Sep 17 00:00:00 2001 From: Gabriel Souza Date: Sun, 15 Sep 2024 18:38:02 -0300 Subject: [PATCH] Fix named accessor code generator --- build.gradle.kts | 2 +- .../icons/generator/AllIconAccessorGenerator.kt | 16 ++++++++++++---- src/test/kotlin/Main.kt | 4 +++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 39927c04..02f32e01 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "br.com.devsrsouza" -version = "0.10.0" +version = "0.11.0-SNAPSHOT" repositories { mavenCentral() diff --git a/src/main/kotlin/androidx/compose/material/icons/generator/AllIconAccessorGenerator.kt b/src/main/kotlin/androidx/compose/material/icons/generator/AllIconAccessorGenerator.kt index e093ce24..7cc3b94c 100644 --- a/src/main/kotlin/androidx/compose/material/icons/generator/AllIconAccessorGenerator.kt +++ b/src/main/kotlin/androidx/compose/material/icons/generator/AllIconAccessorGenerator.kt @@ -70,16 +70,24 @@ class AllIconAccessorGenerator( // preventing that a asset has the name List and conflict with Kotlin List import fileSpec.addAliasedImport(map, "____KtMap") + val allAssetsNamedPropertyName = "${allAssetsPropertyName}Named" + val allIconsType = map.parameterizedBy(STRING, ClassNames.ImageVector) - val allIconsBackingProperty = backingPropertySpec("__${allAssetsPropertyName}Named", allIconsType) + val allIconsBackingProperty = backingPropertySpec("__$allAssetsNamedPropertyName", allIconsType) - val allIconsParametersFromGroups = childGroups.map { "%M.${allAssetsPropertyName}Named.mapKeys { \"\${%M.groupName}.\${it.key}\"}" } + val allIconsParametersFromGroups = childGroups.map { "%M.$allAssetsNamedPropertyName.mapKeys { \"\${%M.groupName}.\${it.key}\"}" } - val allIconsParameters = iconProperties.map { "${it.simpleName.lowercase()} to %M" } + // adding import to `AllAssetsNamed` + childGroups.forEach { + fileSpec.addImport(it.groupPackage, allAssetsNamedPropertyName) + fileSpec.addImport(it.groupPackage, "groupName") + } + + val allIconsParameters = iconProperties.map { "\"${it.simpleName.lowercase()}\" to %M" } val parameters = allIconsParameters.joinToString(prefix = "(", postfix = ")") val childGroupsParameters = allIconsParametersFromGroups.joinToString(" + ") - val allIconProperty = PropertySpec.builder(allAssetsPropertyName, allIconsType) + val allIconProperty = PropertySpec.builder(allAssetsNamedPropertyName, allIconsType) .receiver(accessClass) .getter(FunSpec.getterBuilder().withBackingProperty(allIconsBackingProperty) { addStatement( diff --git a/src/test/kotlin/Main.kt b/src/test/kotlin/Main.kt index 936894fb..f84b3a3d 100644 --- a/src/test/kotlin/Main.kt +++ b/src/test/kotlin/Main.kt @@ -13,7 +13,9 @@ fun main() { vectorsDirectory = iconTest, iconNameTransformer = { name, group -> name.removeSuffix(group, ignoreCase = true) - } + }, + generatePreview = true, + generateStringAccessor = true, ) }