some fixes

This commit is contained in:
InsanusMokrassar 2024-07-25 18:27:06 +06:00
parent 0dd8f41eb0
commit d94cd7ea94
7 changed files with 4303 additions and 2736 deletions

View File

@ -6,7 +6,7 @@ kt-coroutines = "1.8.1"
kslog = "1.3.4" kslog = "1.3.4"
jb-compose = "1.7.0-dev1721" jb-compose = "1.7.0-dev1742"
jb-exposed = "0.51.1" jb-exposed = "0.51.1"
jb-dokka = "1.9.20" jb-dokka = "1.9.20"
@ -21,7 +21,7 @@ koin = "3.5.6"
okio = "3.9.0" okio = "3.9.0"
ksp = "2.0.0-1.0.22" ksp = "2.0.20-Beta2-1.0.23"
kotlin-poet = "1.18.0" kotlin-poet = "1.18.0"
versions = "0.51.0" versions = "0.51.0"
@ -32,9 +32,9 @@ dexcount = "4.0.0"
android-coreKtx = "1.13.1" android-coreKtx = "1.13.1"
android-recyclerView = "1.3.2" android-recyclerView = "1.3.2"
android-appCompat = "1.7.0" android-appCompat = "1.7.0"
android-fragment = "1.7.1" android-fragment = "1.8.1"
android-espresso = "3.5.1" android-espresso = "3.6.1"
android-test = "1.1.5" android-test = "1.2.1"
android-compose-material3 = "1.2.1" android-compose-material3 = "1.2.1"
android-props-minSdk = "21" android-props-minSdk = "21"

File diff suppressed because one or more lines are too long

View File

