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

Introduce flat package option to export icons in single folder #191

Merged
merged 1 commit into from
Oct 2, 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
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