Skip to content

Commit

Permalink
feat(sdds-acore/theme-builder): Generators were refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
malilex committed Mar 19, 2024
1 parent ff6c67f commit 6ef9bdb
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 170 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -19,10 +22,66 @@ internal interface BaseGenerator {
* Интерфейс генератора токенов
* @author Малышев Александр on 07.03.2024
*/
internal interface TokenGenerator<T : Token<TokenValue>> : BaseGenerator {
internal abstract class TokenGenerator<T : Token<TokenValue>>(
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
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ColorToken> {
) : TokenGenerator<ColorToken>(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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,70 +30,63 @@ 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<Token<GradientTokenValue>> {
) : TokenGenerator<Token<GradientTokenValue>>(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<GradientTokenValue>) {
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<GradientTokenValue>) {
/**
* @see TokenGenerator.addViewSystemToken
*/
override fun addViewSystemToken(token: Token<GradientTokenValue>): 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<GradientTokenValue>) = with(ktFileBuilder) {
/**
* @see TokenGenerator.addComposeToken
*/
override fun addComposeToken(token: Token<GradientTokenValue>): 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)
is RadialGradientToken -> builder.appendRadialGradient(token)
is SweepGradientToken -> builder.appendSweepGradient(token)
else -> false
}
if (result && !needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true
return result
}

private fun XmlDocumentBuilder.appendBaseGradient(baseName: String, colors: List<String>, positions: List<Float>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,60 +23,53 @@ 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<ShadowToken> {
) : TokenGenerator<ShadowToken>(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")
appendElement(ElementName.DIMEN, "shadow_${token.xmlName}_dY", "${tokenValue.dY}dp")
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)
Expand All @@ -91,6 +82,6 @@ internal class ShadowGenerator(
token.description,
)
}
if (!needGenerateCompose && target.isComposeOrAll) needGenerateCompose = true
return@with true
}
}
Loading

0 comments on commit 6ef9bdb

Please sign in to comment.