@ -22,17 +22,24 @@ private const val baseClassSerializerAnnotationName = "@Serializable(${baseClass
@Serializable @Serializable
private data class LanguageCode( private data class LanguageCode(
@SerialName("alpha2")
val tag: String,
@SerialName("English") @SerialName("English")
val title: String val title: String,
) @SerialName("alpha2")
val alpha: String? = null,
@SerialName("alpha3-b")
val alpha2: String? = null,
@SerialName("alpha3-t")
val alpha3: String? = null,
) {
val tag: String
get() = alpha ?: alpha2 ?: alpha3!!
}
fun String.adaptAsTitle() = if (first().isDigit()) { fun String.adaptAsTitle() = (if (first().isDigit()) {
"L$this" "L$this"
} else { } else {
this this
} }).replace(".", "_").replace("'", "_")
fun String.normalized() = Normalizer.normalize(this, Normalizer.Form.NFD).replace(Regex("[^\\p{ASCII}]"), "") fun String.normalized() = Normalizer.normalize(this, Normalizer.Form.NFD).replace(Regex("[^\\p{ASCII}]"), "")
@ -73,7 +80,10 @@ data class Tag(
val title: String, val title: String,
val tag: String, val tag: String,
val subtags: List<Tag> val subtags: List<Tag>
) ) {
val adaptedTitle
get() = title.adaptAsTitle()
}
private fun printLanguageCodeAndTags( private fun printLanguageCodeAndTags(
tag: Tag, tag: Tag,
@ -81,17 +91,19 @@ private fun printLanguageCodeAndTags(
indents: String = " " indents: String = " "
): String = if (tag.subtags.isEmpty()) { ): String = if (tag.subtags.isEmpty()) {
"""${indents}${baseClassSerializerAnnotationName} """${indents}${baseClassSerializerAnnotationName}
${indents}object ${tag.title} : ${parent ?.title ?: baseClassName}() { override val code: String = "${tag.tag}"${parent ?.let { parent -> "; override val parentLang: ${parent.title} get() = ${parent.title};" } ?: ""} }""" ${indents}object ${tag.adaptedTitle} : ${parent ?.adaptedTitle ?: baseClassName} { override val code: String = "${tag.tag}"${parent ?.let { parent -> "; override val parentLang: ${parent.adaptedTitle} get() = ${parent.adaptedTitle}" } ?: ""}; override fun toString() = code }"""
} else { } else {
""" """
${indents}${baseClassSerializerAnnotationName} ${indents}${baseClassSerializerAnnotationName}
${indents}sealed class ${tag.title} : ${parent ?.title ?: baseClassName}() { ${indents}sealed interface ${tag.adaptedTitle} : ${parent ?.adaptedTitle ?: baseClassName} {
${indents} override val code: String = "${tag.tag}"${parent ?.let { parent -> "\n${indents} override val parentLang: ${parent.title} get() = ${parent.title};" } ?: ""}
${tag.subtags.joinToString("\n") { printLanguageCodeAndTags(it, tag, "${indents} ") }} ${tag.subtags.joinToString("\n") { printLanguageCodeAndTags(it, tag, "${indents} ") }}
${indents} ${baseClassSerializerAnnotationName} ${indents} ${baseClassSerializerAnnotationName}
${indents} companion object : ${tag.title}() ${indents} companion object : ${tag.adaptedTitle} {
${indents} override val code: String = "${tag.tag}"${parent ?.let { parent -> "\n${indents} override val parentLang: ${parent.adaptedTitle} get() = ${parent.adaptedTitle};" } ?: ""}
${indents} override fun toString() = code
${indents} }
${indents}} ${indents}}
""" """
} }
@ -105,23 +117,22 @@ import kotlinx.serialization.Serializable
* https://datahub.io/core/language-codes/ files (base and tags) and create the whole hierarchy using it. * https://datahub.io/core/language-codes/ files (base and tags) and create the whole hierarchy using it.
*/ */
${baseClassSerializerAnnotationName} ${baseClassSerializerAnnotationName}
sealed class $baseClassName { sealed interface $baseClassName {
abstract val code: String val code: String
open val parentLang: $baseClassName? val parentLang: $baseClassName?
get() = null get() = code.split("-").takeIf { it.size > 1 } ?.first() ?.let(::$unknownBaseClassName)
open val withoutDialect: String val withoutDialect: String
get() = parentLang ?.code ?: code get() = parentLang ?.code ?: code
${tags.joinToString("\n") { printLanguageCodeAndTags(it, indents = " ") } } ${tags.joinToString("\n") { printLanguageCodeAndTags(it, indents = " ") } }
$baseClassSerializerAnnotationName $baseClassSerializerAnnotationName
data class $unknownBaseClassName (override val code: String) : $baseClassName() { data class $unknownBaseClassName (override val code: String) : $baseClassName {
override val parentLang = code.dropLastWhile { it != '-' }.removeSuffix("-").takeIf { it.length > 0 } ?.let(::$unknownBaseClassName) override val parentLang = code.dropLastWhile { it != '-' }.removeSuffix("-").takeIf { it.length > 0 } ?.let(::$unknownBaseClassName)
} }
@Deprecated("Renamed", ReplaceWith("$baseClassName.$unknownBaseClassName", "${if (prePackage.isNotEmpty()) "$prePackage." else ""}$baseClassName.$unknownBaseClassName")) @Deprecated("Renamed", ReplaceWith("$baseClassName.$unknownBaseClassName", "${if (prePackage.isNotEmpty()) "$prePackage." else ""}$baseClassName.$unknownBaseClassName"))
val $oldUnknownBaseClassName = $unknownBaseClassName val $oldUnknownBaseClassName
get() = $unknownBaseClassName
override fun toString() = code
} }
@Deprecated("Renamed", ReplaceWith("$baseClassName", "${if (prePackage.isNotEmpty()) "$prePackage." else ""}$baseClassName")) @Deprecated("Renamed", ReplaceWith("$baseClassName", "${if (prePackage.isNotEmpty()) "$prePackage." else ""}$baseClassName"))
typealias $oldBaseClassName = $baseClassName typealias $oldBaseClassName = $baseClassName
@ -133,7 +144,7 @@ fun createStringConverterCode(tags: List<Tag>, prePackage: String): String {
pretitle: String = baseClassName, pretitle: String = baseClassName,
indents: String = " " indents: String = " "
): String { ): String {
val currentTitle = "$pretitle.${tag.title}" val currentTitle = "$pretitle.${tag.adaptedTitle}"
return """${indents}$currentTitle.code -> $currentTitle${if (tag.subtags.isNotEmpty()) tag.subtags.joinToString("\n", "\n") { createDeserializeVariantForTag(it, currentTitle, indents) } else ""}""" return """${indents}$currentTitle.code -> $currentTitle${if (tag.subtags.isNotEmpty()) tag.subtags.joinToString("\n", "\n") { createDeserializeVariantForTag(it, currentTitle, indents) } else ""}"""
} }
fun createInheritorVariantForTag( fun createInheritorVariantForTag(
@ -141,7 +152,7 @@ fun createStringConverterCode(tags: List<Tag>, prePackage: String): String {
pretitle: String = baseClassName, pretitle: String = baseClassName,
indents: String = " " indents: String = " "
): String { ): String {
val currentTitle = "$pretitle.${tag.title}" val currentTitle = "$pretitle.${tag.adaptedTitle}"
val subtags = if (tag.subtags.isNotEmpty()) { val subtags = if (tag.subtags.isNotEmpty()) {
tag.subtags.joinToString(",\n", ",\n") { createInheritorVariantForTag(it, currentTitle, "$indents ") } tag.subtags.joinToString(",\n", ",\n") { createInheritorVariantForTag(it, currentTitle, "$indents ") }
} else { } else {
@ -155,7 +166,7 @@ fun createStringConverterCode(tags: List<Tag>, prePackage: String): String {
indents: String = " ", indents: String = " ",
codeSuffix: String = "" codeSuffix: String = ""
): String { ): String {
val currentTitle = "$pretitle.${tag.title}" val currentTitle = "$pretitle.${tag.adaptedTitle}"
val subtags = if (tag.subtags.isNotEmpty()) { val subtags = if (tag.subtags.isNotEmpty()) {
tag.subtags.joinToString(",\n", ",\n") { createInheritorVariantForMapForTag(it, currentTitle, "$indents ", codeSuffix) } tag.subtags.joinToString(",\n", ",\n") { createInheritorVariantForMapForTag(it, currentTitle, "$indents ", codeSuffix) }
} else { } else {
@ -266,7 +277,7 @@ suspend fun main(vararg args: String) {
File(outputFolder, "LanguageCodes.kt").apply { File(outputFolder, "LanguageCodes.kt").apply {
delete() delete()
createNewFile() createNewFile()
writeText(targetPackagePrefix + buildKtFileContent(tags, targetPackage ?: "")) writeText("@file:Suppress(\"SERIALIZER_TYPE_INCOMPATIBLE\")\n\n" + targetPackagePrefix + buildKtFileContent(tags, targetPackage ?: ""))
} }
File(outputFolder, "StringToLanguageCodes.kt").apply { File(outputFolder, "StringToLanguageCodes.kt").apply {

File diff suppressed because one or more lines are too long

View File

@ -77,7 +77,6 @@ kotlin {
jsTest { jsTest {
dependencies { dependencies {
implementation kotlin('test-js') implementation kotlin('test-js')
implementation kotlin('test-junit')
} }
} }
nativeMain.dependsOn commonMain nativeMain.dependsOn commonMain