Skip to content

Commit

Permalink
Allow to export icons in flat package
Browse files Browse the repository at this point in the history
  • Loading branch information
egorikftp committed Oct 2, 2024
1 parent bc5ee85 commit 107c919
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal data class ImageVectorSpecConfig(
val iconPackage: String,
val outputFormat: OutputFormat,
val generatePreview: Boolean,
val useFlatPackage: Boolean,
)

internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class ImageVectorGeneratorConfig(
val nestedPackName: String,
val outputFormat: OutputFormat,
val generatePreview: Boolean,
val useFlatPackage: Boolean,
)

enum class OutputFormat(val key: String) {
Expand Down Expand Up @@ -46,6 +47,7 @@ object ImageVectorGenerator {
iconNestedPack = config.nestedPackName,
outputFormat = config.outputFormat,
generatePreview = config.generatePreview,
useFlatPackage = config.useFlatPackage,
),
).createFileFor(vector)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import io.github.composegears.valkyrie.ir.IrVectorNode

internal fun ImageVectorSpecConfig.resolvePackageName(): String = when {
iconNestedPack.isEmpty() -> iconPackage
else -> "$iconPackage.${iconNestedPack.lowercase()}"
else -> {
if (useFlatPackage) {
iconPackage
} else {
"$iconPackage.${iconNestedPack.lowercase()}"
}
}
}

internal fun ImageVectorSpecConfig.resolveIconPackClassName() = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.github.composegears.valkyrie.generator.imagevector

import assertk.assertThat
import assertk.assertions.isEqualTo
import io.github.composegears.valkyrie.extensions.ResourceUtils.getResourcePath
import io.github.composegears.valkyrie.generator.imagevector.common.toResourceText
import io.github.composegears.valkyrie.parser.svgxml.SvgXmlParser
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource

class CustomPackageTest {

@ParameterizedTest
@EnumSource(value = OutputFormat::class)
fun `flat package without icon pack`(outputFormat: OutputFormat) {
val icon = getResourcePath("xml/ic_flat_package.xml")
val parserOutput = SvgXmlParser.toIrImageVector(icon)
val output = ImageVectorGenerator.convert(
vector = parserOutput.vector,
kotlinName = parserOutput.kotlinName,
config = ImageVectorGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
packName = "",
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = true,
),
).content

val expected = outputFormat.toResourceText(
pathToBackingProperty = "kt/backing/FlatPackage.kt",
pathToLazyProperty = "kt/lazy/FlatPackage.kt",
)
assertThat(output).isEqualTo(expected)
}

@ParameterizedTest
@EnumSource(value = OutputFormat::class)
fun `flat package with icon pack`(outputFormat: OutputFormat) {
val icon = getResourcePath("xml/ic_flat_package.xml")
val parserOutput = SvgXmlParser.toIrImageVector(icon)
val output = ImageVectorGenerator.convert(
vector = parserOutput.vector,
kotlinName = parserOutput.kotlinName,
config = ImageVectorGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
packName = "ValkyrieIcons",
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = true,
),
).content

val expected = outputFormat.toResourceText(
pathToBackingProperty = "kt/backing/FlatPackage.pack.kt",
pathToLazyProperty = "kt/lazy/FlatPackage.pack.kt",
)
assertThat(output).isEqualTo(expected)
}

@ParameterizedTest
@EnumSource(value = OutputFormat::class)
fun `flat package with nested icon pack`(outputFormat: OutputFormat) {
val icon = getResourcePath("xml/ic_flat_package.xml")
val parserOutput = SvgXmlParser.toIrImageVector(icon)
val output = ImageVectorGenerator.convert(
vector = parserOutput.vector,
kotlinName = parserOutput.kotlinName,
config = ImageVectorGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
packName = "ValkyrieIcons",
nestedPackName = "Filled",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = true,
),
).content

val expected = outputFormat.toResourceText(
pathToBackingProperty = "kt/backing/FlatPackage.pack.nested.kt",
pathToLazyProperty = "kt/lazy/FlatPackage.pack.nested.kt",
)
assertThat(output).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ImageVectorWithPreviewTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = true,
useFlatPackage = false,
),
).content

Expand All @@ -48,6 +49,7 @@ class ImageVectorWithPreviewTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = true,
useFlatPackage = false,
),
).content

Expand All @@ -72,6 +74,7 @@ class ImageVectorWithPreviewTest {
nestedPackName = "Filled",
outputFormat = outputFormat,
generatePreview = true,
useFlatPackage = false,
),
).content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SvgWithGradientToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -48,6 +49,7 @@ class SvgWithGradientToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -72,6 +74,7 @@ class SvgWithGradientToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -48,6 +49,7 @@ class XmlToImageVectorTest {
nestedPackName = "Colored",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -72,6 +74,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -96,6 +99,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -120,6 +124,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -144,6 +149,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -168,6 +174,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -192,6 +199,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -216,6 +224,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand All @@ -240,6 +249,7 @@ class XmlToImageVectorTest {
nestedPackName = "",
outputFormat = outputFormat,
generatePreview = false,
useFlatPackage = false,
),
).content

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.composegears.valkyrie.icons

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlin.Suppress

val FlatPackage: ImageVector
get() {
if (_FlatPackage != null) {
return _FlatPackage!!
}
_FlatPackage = ImageVector.Builder(
name = "FlatPackage",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 18f,
viewportHeight = 18f
).build()

return _FlatPackage!!
}

@Suppress("ObjectPropertyName")
private var _FlatPackage: ImageVector? = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.composegears.valkyrie.icons

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlin.Suppress

val ValkyrieIcons.FlatPackage: ImageVector
get() {
if (_FlatPackage != null) {
return _FlatPackage!!
}
_FlatPackage = ImageVector.Builder(
name = "FlatPackage",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 18f,
viewportHeight = 18f
).build()

return _FlatPackage!!
}

@Suppress("ObjectPropertyName")
private var _FlatPackage: ImageVector? = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.composegears.valkyrie.icons

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlin.Suppress

val ValkyrieIcons.Filled.FlatPackage: ImageVector
get() {
if (_FlatPackage != null) {
return _FlatPackage!!
}
_FlatPackage = ImageVector.Builder(
name = "Filled.FlatPackage",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 18f,
viewportHeight = 18f
).build()

return _FlatPackage!!
}

@Suppress("ObjectPropertyName")
private var _FlatPackage: ImageVector? = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.composegears.valkyrie.icons

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlin.LazyThreadSafetyMode

val FlatPackage: ImageVector by lazy(LazyThreadSafetyMode.NONE) {
ImageVector.Builder(
name = "FlatPackage",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 18f,
viewportHeight = 18f
).build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.composegears.valkyrie.icons

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlin.LazyThreadSafetyMode

val ValkyrieIcons.FlatPackage: ImageVector by lazy(LazyThreadSafetyMode.NONE) {
ImageVector.Builder(
name = "FlatPackage",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 18f,
viewportHeight = 18f
).build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.composegears.valkyrie.icons

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlin.LazyThreadSafetyMode

val ValkyrieIcons.Filled.FlatPackage: ImageVector by lazy(LazyThreadSafetyMode.NONE) {
ImageVector.Builder(
name = "Filled.FlatPackage",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 18f,
viewportHeight = 18f
).build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="18"
android:viewportHeight="18">
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PersistentSettings : SimplePersistentStateComponent<ValkyrieState>(Valkyri

var generatePreview: Boolean by property(false)
var outputFormat: String? by string()
var flatPackage: Boolean by property(false)

var showImageVectorPreview: Boolean by property(true)
}
Expand Down
Loading

0 comments on commit 107c919

Please sign in to comment.