Skip to content

Commit

Permalink
Generates Sequences and order emojis by unicode list
Browse files Browse the repository at this point in the history
  • Loading branch information
SalomonBrys committed May 26, 2024
1 parent 060b1bd commit ab439bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
6 changes: 5 additions & 1 deletion buildSrc/src/main/kotlin/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@ fun annotate(grouppedForms: GrouppedForms, notoJsonFile: File): List<AnnotatedFo
)
}

return annotatedForms
check(annotatedForms.size == grouppedForms.size)

return grouppedForms.map { forms ->
annotatedForms.first { it.mainForm in forms }
}
}
50 changes: 40 additions & 10 deletions buildSrc/src/main/kotlin/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,23 @@ internal fun genCollections(outputDir: File, groups: AnnotatedFormTree) {
}
writer.appendLine("}")
writer.appendLine()
writer.appendLine("internal fun MutableList<Emoji>.addAll$subgroupPCId() {")
writer.appendLine("internal suspend fun SequenceScope<Emoji>.yieldAll$subgroupPCId() {")
annotatedForms.forEach { annotatedForm ->
val id = annotatedForm.mainForm.entry.description.asEmojiId()
writer.appendLine(" add(_$id)")
writer.appendLine(" yield(_$id)")
}
writer.appendLine("}")
writer.appendLine()
writer.appendLine("internal val count$subgroupPCId = ${annotatedForms.size}")
writer.appendLine()
writer.appendLine("""
/**
* All Emoji of the ${annotatedForms.first().mainForm.entry.group}: ${annotatedForms.first().mainForm.entry.subgroup} subgroup.
*/
""".trimIndent())
writer.appendLine("public fun Emoji.Companion.sequence$subgroupPCId(): Sequence<Emoji> =")
writer.appendLine(" sequence { yieldAll$subgroupPCId() }")
writer.appendLine()
writer.appendLine("""
/**
* All Emoji of the ${annotatedForms.first().mainForm.entry.group}: ${annotatedForms.first().mainForm.entry.subgroup} subgroup.
Expand All @@ -63,8 +73,12 @@ internal fun genCollections(outputDir: File, groups: AnnotatedFormTree) {
* This method should be called in background and its result should be cached.
*/
""".trimIndent())
writer.appendLine("public fun Emoji.Companion.list$subgroupPCId(): List<Emoji> =")
writer.appendLine(" ArrayList<Emoji>(count$subgroupPCId).also { list -> sequence$subgroupPCId().forEach { list.add(it) } }")
writer.appendLine()
writer.appendLine("@Deprecated(\"Renamed list$subgroupPCId.\", replaceWith = ReplaceWith(\"list$subgroupPCId()\"), level = DeprecationLevel.WARNING)")
writer.appendLine("public fun Emoji.Companion.all$subgroupPCId(): List<Emoji> =")
writer.appendLine(" ArrayList<Emoji>(${annotatedForms.size}).apply { addAll$subgroupPCId() }")
writer.appendLine(" list$subgroupPCId()")
}
}
val groupPCId = groupId.pascalCase()
Expand All @@ -84,12 +98,22 @@ internal fun genCollections(outputDir: File, groups: AnnotatedFormTree) {
}
writer.appendLine("}")
writer.appendLine()
writer.appendLine("internal fun MutableList<Emoji>.addAll$groupPCId() {")
writer.appendLine("internal suspend fun SequenceScope<Emoji>.yieldAll$groupPCId() {")
subGroups.keys.forEach { subgroupId ->
writer.appendLine(" addAll${subgroupId.pascalCase()}()")
writer.appendLine(" yieldAll${subgroupId.pascalCase()}()")
}
writer.appendLine("}")
writer.appendLine()
writer.appendLine("internal val count$groupPCId = ${subGroups.values.sumOf { it.size }}")
writer.appendLine()
writer.appendLine("""
/**
* All Emoji of the ${subGroups.values.first().first().mainForm.entry.group} group.
*/
""".trimIndent())
writer.appendLine("public fun Emoji.Companion.sequence$groupPCId(): Sequence<Emoji> =")
writer.appendLine(" sequence { yieldAll$groupPCId() }")
writer.appendLine()
writer.appendLine("""
/**
* All Emoji of the ${subGroups.values.first().first().mainForm.entry.group} group.
Expand All @@ -99,12 +123,12 @@ internal fun genCollections(outputDir: File, groups: AnnotatedFormTree) {
*/
""".trimIndent())
writer.appendLine("public fun Emoji.Companion.all$groupPCId(): List<Emoji> =")
writer.appendLine(" ArrayList<Emoji>(${subGroups.values.sumOf { it.size }}).apply { addAll$groupPCId() }")
writer.appendLine(" ArrayList<Emoji>(count$groupPCId).also { list -> sequence$groupPCId().forEach { list.add(it) } }")
writer.appendLine()
writer.appendLine("internal fun all${groupPCId}Subgroups(): Map<String, () -> List<Emoji>> =")
writer.appendLine(" mapOf(")
subGroups.keys.forEach { subgroupId ->
writer.appendLine(" \"$subgroupId\" to { Emoji.all${subgroupId.pascalCase()}() },")
writer.appendLine(" \"$subgroupId\" to { Emoji.list${subgroupId.pascalCase()}() },")
}
writer.appendLine(" )")
}
Expand Down Expand Up @@ -147,10 +171,10 @@ internal fun genCollections(outputDir: File, groups: AnnotatedFormTree) {
* This method should be called in background and its result should be cached.
*/
""".trimIndent())
writer.appendLine("public fun Emoji.Companion.all(): List<Emoji> =")
writer.appendLine(" ArrayList<Emoji>(emojiCount).apply {")
writer.appendLine("public fun Emoji.Companion.sequence(): Sequence<Emoji> =")
writer.appendLine(" sequence {")
groups.keys.forEach { groupId ->
writer.appendLine(" addAll${groupId.pascalCase()}()")
writer.appendLine(" yieldAll${groupId.pascalCase()}()")
}
writer.appendLine(" }")
writer.appendLine()
Expand All @@ -160,5 +184,11 @@ internal fun genCollections(outputDir: File, groups: AnnotatedFormTree) {
writer.appendLine(" \"$groupId\" to all${groupId.pascalCase()}Subgroups(),")
}
writer.appendLine(" )")
writer.appendLine()
writer.appendLine("public fun Emoji.Companion.list(): List<Emoji> =")
writer.appendLine(" ArrayList<Emoji>(emojiCount).also { list -> sequence().forEach { list.add(it) } }")
writer.appendLine()
writer.appendLine("@Deprecated(\"Renamed list.\", replaceWith = ReplaceWith(\"list()\"), level = DeprecationLevel.WARNING)")
writer.appendLine("public fun Emoji.Companion.all(): List<Emoji> = list()")
}
}
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/EmojiFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ private fun skinToneIntIndices2CharIndices(code: List<Int>, intIndices: List<Int
internal typealias AnnotatedFormTree = Map<String, Map<String, List<AnnotatedForm>>>

internal fun genEmojiFiles(outputDir: File, annotatedForms: List<AnnotatedForm>): AnnotatedFormTree {
val ids = HashMap<String, HashMap<String, ArrayList<AnnotatedForm>>>()
val ids = LinkedHashMap<String, LinkedHashMap<String, ArrayList<AnnotatedForm>>>()
annotatedForms
.forEach { annotatedForm ->
val groupId = annotatedForm.mainForm.entry.group.snakeCase()
val subgroupId = annotatedForm.mainForm.entry.subgroup.snakeCase()
val dir = outputDir.resolve(groupId).resolve(subgroupId)
dir.mkdirs()
val id = annotatedForm.mainForm.entry.description.asEmojiId()
ids.getOrPut(groupId) { HashMap() }.getOrPut(subgroupId) { ArrayList() }.add(annotatedForm)
ids.getOrPut(groupId) { LinkedHashMap() }.getOrPut(subgroupId) { ArrayList() }.add(annotatedForm)
val doubleSkinToneZWJ = annotatedForm.mainForm.doubleSkinToneZWJs["minimally-qualified"] ?: annotatedForm.mainForm.doubleSkinToneZWJs["fully-qualified"]
val unqualifiedForm = annotatedForm.altForms.firstOrNull { it.entry.type == "unqualified" }
val (itf, impl) = when {
Expand Down
9 changes: 1 addition & 8 deletions buildSrc/src/main/kotlin/GenEmojis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ abstract class GenEmojis : DefaultTask() {
genDirectory.convention(project.layout.buildDirectory.dir("gen/emoji"))
}

private val skinTones = listOf(
0x1F3FB to "Light",
0x1F3FC to "MediumLight",
0x1F3FD to "Medium",
0x1F3FE to "MediumDark",
0x1F3FF to "Dark"
)

// Generates emojis


Expand All @@ -48,6 +40,7 @@ abstract class GenEmojis : DefaultTask() {
outputDir.mkdirs()

val tree = genEmojiFiles(outputDir, annotatedForms)

genCollections(outputDir, tree)
}

Expand Down

0 comments on commit ab439bb

Please sign in to comment.