Skip to content

Commit

Permalink
feat(sdds-acore/theme-builder): Generators unit tests were added. KtF…
Browse files Browse the repository at this point in the history
…ileBuilder and resource prefixes were implemented.
  • Loading branch information
malilex committed Mar 15, 2024
1 parent 7aa954e commit d6edbb4
Show file tree
Hide file tree
Showing 47 changed files with 1,668 additions and 408 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ jobs:
path: |
**/build/outputs/apk/*/*.apk
**/build/outputs/aar/*.aar
- name: Upload test theme
uses: actions/upload-artifact@v2
with:
name: generated-test-theme.zip
path: |
playground/theme-builder/build/generated/theme-builder
playground/theme-builder/build/generated/theme-builder-res
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ kotlinPoet = "1.12.0"
plugin-androidCacheFix = "3.0.1"
plugin-gradleNexusPublish = "1.3.0"
plugin-gradlePluginPublish = "1.2.1"
material = "1.11.0"

[libraries]
base-androidX-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidX-appcompat" }
Expand Down Expand Up @@ -71,8 +70,6 @@ base-gradle-paparazzi = { module = "app.cash.paparazzi:paparazzi-gradle-plugin",
base-gradle-spotless= { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "staticAnalysis-spotless" }
base-gradle-cacheFix= { module = "org.gradle.android.cache-fix:org.gradle.android.cache-fix.gradle.plugin", version.ref = "plugin-androidCacheFix" }
base-gradle-nexusPublish= { module = "io.github.gradle-nexus:publish-plugin", version.ref = "plugin-gradleNexusPublish" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }


[plugins]
android-lib = { id = "com.android.library", version.ref = "global-gradle" }
Expand Down
4 changes: 3 additions & 1 deletion playground/theme-builder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ plugins {

android {
namespace = "com.sdds.playground.themebuilder"
resourcePrefix = "thmbldr"
}

configure<ThemeBuilderExtension> {
themeUrl.set("file://${projectDir.path}/new_theme_scheme.json")
target.set(ThemeBuilderTarget.COMPOSE.value)
target.set(ThemeBuilderTarget.ALL)
packageName.set("com.sdds.playground.themebuilder.tokens")
}

dependencies {
Expand Down
4 changes: 0 additions & 4 deletions playground/theme-builder/src/main/res/values/dimens.xml

This file was deleted.

4 changes: 1 addition & 3 deletions playground/theme-builder/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Sdds"/>

<style name="Sdds.My" parent="Sdds.TextAppearance.DisplayL"/>
<style name="Thmbldr" />
</resources>
139 changes: 0 additions & 139 deletions playground/theme-builder/theme_scheme.json

This file was deleted.

5 changes: 4 additions & 1 deletion sdds-core/plugin_theme_builder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ plugins {

afterEvaluate {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.languageVersion = "1.7"
kotlinOptions {
languageVersion = "1.7"
freeCompilerArgs = freeCompilerArgs + "-Xcontext-receivers"
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.sdds.plugin.themebuilder

import com.sdds.plugin.themebuilder.internal.dimens.DimensAggregator
import com.sdds.plugin.themebuilder.internal.factory.GeneratorFactory
import com.sdds.plugin.themebuilder.internal.factory.KtFileBuilderFactory
import com.sdds.plugin.themebuilder.internal.factory.XmlDocumentBuilderFactory
import com.sdds.plugin.themebuilder.internal.serializer.Serializer
import com.sdds.plugin.themebuilder.internal.token.ColorToken
import com.sdds.plugin.themebuilder.internal.token.LinearGradientToken
Expand All @@ -10,10 +12,14 @@ import com.sdds.plugin.themebuilder.internal.token.ShadowToken
import com.sdds.plugin.themebuilder.internal.token.SweepGradientToken
import com.sdds.plugin.themebuilder.internal.token.Theme
import com.sdds.plugin.themebuilder.internal.token.TypographyToken
import com.sdds.plugin.themebuilder.internal.utils.ResourceReferenceProvider
import com.sdds.plugin.themebuilder.internal.utils.unsafeLazy
import kotlinx.serialization.decodeFromString
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
Expand All @@ -30,6 +36,24 @@ abstract class GenerateThemeTask : DefaultTask() {
@get:InputFile
abstract val themeFile: RegularFileProperty

/**
* Название пакета для файлов kotlin
*/
@get:Input
abstract val packageName: Property<String>

/**
* Префикс для названий ресурсов токенов
*/
@get:Input
abstract val resourcesPrefix: Property<String>

/**
* Целевой фреймворк
*/
@get:Input
abstract val target: Property<ThemeBuilderTarget>

