From cbcb7ce30b845a87469a5b375ede9ca6b915c8f8 Mon Sep 17 00:00:00 2001 From: shuuy Date: Sat, 5 Oct 2024 00:33:54 -0700 Subject: [PATCH] Simplify i18n tasks --- .../i18n/tasks/GenerateI18nFileTask.kt | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/build-logic/i18n/src/main/kotlin/live/shuuyu/plugins/i18n/tasks/GenerateI18nFileTask.kt b/build-logic/i18n/src/main/kotlin/live/shuuyu/plugins/i18n/tasks/GenerateI18nFileTask.kt index 4a7cd2d..337a64d 100644 --- a/build-logic/i18n/src/main/kotlin/live/shuuyu/plugins/i18n/tasks/GenerateI18nFileTask.kt +++ b/build-logic/i18n/src/main/kotlin/live/shuuyu/plugins/i18n/tasks/GenerateI18nFileTask.kt @@ -47,21 +47,21 @@ public abstract class GenerateI18nFileTask: DefaultTask() { println(files) for (file in files) { - createFile(generatedPackageName.get(), file.name.capitalized().stripSuffix(parser)) { + createFile(generatedPackageName.get(), file.nameWithoutExtension.capitalized()) { addFileComment( """ THIS IS AN AUTOMATICALLY GENERATED FILE FROM THE PLUGIN `live.shuuyu.plugins.i18n`. DO NOT DELETE OR MODIFY! """.trimIndent() ) - addType(generateKotlinObject(file.name, load(file, parser), listOf())) + addType(generateKotlinObject(file.nameWithoutExtension, load(file, parser), listOf())) }.writeTo(outputDirectory) } } @Suppress("Unchecked_Cast") private fun generateKotlinObject(name: String, contents: Map, children: List): TypeSpec { - return createObject(name.capitalized().stripSuffix(parserType.get())) { + return createObject(name.capitalized()) { for ((key, value) in contents) { when (value) { is Map<*, *> -> { @@ -69,7 +69,7 @@ public abstract class GenerateI18nFileTask: DefaultTask() { generateKotlinObject( key, value as Map, - (children + name.toDefaultLowerCase().stripSuffix(parserType.get())).toMutableList().apply { + (children + name.toDefaultLowerCase()).toMutableList().apply { this.add(key) } ) @@ -174,7 +174,10 @@ public abstract class GenerateI18nFileTask: DefaultTask() { @Synchronized private fun getFiles(directory: File): List { val fileList = mutableListOf() - val files = directory.listFiles() + // Exclude languages to prevent it from being picked up in detection. + val files = directory.listFiles().filter { + it.nameWithoutExtension != "Language" + } for (file in files) { if (file.isFile) { @@ -194,14 +197,4 @@ public abstract class GenerateI18nFileTask: DefaultTask() { ParserType.Toml -> ObjectMapper(TomlFactory()).readValue(file, object: TypeReference>() {}) } } - - // KotlinPoet hates suffixes apparently - private fun String.stripSuffix(parser: ParserType) = when(parser) { - ParserType.Json -> this.removeSuffix(".json") - ParserType.Yaml -> { - this.removeSuffix(".yml") - this.removeSuffix(".yaml") - } - ParserType.Toml -> this.removeSuffix(".toml") - } }