From 7b17b953e69f92845475574556b6901282f38747 Mon Sep 17 00:00:00 2001 From: Marco Gomiero Date: Fri, 5 Apr 2024 21:49:44 +0200 Subject: [PATCH] Add option on the xml processor to avoid creating compose accessors (#46) --- README.md | 2 + .../xml/internal/LyricistXmlConfig.kt | 1 + .../internal/LyricistXmlSymbolProcessor.kt | 77 ++++++++++++------- .../LyricistXmlSymbolProcessorProvider.kt | 4 +- 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 569b035..dd427ee 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,7 @@ Let's say you have a "dashboard" module, the generated declarations will be `Loc ## Migrating from `strings.xml` So you liked Lyricist, but already have a project with thousands of strings spread over multiples files? I have good news for you: Lyricist can extract these existing strings and generate all the code you just saw above. +If you don't want to have the Compose code generated by KSP, you can set the `lyricist.xml.generateComposeAccessors` arg to `"false"`, and you can write the code manually by following the instructions [below](#extending-lyricist). Similar to the multi module setup, you must provide a few arguments to KSP. Lyricist will search for `strings.xml` files in the resources path. You can also provide a language tag to be used as default value for the `LocalStrings`. ```gradle @@ -192,6 +193,7 @@ ksp { arg("lyricist.packageName", "com.my.app") arg("lyricist.xml.moduleName", "xml") arg("lyricist.xml.defaultLanguageTag", "en") + arg("lyricist.xml.generateComposeAccessors", "false") } ``` diff --git a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlConfig.kt b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlConfig.kt index 7f25057..d674f27 100644 --- a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlConfig.kt +++ b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlConfig.kt @@ -5,4 +5,5 @@ internal data class LyricistXmlConfig( val moduleName: String, val defaultLanguageTag: String, val resourcesPath: String, + val generateComposeAccessors: Boolean, ) diff --git a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt index 9b67a5d..5cff7d1 100644 --- a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt +++ b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt @@ -109,14 +109,26 @@ internal class LyricistXmlSymbolProcessor( """ |package ${config.packageName} | - |import androidx.compose.runtime.Composable - |import androidx.compose.runtime.ProvidableCompositionLocal - |import androidx.compose.runtime.staticCompositionLocalOf - |import androidx.compose.ui.text.intl.Locale - |import cafe.adriel.lyricist.Lyricist - |import cafe.adriel.lyricist.LanguageTag - |import cafe.adriel.lyricist.rememberStrings - |import cafe.adriel.lyricist.ProvideStrings + """.trimMargin().toByteArray() + ) + if (config.generateComposeAccessors) { + stream.write( + """ + | + |import androidx.compose.runtime.Composable + |import androidx.compose.runtime.ProvidableCompositionLocal + |import androidx.compose.runtime.staticCompositionLocalOf + |import androidx.compose.ui.text.intl.Locale + |import cafe.adriel.lyricist.Lyricist + |import cafe.adriel.lyricist.LanguageTag + |import cafe.adriel.lyricist.rememberStrings + |import cafe.adriel.lyricist.ProvideStrings + | + """.trimMargin().toByteArray() + ) + } + stream.write( + """ | |public interface $fileName { |$values @@ -126,29 +138,36 @@ internal class LyricistXmlSymbolProcessor( |$localesOutput |} | - |public val $stringsName: Map = mapOf( - |$translationMappingOutput - |) - | - |public val Local$fileName: ProvidableCompositionLocal<$fileName> = - | staticCompositionLocalOf { $defaultStringsOutput } - | - |@Composable - |public fun remember$fileName( - | defaultLanguageTag: LanguageTag = "${config.defaultLanguageTag}", - | currentLanguageTag: LanguageTag = Locale.current.toLanguageTag(), - |): Lyricist<$fileName> = - | rememberStrings($stringsName, defaultLanguageTag, currentLanguageTag) - | - |@Composable - |public fun Provide$fileName( - | lyricist: Lyricist<$fileName>, - | content: @Composable () -> Unit - |) { - | ProvideStrings(lyricist, Local$fileName, content) - |} """.trimMargin().toByteArray() ) + if (config.generateComposeAccessors) { + stream.write( + """ + | + |public val $stringsName: Map = mapOf( + |$translationMappingOutput + |) + | + |public val Local$fileName: ProvidableCompositionLocal<$fileName> = + | staticCompositionLocalOf { $defaultStringsOutput } + | + |@Composable + |public fun remember$fileName( + | defaultLanguageTag: LanguageTag = "${config.defaultLanguageTag}", + | currentLanguageTag: LanguageTag = Locale.current.toLanguageTag(), + |): Lyricist<$fileName> = + | rememberStrings($stringsName, defaultLanguageTag, currentLanguageTag) + | + |@Composable + |public fun Provide$fileName( + | lyricist: Lyricist<$fileName>, + | content: @Composable () -> Unit + |) { + | ProvideStrings(lyricist, Local$fileName, content) + |} + """.trimMargin().toByteArray() + ) + } } } diff --git a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessorProvider.kt b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessorProvider.kt index c5453c0..6700d4e 100644 --- a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessorProvider.kt +++ b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessorProvider.kt @@ -14,7 +14,8 @@ internal class LyricistXmlSymbolProcessorProvider : SymbolProcessorProvider { moduleName = options[ARG_MODULE_NAME].orEmpty(), defaultLanguageTag = options[ARG_DEFAULT_LANGUAGE_TAG] ?: DEFAULT_LANGUAGE_TAG, resourcesPath = options[ARG_RESOURCES_PATH] - ?: throw IllegalArgumentException("lyricist.xml.resourcesPath not found") + ?: throw IllegalArgumentException("lyricist.xml.resourcesPath not found"), + generateComposeAccessors = options[ARG_GENERATE_COMPOSE_ACCESSORS]?.toBoolean() ?: true ), codeGenerator = codeGenerator, logger = logger @@ -26,6 +27,7 @@ internal class LyricistXmlSymbolProcessorProvider : SymbolProcessorProvider { const val ARG_MODULE_NAME = "lyricist.xml.moduleName" const val ARG_RESOURCES_PATH = "lyricist.xml.resourcesPath" const val ARG_DEFAULT_LANGUAGE_TAG = "lyricist.xml.defaultLanguageTag" + const val ARG_GENERATE_COMPOSE_ACCESSORS = "lyricist.xml.generateComposeAccessors" const val DEFAULT_PACKAGE_NAME = "cafe.adriel.lyricist" const val DEFAULT_LANGUAGE_TAG = "en"