Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdds-acore/theme-builder): Theme Builder #4

Merged
merged 13 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/android-code-quality-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- main
- develop
- feature/*
- release/*
- hotfix/*

concurrency:
# New commit on branch cancels running workflows of the same branch
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- develop
- feature/*

concurrency:
# New commit on branch cancels running workflows of the same branch
Expand Down Expand Up @@ -36,3 +37,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
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ internal class TodoPatternRule(config: Config = Config.empty) : Rule(config) {
}

private companion object {
const val issuePattern = "[A-Z]{2,}-\\d+"
val todoCommentPattern = Regex("//\\s*todo.*", RegexOption.IGNORE_CASE)
val todoPattern = Regex("// TODO: $issuePattern.*")
val todoPattern = Regex("// TODO: https://github.com/salute-developers/plasma-android/issues/\\d+")
const val MESSAGE = "Укажите issue, например: " +
"// TODO: https://github.com/salute-developers/plasma/issues/0"
}
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ test-robolectric = "4.9"
test-androidX-rules = "1.5.0"
test-androidX-runner = "1.5.2"

kotlinSerialization = "1.3.2"
kotlinPoet = "1.12.0"

plugin-androidCacheFix = "3.0.1"
plugin-gradleNexusPublish = "1.3.0"
plugin-gradlePluginPublish = "1.2.1"
Expand All @@ -40,6 +43,8 @@ base-androidX-compose-uiTooling = { module = "androidx.compose.ui:ui-tooling" }
base-androidX-compose-uiTooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }

base-kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "global-kotlin" }
base-kotlin-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinSerialization" }
base-kotlin-poet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinPoet"}

base-staticAnalysis-ktlint = { module = "com.pinterest:ktlint", version.ref = "staticAnalysis-ktlint" }
base-staticAnalysis-detekt-api = { module = "io.gitlab.arturbosch.detekt:detekt-api", version.ref = "staticAnalysis-detekt" }
Expand Down Expand Up @@ -75,6 +80,7 @@ android-lib = { id = "com.android.library", version.ref = "global-gradle" }
android-app = { id = "com.android.application", version.ref = "global-gradle" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "global-kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "global-kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "global-kotlin" }
paparazzi = { id = "app.cash.paparazzi", version.ref = "test-paparazzi" }
jgp = { id = "java-gradle-plugin" }
android-cache-fix = { id = "org.gradle.android.cache-fix", version.ref= "plugin-androidCacheFix" }
Expand Down
5 changes: 5 additions & 0 deletions playground/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ buildscript {
}

dependencies {
configurations.all {
resolutionStrategy {
force(libs.base.kotlin.serialization.json)
}
}
classpath(libs.base.gradle.android)
classpath(libs.base.gradle.kotlin)
classpath(libs.base.gradle.detekt)
Expand Down
23 changes: 23 additions & 0 deletions playground/theme-builder/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import com.sdds.plugin.themebuilder.ThemeBuilderExtension
import com.sdds.plugin.themebuilder.ThemeBuilderTarget

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
id("convention.android-lib")
id(libs.plugins.themebuilder.get().pluginId)
}

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

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

dependencies {
implementation("sdds-core:uikit")
implementation("sdds-core:uikit-compose")

implementation(libs.base.androidX.core)
implementation(libs.base.androidX.appcompat)

val composeBom = platform(libs.base.androidX.compose.bom)
implementation(composeBom)
implementation(libs.base.androidX.compose.foundation)
}
Binary file added playground/theme-builder/json/plasma_b2c.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions playground/theme-builder/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Thmbldr" />
</resources>
56 changes: 54 additions & 2 deletions sdds-core/plugin_theme_builder/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,59 @@
Плагин генерирует тему и токены на основе метаданных.

## Установка
TBD
<details>
<summary>Добавить зависимость в gradle</summary>

```groovy
[plugins]
themebuilder = { id = "TBD", version.ref = "TBD" }
```
</details>

<details>

<summary>Подключить плагин в build.gradle модуля</summary>

```kotlin
plugins {
id(libs.plugins.themebuilder.get().pluginId)
}
```
</details>
<br>

Далее в `build.gradle` необходимо сконфигурировать плагин через экстеншн `ThemeBuilderExtension`. В нем указать:

- `themeUrl`, по которому находится json схема темы
- `target` - `COMPOSE`|`VIEW_SYSTEM`|`ALL`
- `packageName` - пакет для темы Compose, если указан таргет `ALL` или `COMPOSE`
- `resourcesPrefix` - префикс для названий ресурсов, если таргет `ALL` или `VIEW_SYSTEM`. Можно не указывать, если он уже указан в android конфигурации (см. пример)

<details>
<summary>Пример</summary>

```kotlin
plugins {
...
id(libs.plugins.themebuilder.get().pluginId)
}

android {
namespace = "com.example.app.themebuilder"
resourcePrefix = "thmbldr"
}

configure<ThemeBuilderExtension> {
themeUrl.set("file://${projectDir.path}/new_theme_scheme.json")
target.set(ThemeBuilderTarget.ALL)
packageName.set("com.example.app.themebuilder.tokens")
}
```
</details>

## Использование
TBD
Запускаем `./gradlew :your_module:build`.

Плагин скачает json-схему темы по ссылке `themeUrl` и сгенерирует темы под таргеты, указанные в `target`. По пути `your_module/build/generated/` должны появиться папки:
- `your_module/build/generated/theme-builder` - тема для Compose
- `your_module/build/generated/theme-builder-res` - тема для ViewSystem
16 changes: 12 additions & 4 deletions sdds-core/plugin_theme_builder/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import utils.findPropertyOrDefault
import utils.versionInfo
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
`java-gradle-plugin`
`kotlin-dsl`
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.gradlePluginPublish)
id("convention.detekt")
id("convention.spotless")
}

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

group = "io.github.salute-developers"
Expand All @@ -40,7 +46,9 @@ publishing {
}

dependencies {
implementation(libs.base.kotlin.stdlib)
implementation(libs.base.kotlin.serialization.json)
implementation(libs.base.gradle.android)
implementation(libs.base.kotlin.poet)
testImplementation(libs.base.test.unit.jUnit)
testImplementation(libs.base.test.unit.mockk)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.sdds.plugin.themebuilder

import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.net.URL

/**
* Gradle-task для загрузки файла темы
* @author Малышев Александр on 04.03.2024
*/
internal abstract class FetchThemeTask : DefaultTask() {

/**
* Строка URL файла темы
*/
@get:Input
abstract val url: Property<String>

/**
* Местоположение архива с темой после загрузки
*/
@get:OutputFile
abstract val themeFile: RegularFileProperty

/**
* Загружает файл с темой
*/
@TaskAction
fun fetch() {
runCatching { URL(url.get()).readBytes() }
.onSuccess { themeContent ->
val themeJSON = themeFile.get().asFile
themeJSON.writeBytes(themeContent)
}.onFailure {
logger.error("Can't fetch theme", it)
throw it
}
}
}
Loading
Loading