/**
* Директория для сохранения kt-файлов токенов
*/
Expand All @@ -42,20 +66,23 @@ abstract class GenerateThemeTask : DefaultTask() {
@get:OutputDirectory
abstract val outputResDir: DirectoryProperty

private val dimensAggregator by lazy { DimensAggregator() }
private val generatorFactory by lazy {
private val dimensAggregator by unsafeLazy { DimensAggregator() }
private val generatorFactory by unsafeLazy {
GeneratorFactory(
outputDir.get().asFile,
outputResDir.get().asFile,
ThemeBuilderTarget.ALL,
target.get(),
dimensAggregator,
XmlDocumentBuilderFactory(resourcesPrefix.get()),
KtFileBuilderFactory(packageName.get()),
ResourceReferenceProvider(resourcesPrefix.get()),
)
}

private val colorGenerator by lazy { generatorFactory.createColorGenerator() }
private val gradientGenerator by lazy { generatorFactory.createGradientGenerator() }
private val typographyGenerator by lazy { generatorFactory.createTypographyGenerator() }
private val dimensGenerator by lazy { generatorFactory.createDimensGenerator() }
private val colorGenerator by unsafeLazy { generatorFactory.createColorGenerator() }
private val gradientGenerator by unsafeLazy { generatorFactory.createGradientGenerator() }
private val typographyGenerator by unsafeLazy { generatorFactory.createTypographyGenerator() }
private val dimensGenerator by unsafeLazy { generatorFactory.createDimensGenerator() }

/**
* Генерирует файлы с токенами
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ interface ThemeBuilderExtension {
*/
val themeUrl: Property<String>

/**
* Название пакета для генерируемых файлов kotlin
*/
val packageName: Property<String>

/**
* Целевой фреймворк для которого будет сгенерирована тема.
* @see ThemeBuilderTarget
*/
val target: Property<String>
val target: Property<ThemeBuilderTarget>

/**
* Префикс для названий ресурсов токенов
*/
val resourcesPrefix: Property<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.internal.crash.afterEvaluate
import com.android.build.gradle.tasks.MergeResources
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
Expand All @@ -22,21 +24,24 @@ import org.gradle.kotlin.dsl.withType
*/
class ThemeBuilderPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create("theme-builder", ThemeBuilderExtension::class.java)
val themeOutputFile = project.layout.buildDirectory.file("theme-builder/theme.json")
val fetchThemeTask = project.registerThemeFetcher(extension, themeOutputFile)
val generateThemeTask = project.registerThemeGenerator(themeOutputFile, fetchThemeTask)

project.tasks.withType(MergeResources::class).configureEach {
dependsOn(generateThemeTask)
}

val extension = project.extensions.create("theme-builder", ThemeBuilderExtension::class.java)
project.plugins.all {
when (this) {
is AppPlugin -> project.extensions.getByType(AppExtension::class).configureSourceSets()
is LibraryPlugin -> project.extensions.getByType(LibraryExtension::class).configureSourceSets()
}
}

project.afterEvaluate {
extension.resourcesPrefix.convention(project.getDefaultResourcePrefix())
val fetchThemeTask = registerThemeFetcher(extension, themeOutputFile)
val generateThemeTask = registerThemeGenerator(extension, themeOutputFile, fetchThemeTask)

tasks.withType(MergeResources::class).configureEach {
dependsOn(generateThemeTask)
}
}
}

private fun Project.registerThemeFetcher(
Expand All @@ -51,11 +56,15 @@ class ThemeBuilderPlugin : Plugin<Project> {
}

private fun Project.registerThemeGenerator(
extension: ThemeBuilderExtension,
input: Provider<RegularFile>,
fetchTask: Any,
): TaskProvider<GenerateThemeTask> {
return project.tasks.register<GenerateThemeTask>("generateTheme") {
themeFile.set(input)
packageName.set(extension.packageName)
target.set(extension.target)
resourcesPrefix.set(extension.resourcesPrefix)
outputDir.set(project.layout.projectDirectory.dir(OUTPUT_PATH))
outputResDir.set(project.layout.projectDirectory.dir(OUTPUT_RESOURCE_PATH))
dependsOn(fetchTask)
Expand All @@ -67,6 +76,12 @@ class ThemeBuilderPlugin : Plugin<Project> {
this.sourceSets.maybeCreate("main").kotlin.srcDir(OUTPUT_PATH)
}

private fun Project.getDefaultResourcePrefix(): String {
val baseExtension = extensions.findByType<AppExtension>()
?: extensions.findByType<LibraryExtension>()
return baseExtension?.resourcePrefix.orEmpty()
}

private companion object {
const val OUTPUT_RESOURCE_PATH = "build/generated/theme-builder-res"
const val OUTPUT_PATH = "build/generated/theme-builder"
Expand Down
Loading

0 comments on commit d6edbb4

Please sign in to comment.