From 6ef9bdb391e82f4e820a0f8eb03bac79aa3e44ab Mon Sep 17 00:00:00 2001 From: malilex Date: Tue, 19 Mar 2024 16:41:57 +0300 Subject: [PATCH] feat(sdds-acore/theme-builder): Generators were refactored. --- gradle/libs.versions.toml | 2 +- .../internal/generator/BaseGenerator.kt | 63 ++++++++++++++++++- .../internal/generator/ColorGenerator.kt | 53 +++++++--------- .../internal/generator/DimenGenerator.kt | 4 +- .../internal/generator/GradientGenerator.kt | 51 +++++++-------- .../internal/generator/ShadowGenerator.kt | 57 +++++++---------- .../internal/generator/ShapeGenerator.kt | 61 ++++++++---------- .../internal/generator/TypographyGenerator.kt | 62 ++++++++---------- 8 files changed, 183 insertions(+), 170 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 69368dd6..06466395 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -24,7 +24,7 @@ test-robolectric = "4.9" test-androidX-rules = "1.5.0" test-androidX-runner = "1.5.2" -kotlinSerialization = "1.4.0" +kotlinSerialization = "1.3.2" kotlinPoet = "1.12.0" plugin-androidCacheFix = "3.0.1" diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/BaseGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/BaseGenerator.kt index 3e7bd8be..ac63633a 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/BaseGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/BaseGenerator.kt @@ -1,5 +1,8 @@ package com.sdds.plugin.themebuilder.internal.generator +import com.sdds.plugin.themebuilder.ThemeBuilderTarget +import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isComposeOrAll +import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isViewSystemOrAll import com.sdds.plugin.themebuilder.internal.token.Token import com.sdds.plugin.themebuilder.internal.token.TokenValue @@ -19,10 +22,66 @@ internal interface BaseGenerator { * Интерфейс генератора токенов * @author Малышев Александр on 07.03.2024 */ -internal interface TokenGenerator> : BaseGenerator { +internal abstract class TokenGenerator>( + private val target: ThemeBuilderTarget, +) : BaseGenerator { + + private var needGenerateCompose: Boolean = false + private var needGenerateViewSystem: Boolean = false /** * Добавляет [token] для генерации данных */ - fun addToken(token: T) + fun addToken(token: T) { + val result = when (target) { + ThemeBuilderTarget.VIEW_SYSTEM -> addViewSystemToken(token) + ThemeBuilderTarget.COMPOSE -> addComposeToken(token) + ThemeBuilderTarget.ALL -> addViewSystemToken(token) && addComposeToken(token) + } + + if (!needGenerateViewSystem && target.isViewSystemOrAll && result) { + needGenerateViewSystem = true + } + + if (!needGenerateCompose && target.isComposeOrAll && result) { + needGenerateCompose = true + } + } + + /** + * @see BaseGenerator.generate + */ + final override fun generate() { + if (needGenerateViewSystem) { + generateViewSystem() + } + + if (needGenerateCompose) { + generateCompose() + } + } + + /** + * Добавляет токен [token] для ViewSystem. + * @return статус добавления токена + */ + protected open fun addViewSystemToken(token: T): Boolean = false + + /** + * Добавляет токен [token] для Compose. + * @return статус добавления токена + */ + protected open fun addComposeToken(token: T): Boolean = false + + /** + * Генерирует файл с токенами для ViewSystem, если хотя бы один токен + * был успешно добавлен + */ + protected open fun generateViewSystem() = Unit + + /** + * Генерирует файл с токенами для Compose, если хотя бы один токен + * был успешно добавлен + */ + protected open fun generateCompose() = Unit } diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ColorGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ColorGenerator.kt index 84b4724b..a320e476 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ColorGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ColorGenerator.kt @@ -1,8 +1,6 @@ package com.sdds.plugin.themebuilder.internal.generator import com.sdds.plugin.themebuilder.ThemeBuilderTarget -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isComposeOrAll -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isViewSystemOrAll import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder.ElementName import com.sdds.plugin.themebuilder.internal.factory.KtFileBuilderFactory @@ -24,63 +22,54 @@ import java.io.File internal class ColorGenerator( private val outputLocation: KtFileBuilder.OutputLocation, private val outputResDir: File, - private val target: ThemeBuilderTarget, + target: ThemeBuilderTarget, private val xmlBuilderFactory: XmlDocumentBuilderFactory, private val ktFileBuilderFactory: KtFileBuilderFactory, -) : TokenGenerator { +) : TokenGenerator(target) { private val xmlDocumentBuilder by unsafeLazy { xmlBuilderFactory.create() } private val ktFileBuilder by unsafeLazy { ktFileBuilderFactory.create("ColorTokens") } private val lightBuilder by unsafeLazy { ktFileBuilder.rootObject("LightColorTokens") } private val darkBuilder by unsafeLazy { ktFileBuilder.rootObject("DarkColorTokens") } - private var needGenerateCompose: Boolean = false - private var needGenerateViewSystem: Boolean = false /** - * @see TokenGenerator.addToken + * @see TokenGenerator.generateViewSystem */ - override fun addToken(token: ColorToken) { - when (target) { - ThemeBuilderTarget.VIEW_SYSTEM -> addViewSystemToken(token) - ThemeBuilderTarget.COMPOSE -> addComposeToken(token) - ThemeBuilderTarget.ALL -> { - addViewSystemToken(token) - addComposeToken(token) - } - } + override fun generateViewSystem() { + xmlDocumentBuilder.build(outputResDir.colorsXmlFile()) } /** - * @see TokenGenerator.generate + * @see TokenGenerator.generateCompose */ - override fun generate() { - if (needGenerateViewSystem) { - xmlDocumentBuilder.build(outputResDir.colorsXmlFile()) - } - - if (needGenerateCompose) { - ktFileBuilder.build(outputLocation) - } + override fun generateCompose() { + ktFileBuilder.build(outputLocation) } - private fun addViewSystemToken(token: ColorToken) { - val tokenValue = token.value ?: return + /** + * @see TokenGenerator.addViewSystemToken + */ + override fun addViewSystemToken(token: ColorToken): Boolean { + val tokenValue = token.value ?: return false xmlDocumentBuilder.appendComment(token.description) xmlDocumentBuilder.appendElement(ElementName.COLOR, token.xmlName, tokenValue.origin) - if (!needGenerateViewSystem && target.isViewSystemOrAll) needGenerateViewSystem = true + return true } - private fun addComposeToken(token: ColorToken) = with(ktFileBuilder) { - val tokenValue = token.value ?: return + /** + * @see TokenGenerator.addComposeToken + */ + override fun addComposeToken(token: ColorToken): Boolean = with(ktFileBuilder) { + val tokenValue = token.value ?: return false val root = if (token.tags.contains("dark")) { darkBuilder } else if (token.tags.contains("light")) { lightBuilder } else { - return + return false } val value = "Color(${tokenValue.origin.replace("#", "0x")})" root.appendProperty(token.ktName, KtFileBuilder.TypeColor, value, token.description) - if (!needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true + return true } } diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/DimenGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/DimenGenerator.kt index 4999799c..fcc87e5a 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/DimenGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/DimenGenerator.kt @@ -31,7 +31,9 @@ internal class DimenGenerator( dimensAggregator.dimens.forEach { xmlDocumentBuilder.appendDimen(it) } - xmlDocumentBuilder.build(outputResDir.dimensFile()) + if (dimensAggregator.dimens.isNotEmpty()) { + xmlDocumentBuilder.build(outputResDir.dimensFile()) + } } private fun XmlDocumentBuilder.appendDimen(dimen: DimenData) { diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/GradientGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/GradientGenerator.kt index 5a62f2f7..099e7c07 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/GradientGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/GradientGenerator.kt @@ -1,8 +1,6 @@ package com.sdds.plugin.themebuilder.internal.generator import com.sdds.plugin.themebuilder.ThemeBuilderTarget -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isComposeOrAll -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isViewSystemOrAll import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder.Companion.appendObject import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder @@ -32,62 +30,55 @@ import java.io.File internal class GradientGenerator( private val outputLocation: KtFileBuilder.OutputLocation, private val outputResDir: File, - private val target: ThemeBuilderTarget, + target: ThemeBuilderTarget, private val xmlBuilderFactory: XmlDocumentBuilderFactory, private val ktFileBuilderFactory: KtFileBuilderFactory, -) : TokenGenerator> { +) : TokenGenerator>(target) { private val xmlDocumentBuilder by unsafeLazy { xmlBuilderFactory.create() } private val ktFileBuilder by unsafeLazy { ktFileBuilderFactory.create("GradientTokens") } private val lightBuilder by unsafeLazy { ktFileBuilder.rootObject("LightGradientTokens") } private val darkBuilder by unsafeLazy { ktFileBuilder.rootObject("DarkGradientTokens") } - private var needGenerateCompose: Boolean = false - private var needGenerateViewSystem: Boolean = false /** - * @see TokenGenerator.addToken + * @see TokenGenerator.generateViewSystem */ - override fun addToken(token: Token) { - when (target) { - ThemeBuilderTarget.VIEW_SYSTEM -> addViewSystemToken(token) - ThemeBuilderTarget.COMPOSE -> addComposeToken(token) - ThemeBuilderTarget.ALL -> { - addViewSystemToken(token) - addComposeToken(token) - } - } + override fun generateViewSystem() { + super.generateViewSystem() + xmlDocumentBuilder.build(outputResDir.gradientsXmlFile()) } /** - * @see TokenGenerator.generate + * @see TokenGenerator.generateCompose */ - override fun generate() { - if (needGenerateViewSystem) { - xmlDocumentBuilder.build(outputResDir.gradientsXmlFile()) - } - - if (needGenerateCompose) { - ktFileBuilder.build(outputLocation) - } + override fun generateCompose() { + super.generateCompose() + ktFileBuilder.build(outputLocation) } - private fun addViewSystemToken(token: Token) { + /** + * @see TokenGenerator.addViewSystemToken + */ + override fun addViewSystemToken(token: Token): Boolean { val result = when (token) { is LinearGradientToken -> xmlDocumentBuilder.appendLinearGradient(token) is RadialGradientToken -> xmlDocumentBuilder.appendRadialGradient(token) is SweepGradientToken -> xmlDocumentBuilder.appendSweepGradient(token) else -> false } - if (result && !needGenerateViewSystem && target.isViewSystemOrAll) needGenerateViewSystem = true + return result } - private fun addComposeToken(token: Token) = with(ktFileBuilder) { + /** + * @see TokenGenerator.addComposeToken + */ + override fun addComposeToken(token: Token): Boolean = with(ktFileBuilder) { val builder = if (token.tags.contains("dark")) { darkBuilder } else if (token.tags.contains("light")) { lightBuilder } else { - return + return false } val result = when (token) { is LinearGradientToken -> builder.appendLinearGradient(token) @@ -95,7 +86,7 @@ internal class GradientGenerator( is SweepGradientToken -> builder.appendSweepGradient(token) else -> false } - if (result && !needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true + return result } private fun XmlDocumentBuilder.appendBaseGradient(baseName: String, colors: List, positions: List) { diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShadowGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShadowGenerator.kt index eb7684b3..6bbb7e3b 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShadowGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShadowGenerator.kt @@ -1,8 +1,6 @@ package com.sdds.plugin.themebuilder.internal.generator import com.sdds.plugin.themebuilder.ThemeBuilderTarget -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isComposeOrAll -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isViewSystemOrAll import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder.Companion.appendObject import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder.ElementName @@ -25,48 +23,38 @@ import java.io.File internal class ShadowGenerator( private val outputLocation: KtFileBuilder.OutputLocation, private val outputResDir: File, - private val target: ThemeBuilderTarget, + target: ThemeBuilderTarget, private val xmlBuilderFactory: XmlDocumentBuilderFactory, private val ktFileBuilderFactory: KtFileBuilderFactory, -) : TokenGenerator { +) : TokenGenerator(target) { private val xmlDocumentBuilder by unsafeLazy { xmlBuilderFactory.create() } private val ktFileBuilder by unsafeLazy { ktFileBuilderFactory.create("ShadowTokens") } private val rootShadows by unsafeLazy { ktFileBuilder.rootObject("ShadowTokens") } - private var needGenerateCompose: Boolean = false - private var needGenerateViewSystem: Boolean = false /** - * @see TokenGenerator.addToken + * @see TokenGenerator.generateViewSystem */ - override fun addToken(token: ShadowToken) { - when (target) { - ThemeBuilderTarget.VIEW_SYSTEM -> addViewSystemToken(token) - ThemeBuilderTarget.COMPOSE -> addComposeToken(token) - ThemeBuilderTarget.ALL -> { - addViewSystemToken(token) - addComposeToken(token) - } - } + override fun generateViewSystem() { + super.generateViewSystem() + xmlDocumentBuilder.build(outputResDir.shadowsXmlFile()) } /** - * @see TokenGenerator.generate + * @see TokenGenerator.generateCompose */ - override fun generate() { - if (needGenerateViewSystem) { - xmlDocumentBuilder.build(outputResDir.shadowsXmlFile()) - } - - if (needGenerateCompose) { - ktFileBuilder.addImport(KtFileBuilder.TypeDpExtension) - ktFileBuilder.addImport(KtFileBuilder.TypeCornerSize) - ktFileBuilder.build(outputLocation) - } + override fun generateCompose() { + super.generateCompose() + ktFileBuilder.addImport(KtFileBuilder.TypeDpExtension) + ktFileBuilder.addImport(KtFileBuilder.TypeCornerSize) + ktFileBuilder.build(outputLocation) } - private fun addViewSystemToken(token: ShadowToken) = with(xmlDocumentBuilder) { - val tokenValue = token.value ?: return + /** + * @see TokenGenerator.addViewSystemToken + */ + override fun addViewSystemToken(token: ShadowToken): Boolean = with(xmlDocumentBuilder) { + val tokenValue = token.value ?: return@with false wrapWithRegion(token.description) { appendElement(ElementName.DIMEN, "shadow_${token.xmlName}_dx", "${tokenValue.dX}dp") @@ -74,11 +62,14 @@ internal class ShadowGenerator( appendElement(ElementName.COLOR, "shadow_${token.xmlName}_color", tokenValue.color) appendElement(ElementName.DIMEN, "shadow_${token.xmlName}_radius", "${tokenValue.radius}dp") } - if (!needGenerateViewSystem && target.isViewSystemOrAll) needGenerateViewSystem = true + return@with true } - private fun addComposeToken(token: ShadowToken) = with(ktFileBuilder) { - val tokenValue = token.value ?: return + /** + * @see TokenGenerator.addComposeToken + */ + override fun addComposeToken(token: ShadowToken): Boolean = with(ktFileBuilder) { + val tokenValue = token.value ?: return@with false rootShadows.appendObject(token.ktName, token.description) { appendProperty("dX", KtFileBuilder.TypeDp, "${tokenValue.dX}.dp", token.description) @@ -91,6 +82,6 @@ internal class ShadowGenerator( token.description, ) } - if (!needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true + return@with true } } diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShapeGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShapeGenerator.kt index cd4400b7..2229095e 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShapeGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/ShapeGenerator.kt @@ -1,8 +1,6 @@ package com.sdds.plugin.themebuilder.internal.generator import com.sdds.plugin.themebuilder.ThemeBuilderTarget -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isComposeOrAll -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isViewSystemOrAll import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder.ElementName import com.sdds.plugin.themebuilder.internal.dimens.DimenData @@ -32,52 +30,42 @@ import java.io.File internal class ShapeGenerator( private val outputLocation: KtFileBuilder.OutputLocation, private val outputResDir: File, - private val target: ThemeBuilderTarget, + target: ThemeBuilderTarget, private val xmlBuilderFactory: XmlDocumentBuilderFactory, private val ktFileBuilderFactory: KtFileBuilderFactory, private val dimensAggregator: DimensAggregator, private val resourceReferenceProvider: ResourceReferenceProvider, -) : TokenGenerator> { +) : TokenGenerator>(target) { private val xmlDocumentBuilder by unsafeLazy { xmlBuilderFactory.create() } private val ktFileBuilder by unsafeLazy { ktFileBuilderFactory.create("ShapeTokens") } private val rootRoundShapes by unsafeLazy { ktFileBuilder.rootObject("RoundShapeTokens") } - private var needGenerateCompose: Boolean = false - private var needGenerateViewSystem: Boolean = false private var needCreateStyle: Boolean = true /** - * @see TokenGenerator.addToken + * @see TokenGenerator.generateViewSystem */ - override fun addToken(token: Token) { - when (target) { - ThemeBuilderTarget.VIEW_SYSTEM -> addViewSystemToken(token) - ThemeBuilderTarget.COMPOSE -> addComposeToken(token) - ThemeBuilderTarget.ALL -> { - addViewSystemToken(token) - addComposeToken(token) - } - } + override fun generateViewSystem() { + super.generateViewSystem() + xmlDocumentBuilder.build(outputResDir.shapesXmlFile()) } /** - * @see TokenGenerator.generate + * @see TokenGenerator.generateCompose */ - override fun generate() { - if (needGenerateViewSystem) { - xmlDocumentBuilder.build(outputResDir.shapesXmlFile()) - } - - if (needGenerateCompose) { - ktFileBuilder.addImport(KtFileBuilder.TypeDpExtension) - ktFileBuilder.addImport(KtFileBuilder.TypeCornerSize) - ktFileBuilder.build(outputLocation) - } + override fun generateCompose() { + super.generateCompose() + ktFileBuilder.addImport(KtFileBuilder.TypeDpExtension) + ktFileBuilder.addImport(KtFileBuilder.TypeCornerSize) + ktFileBuilder.build(outputLocation) } - private fun addViewSystemToken(token: Token) = with(xmlDocumentBuilder) { - val roundedShapeToken = token as? RoundedShapeToken ?: return - val tokenValue = roundedShapeToken.value ?: return + /** + * @see TokenGenerator.addViewSystemToken + */ + override fun addViewSystemToken(token: Token): Boolean = with(xmlDocumentBuilder) { + val roundedShapeToken = token as? RoundedShapeToken ?: return@with false + val tokenValue = roundedShapeToken.value ?: return@with false val cornerSize = DimenData( name = "${roundedShapeToken.name.techToSnakeCase()}_corner_size", value = tokenValue.cornerRadius, @@ -100,12 +88,15 @@ internal class ShapeGenerator( usePrefix = false, ) } - if (!needGenerateViewSystem && target.isViewSystemOrAll) needGenerateViewSystem = true + return@with true } - private fun addComposeToken(token: Token) = with(ktFileBuilder) { - val roundedShapeToken = token as? RoundedShapeToken ?: return - val tokenValue = roundedShapeToken.value ?: return + /** + * @see TokenGenerator.addComposeToken + */ + override fun addComposeToken(token: Token): Boolean = with(ktFileBuilder) { + val roundedShapeToken = token as? RoundedShapeToken ?: return@with false + val tokenValue = roundedShapeToken.value ?: return@with false val value = "${tokenValue.cornerRadius}.dp" val initializer = KtFileBuilder.createConstructorCall( @@ -113,6 +104,6 @@ internal class ShapeGenerator( "CornerSize($value)", ) rootRoundShapes.appendProperty(token.ktName, KtFileBuilder.TypeRoundRectShape, initializer, token.description) - if (!needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true + return@with true } } diff --git a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/TypographyGenerator.kt b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/TypographyGenerator.kt index 133927b1..732d2ee7 100644 --- a/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/TypographyGenerator.kt +++ b/sdds-core/plugin_theme_builder/src/main/kotlin/com/sdds/plugin/themebuilder/internal/generator/TypographyGenerator.kt @@ -1,8 +1,6 @@ package com.sdds.plugin.themebuilder.internal.generator import com.sdds.plugin.themebuilder.ThemeBuilderTarget -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isComposeOrAll -import com.sdds.plugin.themebuilder.ThemeBuilderTarget.Companion.isViewSystemOrAll import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder.ElementName @@ -30,12 +28,12 @@ import java.io.File internal class TypographyGenerator( private val outputLocation: KtFileBuilder.OutputLocation, private val outputResDir: File, - private val target: ThemeBuilderTarget, + target: ThemeBuilderTarget, private val dimensAggregator: DimensAggregator, private val xmlBuilderFactory: XmlDocumentBuilderFactory, private val ktFileBuilderFactory: KtFileBuilderFactory, private val resourceReferenceProvider: ResourceReferenceProvider, -) : TokenGenerator { +) : TokenGenerator(target) { private val textAppearanceXmlBuilders = mutableMapOf() private val typographyXmlBuilder by unsafeLazy { xmlBuilderFactory.create() } @@ -43,44 +41,34 @@ internal class TypographyGenerator( private val largeBuilder by unsafeLazy { ktFileBuilder.rootObject("TypographyLargeTokens") } private val mediumBuilder by unsafeLazy { ktFileBuilder.rootObject("TypographyMediumTokens") } private val smallBuilder by unsafeLazy { ktFileBuilder.rootObject("TypographySmallTokens") } - private var needGenerateCompose: Boolean = false - private var needGenerateViewSystem: Boolean = false private var needDeclareStyle: Boolean = true /** - * @see TokenGenerator.addToken + * @see TokenGenerator.generateViewSystem */ - override fun addToken(token: TypographyToken) { - when (target) { - ThemeBuilderTarget.VIEW_SYSTEM -> addViewSystemToken(token) - ThemeBuilderTarget.COMPOSE -> addComposeToken(token) - ThemeBuilderTarget.ALL -> { - addViewSystemToken(token) - addComposeToken(token) - } + override fun generateViewSystem() { + super.generateViewSystem() + textAppearanceXmlBuilders.forEach { + it.value.build(outputResDir.textAppearancesXmlFile(it.key.qualifier())) } + typographyXmlBuilder.build(outputResDir.typographyXmlFile()) } /** - * @see TokenGenerator.generate + * @see TokenGenerator.generateCompose */ - override fun generate() { - if (needGenerateViewSystem) { - textAppearanceXmlBuilders.forEach { - it.value.build(outputResDir.textAppearancesXmlFile(it.key.qualifier())) - } - typographyXmlBuilder.build(outputResDir.typographyXmlFile()) - } - - if (needGenerateCompose) { - ktFileBuilder.addImport(KtFileBuilder.TypeSp) - ktFileBuilder.addImport(KtFileBuilder.TypeFontWeight) - ktFileBuilder.build(outputLocation) - } + override fun generateCompose() { + super.generateCompose() + ktFileBuilder.addImport(KtFileBuilder.TypeSp) + ktFileBuilder.addImport(KtFileBuilder.TypeFontWeight) + ktFileBuilder.build(outputLocation) } - private fun addViewSystemToken(token: TypographyToken) { - val tokenValue = token.value ?: return + /** + * @see TokenGenerator.addViewSystemToken + */ + override fun addViewSystemToken(token: TypographyToken): Boolean { + val tokenValue = token.value ?: return false val builder = textAppearanceXmlBuilders[token.screenClass] ?: xmlBuilderFactory.create().also { textAppearanceXmlBuilders[token.screenClass] = it } @@ -106,12 +94,14 @@ internal class TypographyGenerator( } builder.appendComment(token.description) builder.appendTypographyToken(token, tokenValue, textAppearanceName) - - if (!needGenerateViewSystem && target.isViewSystemOrAll) needGenerateViewSystem = true + return true } - private fun addComposeToken(token: TypographyToken) = with(ktFileBuilder) { - val tokenValue = token.value ?: return + /** + * @see TokenGenerator.addComposeToken + */ + override fun addComposeToken(token: TypographyToken): Boolean = with(ktFileBuilder) { + val tokenValue = token.value ?: return@with false when (token.screenClass) { TypographyToken.ScreenClass.SMALL -> smallBuilder.addTypographyToken( token.ktName, @@ -127,7 +117,7 @@ internal class TypographyGenerator( else -> mediumBuilder.addTypographyToken(token.ktName, token.description, tokenValue) } - if (!needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true + return@with true } private fun XmlDocumentBuilder.appendTypographyToken(