Skip to content

Commit

Permalink
returning a ParsingResult for information of all icons created and th…
Browse files Browse the repository at this point in the history
…eir svg/drawable files
  • Loading branch information
DevSrSouza committed Mar 14, 2021
1 parent 457f048 commit f59556f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 51 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "br.com.devsrsouza"
version = "0.5.0"
version = "0.6.0-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy

class AllIconAccessorGenerator(
private val iconProperties: List<MemberName>,
private val iconProperties: Collection<MemberName>,
private val accessClass: ClassName,
private val allAssetsPropertyName: String,
private val childGroups: List<GeneratedGroup>
Expand Down Expand Up @@ -58,6 +58,6 @@ class AllIconAccessorGenerator(
}

fun groupAllIconsMember(group: GeneratedGroup): MemberName {
return MemberName(group.groupPackage, group.groupName)
return MemberName(group.groupPackage, group.groupName.second)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ package androidx.compose.material.icons.generator
data class Icon(
val kotlinName: String,
val xmlFileName: String,
val fileContent: String
val fileContent: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ typealias IconGroup = String
* @property icons the list of [Icon]s to generate Kotlin files for
*/
class IconWriter(
private val icons: List<Icon>,
private val icons: Collection<Icon>,
private val groupClass: ClassName,
private val groupPackage: String,
private val groupName: String,
private val iconNameTransformer: IconNameTransformer
) {
/**
* Generates icons and writes them to [outputSrcDirectory], using [iconNamePredicate] to
Expand All @@ -51,16 +49,16 @@ class IconWriter(
): List<MemberName> {

return icons.filter { icon ->
val iconName = icon.kotlinName.trim()
val iconName = icon.kotlinName

iconNamePredicate(iconName)
}.map { icon ->
val iconName = icon.kotlinName.trim()
val iconName = icon.kotlinName

val vector = IconParser(icon).parse()

val (fileSpec, accessProperty) = VectorAssetGenerator(
iconNameTransformer(iconName, groupName),
iconName,
groupPackage,
vector
).createFileSpec(groupClass)
Expand Down
107 changes: 66 additions & 41 deletions src/main/kotlin/br/com/devsrsouza/svg2compose/Svg2Compose.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package br.com.devsrsouza.svg2compose

import androidx.compose.material.icons.generator.*
import androidx.compose.material.icons.generator.toKotlinPropertyName
import com.android.ide.common.vectordrawable.Svg2Vector
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
Expand Down Expand Up @@ -29,7 +28,7 @@ object Svg2Compose {
type: VectorType = VectorType.SVG,
iconNameTransformer: IconNameTransformer = { it, _ -> it },
allAssetsPropertyName: String = "AllAssets"
) {
): ParsingResult {
fun nameRelative(vectorFile: File) = vectorFile.relativeTo(vectorsDirectory).path

val drawableDir = drawableTempDirectory()
Expand All @@ -40,14 +39,14 @@ object Svg2Compose {
.maxDepth(10)
.onEnter {
val dirIcons = it.listFiles()
.filter { !it.isDirectory }
.filter { it.isDirectory.not() }
.filter { it.extension.equals(type.extension, ignoreCase = true) }

val previousGroup = groupStack.peekOrNull()

// if there is no previous group, this is the root dir, and the group name should be the accessorName
val groupName = if(previousGroup == null) accessorName else it.name.toKotlinPropertyName()
val groupPackage = previousGroup?.let { group -> "${group.groupPackage}.${group.groupName.toLowerCase()}" }
val groupPackage = previousGroup?.let { group -> "${group.groupPackage}.${group.groupName.second.toLowerCase()}" }
?: "$applicationIconPackage"
val iconsPackage = "$groupPackage.${groupName.toLowerCase()}"

Expand All @@ -57,45 +56,51 @@ object Svg2Compose {
).createFileSpec(previousGroup?.groupClass)


val generatedIconsMemberNames = if(dirIcons.isNotEmpty()) {
val drawables: List<File> = when (type) {
VectorType.SVG -> dirIcons.map {
val iconName = nameRelative(it).withoutExtension

val parsedFile = File(drawableDir, "${iconName}.xml")
parsedFile.parentFile.mkdirs()
val generatedIconsMemberNames: Map<VectorFile, MemberName> =
if(dirIcons.isNotEmpty()) {
val drawables: List<Pair<File, File>> = when (type) {
VectorType.SVG -> dirIcons.map {
val iconName = nameRelative(it).withoutExtension

val parsedFile = File(drawableDir, "${iconName}.xml")
parsedFile.parentFile.mkdirs()

Svg2Vector.parseSvgToXml(it, parsedFile.outputStream())

it to parsedFile
}.toList()
VectorType.DRAWABLE -> dirIcons.toList().map { it to it }
}

val icons: Map<VectorFile, Icon> = drawables.associate { (vectorFile, drawableFile) ->
vectorFile to Icon(
iconNameTransformer(
drawableFile.nameWithoutExtension.toKotlinPropertyName().trim(),
groupName
),
drawableFile.name,
drawableFile.readText()
)
}

val writer = IconWriter(
icons.values,
groupClassName,
iconsPackage,
)

Svg2Vector.parseSvgToXml(it, parsedFile.outputStream())
val memberNames = writer.generateTo(outputSourceDirectory) { true }

parsedFile
}.toList()
VectorType.DRAWABLE -> dirIcons.toList()
}

val icons = drawables.map {
Icon(
it.nameWithoutExtension.toKotlinPropertyName(),
it.name,
it.readText()
)
icons.mapValues { entry ->
memberNames.first { it.simpleName == entry.value.kotlinName }
}
} else {
emptyMap<VectorFile, MemberName>()
}

val writer = IconWriter(
icons,
groupClassName,
iconsPackage,
groupName,
iconNameTransformer
)

writer.generateTo(outputSourceDirectory) { true }
} else {
emptyList<MemberName>()
}

val result = GeneratedGroup(
groupPackage,
groupName,
it to groupName,
generatedIconsMemberNames,
groupClassName,
groupFileSpec,
Expand All @@ -112,10 +117,13 @@ object Svg2Compose {
true
}
.onLeave {
val group = groupStack.pop()
val group = if(groupStack.size > 1)
groupStack.pop()
else
groupStack.peek()

val allAssetsGenerator = AllIconAccessorGenerator(
group.generatedIconsMemberNames,
group.generatedIconsMemberNames.values,
group.groupClass,
allAssetsPropertyName,
group.childGroups
Expand All @@ -128,18 +136,35 @@ object Svg2Compose {
group.groupFileSpec.build().writeTo(outputSourceDirectory)
}
.toList() // consume, to onEnter and onLeave be triggered

return groupStack.pop().asParsingResult()
}

private fun drawableTempDirectory() = createTempDir(suffix = "svg2compose/")

private val String.withoutExtension get() = substringBeforeLast(".")
}

typealias GroupFolder = File
typealias VectorFile = File

data class GeneratedGroup(
val groupPackage: String,
val groupName: String,
val generatedIconsMemberNames: List<MemberName>,
val groupName: Pair<GroupFolder, String>,
val generatedIconsMemberNames: Map<VectorFile, MemberName>,
val groupClass: ClassName,
val groupFileSpec: FileSpec.Builder,
val childGroups: List<GeneratedGroup>
)

data class ParsingResult(
val groupName: Pair<GroupFolder, String>,
val generatedIconsMemberNames: Map<VectorFile, MemberName>,
val generatedGroups: List<ParsingResult>
)

private fun GeneratedGroup.asParsingResult(): ParsingResult = ParsingResult(
groupName = groupName,
generatedIconsMemberNames = generatedIconsMemberNames,
generatedGroups = childGroups.map { it.asParsingResult() },
)

0 comments on commit f59556f

Please sign in to comment.