Skip to content

Refactor icon pack generator to use iconpack model for nested packs #379

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

Merged
merged 1 commit into from
Mar 9, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.github.composegears.valkyrie.cli.ext.stringOption
import io.github.composegears.valkyrie.extensions.writeToKt
import io.github.composegears.valkyrie.generator.iconpack.IconPackGenerator
import io.github.composegears.valkyrie.generator.iconpack.IconPackGeneratorConfig
import io.github.composegears.valkyrie.generator.model.IconPack
import java.nio.file.Path
import kotlin.io.path.absolutePathString

Expand Down Expand Up @@ -91,8 +92,10 @@ private fun generateIconPack(
val iconPack = IconPackGenerator.create(
config = IconPackGeneratorConfig(
packageName = packageName,
iconPackName = iconPackName,
nestedPacks = nestedPacks,
iconPack = IconPack(
name = iconPackName,
nested = nestedPacks.map(::IconPack),
),
useExplicitMode = useExplicitMode,
indentSize = indentSize,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class IconPackCliTest {
fun `generate nested packs`(cliTestType: CliTestType) {
testIconPack(
cliTestType = cliTestType,
expectedResource = "iconpack/IconPack.nested.kt",
expectedResource = "iconpack/IconPack.nested.L2.kt",
nestedPacks = NestedPacks(listOf("Filled", "Colored")),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.composegears.valkyrie.generator.model

data class IconPack(
val name: String,
val nested: List<IconPack> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.composegears.valkyrie.generator.model

@DslMarker
annotation class IconPackDsl

@IconPackDsl
fun iconpack(name: String, init: PackBuilder.() -> Unit = {}): IconPack {
val builder = PackBuilder(name)
builder.init()
return builder.build()
}

@IconPackDsl
class PackBuilder(private val name: String) {
private val nestedPacks = mutableListOf<IconPack>()

fun pack(name: String, init: PackBuilder.() -> Unit = {}) {
val builder = PackBuilder(name)
builder.init()
nestedPacks.add(builder.build())
}

internal fun build(): IconPack = IconPack(name, nestedPacks)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.composegears.valkyrie.generator.model

import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import org.junit.jupiter.api.Test

class IconPackDslTest {

@Test
fun `simple iconpack`() {
val iconPack = iconpack(name = "ValkyrieIcons")

assertThat(iconPack.name).isEqualTo("ValkyrieIcons")
assertThat(iconPack.nested).hasSize(0)
}

@Test
fun `pack with one level of nesting`() {
val iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Outlined")
pack(name = "Filled")
}

assertThat(iconPack.name).isEqualTo("ValkyrieIcons")
assertThat(iconPack.nested).hasSize(2)
assertThat(iconPack.nested[0].name).isEqualTo("Outlined")
assertThat(iconPack.nested[1].name).isEqualTo("Filled")
}

@Test
fun `iconpack with multiple nested levels`() {
val iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Material") {
pack(name = "Rounded") {
pack(name = "Filled")
}
pack(name = "Sharp") {
pack(name = "Outlined")
}
}
pack(name = "Custom") {
pack(name = "Brand")
}
}

assertThat(iconPack.name).isEqualTo("ValkyrieIcons")
assertThat(iconPack.nested).hasSize(2)

val material = iconPack.nested[0]
assertThat(material.name).isEqualTo("Material")
assertThat(material.nested).hasSize(2)

val rounded = material.nested[0]
assertThat(rounded.name).isEqualTo("Rounded")
assertThat(rounded.nested).hasSize(1)
assertThat(rounded.nested[0].name).isEqualTo("Filled")

val sharp = material.nested[1]
assertThat(sharp.name).isEqualTo("Sharp")
assertThat(sharp.nested).hasSize(1)
assertThat(sharp.nested[0].name).isEqualTo("Outlined")

val custom = iconPack.nested[1]
assertThat(custom.name).isEqualTo("Custom")
assertThat(custom.nested).hasSize(1)
assertThat(custom.nested[0].name).isEqualTo("Brand")
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package io.github.composegears.valkyrie.generator.iconpack

import com.squareup.kotlinpoet.TypeSpec
import io.github.composegears.valkyrie.generator.ext.fileSpecBuilder
import io.github.composegears.valkyrie.generator.ext.objectBuilder
import io.github.composegears.valkyrie.generator.ext.removeExplicitModeCode
import io.github.composegears.valkyrie.generator.ext.setIndent
import io.github.composegears.valkyrie.generator.model.IconPack

internal class IconPackFileSpec(private val config: IconPackGeneratorConfig) {

fun createSpec(): IconPackSpecOutput {
val iconPackSpec = objectBuilder(name = config.iconPackName) {
config.nestedPacks.forEach { icon ->
addType(objectBuilder(name = icon))
val iconPackName = config.iconPack.name

val iconPackSpec = objectBuilder(name = iconPackName) {
config.iconPack.nested.forEach { pack ->
addType(createNestedObjectSpec(pack))
}
}
val fileSpec = fileSpecBuilder(
packageName = config.packageName,
fileName = config.iconPackName,
fileName = iconPackName,
) {
addType(iconPackSpec)
setIndent(config.indentSize)
Expand All @@ -28,4 +32,12 @@ internal class IconPackFileSpec(private val config: IconPackGeneratorConfig) {
name = fileSpec.name,
)
}

private fun createNestedObjectSpec(pack: IconPack): TypeSpec {
return objectBuilder(name = pack.name) {
pack.nested.forEach { nestedPack ->
addType(createNestedObjectSpec(nestedPack))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.composegears.valkyrie.generator.iconpack

import io.github.composegears.valkyrie.generator.model.IconPack

data class IconPackGeneratorConfig(
val packageName: String,
val iconPackName: String,
val nestedPacks: List<String>,
val iconPack: IconPack,
val useExplicitMode: Boolean,
val indentSize: Int,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package io.github.composegears.valkyrie.generator.iconpack
import assertk.assertThat
import assertk.assertions.isEqualTo
import io.github.composegears.valkyrie.extensions.ResourceUtils.getResourceText
import io.github.composegears.valkyrie.generator.model.iconpack
import org.junit.jupiter.api.Test

class IcoPackWithIndentTest {

private fun createConfig(
nestedPacks: List<String> = listOf("Filled", "Colored"),
useExplicitMode: Boolean = false,
indentSize: Int = 4,
) = IconPackGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
iconPackName = "ValkyrieIcons",
nestedPacks = nestedPacks,
iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Filled")
pack(name = "Colored")
},
useExplicitMode = useExplicitMode,
indentSize = indentSize,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package io.github.composegears.valkyrie.generator.iconpack
import assertk.assertThat
import assertk.assertions.isEqualTo
import io.github.composegears.valkyrie.extensions.ResourceUtils.getResourceText
import io.github.composegears.valkyrie.generator.model.IconPack
import io.github.composegears.valkyrie.generator.model.iconpack
import org.junit.jupiter.api.Test

class IconPackGeneratorTest {

private fun createConfig(
nestedPacks: List<String> = emptyList(),
iconPack: IconPack = iconpack(name = "ValkyrieIcons"),
useExplicitMode: Boolean = false,
indentSize: Int = 4,
) = IconPackGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
iconPackName = "ValkyrieIcons",
nestedPacks = nestedPacks,
iconPack = iconPack,
useExplicitMode = useExplicitMode,
indentSize = indentSize,
)
Expand Down Expand Up @@ -42,12 +43,63 @@ class IconPackGeneratorTest {
}

@Test
fun `generate nested packs`() {
fun `generate nested pack level 2`() {
val result = IconPackGenerator.create(
config = createConfig(nestedPacks = listOf("Filled", "Colored")),
config = createConfig(
iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Filled")
pack(name = "Colored")
},
),
)

val expected = getResourceText("iconpack/IconPack.nested.L2.kt")

assertThat(result.content).isEqualTo(expected)
assertThat(result.name).isEqualTo("ValkyrieIcons")
}

@Test
fun `generate nested pack level 3`() {
val result = IconPackGenerator.create(
config = createConfig(
iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Rounded") {
pack(name = "Filled")
}
pack(name = "Sharp") {
pack(name = "Colored")
pack(name = "Dark")
}
},
),
)

val expected = getResourceText("iconpack/IconPack.nested.L3.kt")

assertThat(result.content).isEqualTo(expected)
assertThat(result.name).isEqualTo("ValkyrieIcons")
}

@Test
fun `generate nested pack level 4`() {
val result = IconPackGenerator.create(
config = createConfig(
iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Material") {
pack(name = "Rounded") {
pack(name = "Filled")
pack(name = "Outlined")
}
}
pack(name = "Custom") {
pack(name = "Brand")
}
},
),
)

val expected = getResourceText("iconpack/IconPack.nested.kt")
val expected = getResourceText("iconpack/IconPack.nested.L4.kt")

assertThat(result.content).isEqualTo(expected)
assertThat(result.name).isEqualTo("ValkyrieIcons")
Expand All @@ -57,7 +109,10 @@ class IconPackGeneratorTest {
fun `generate nested packs explicit`() {
val result = IconPackGenerator.create(
config = createConfig(
nestedPacks = listOf("Filled", "Colored"),
iconPack = iconpack(name = "ValkyrieIcons") {
pack(name = "Filled")
pack(name = "Colored")
},
useExplicitMode = true,
),
)
Expand Down
2 changes: 1 addition & 1 deletion components/generator/imagevector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sourceSets {

dependencies {
implementation(projects.components.extensions)
implementation(projects.components.generator.common)
api(projects.components.generator.common)
implementation(projects.components.ir)

implementation(libs.kotlinpoet)
Expand Down
13 changes: 13 additions & 0 deletions components/sharedTestResources/iconpack/IconPack.nested.L3.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.composegears.valkyrie.icons

object ValkyrieIcons {
object Rounded {
object Filled
}

object Sharp {
object Colored

object Dark
}
}
15 changes: 15 additions & 0 deletions components/sharedTestResources/iconpack/IconPack.nested.L4.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.composegears.valkyrie.icons

object ValkyrieIcons {
object Material {
object Rounded {
object Filled

object Outlined
}
}

object Custom {
object Brand
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.vfs.VirtualFileManager
import io.github.composegears.valkyrie.extensions.writeToKt
import io.github.composegears.valkyrie.generator.iconpack.IconPackGenerator
import io.github.composegears.valkyrie.generator.iconpack.IconPackGeneratorConfig
import io.github.composegears.valkyrie.generator.model.IconPack
import io.github.composegears.valkyrie.settings.InMemorySettings
import io.github.composegears.valkyrie.settings.updateNestedPack
import io.github.composegears.valkyrie.ui.domain.model.Mode
Expand All @@ -31,8 +32,10 @@ object IconPackWriter {
val iconPack = IconPackGenerator.create(
config = IconPackGeneratorConfig(
packageName = currentSettings.packageName,
iconPackName = currentSettings.iconPackName,
nestedPacks = currentSettings.nestedPacks,
iconPack = IconPack(
name = currentSettings.iconPackName,
nested = currentSettings.nestedPacks.map(::IconPack),
),
useExplicitMode = currentSettings.useExplicitMode,
indentSize = currentSettings.indentSize,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.project.Project
import io.github.composegears.valkyrie.extensions.safeAs
import io.github.composegears.valkyrie.generator.iconpack.IconPackGenerator
import io.github.composegears.valkyrie.generator.iconpack.IconPackGeneratorConfig
import io.github.composegears.valkyrie.generator.model.IconPack
import io.github.composegears.valkyrie.psi.iconpack.IconPackInfo
import io.github.composegears.valkyrie.psi.iconpack.IconPackPsiParser
import io.github.composegears.valkyrie.ui.di.DI
Expand Down Expand Up @@ -102,8 +103,10 @@ class ExistingPackViewModel : TiamatViewModel() {
val iconPackCode = IconPackGenerator.create(
config = IconPackGeneratorConfig(
packageName = inputFieldState.packageName.text,
iconPackName = inputFieldState.iconPackName.text,
nestedPacks = inputFieldState.nestedPacks.map { it.inputFieldState.text },
iconPack = IconPack(
name = inputFieldState.iconPackName.text,
nested = inputFieldState.nestedPacks.map { IconPack(it.inputFieldState.text) },
),
useExplicitMode = inMemorySettings.current.useExplicitMode,
indentSize = inMemorySettings.current.indentSize,
),
Expand Down
Loading