Skip to content

Commit

Permalink
Simplify i18n tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
shuuyu committed Oct 5, 2024
1 parent 6557a4e commit cbcb7ce
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,29 @@ 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<String, Any>, children: List<String>): TypeSpec {
return createObject(name.capitalized().stripSuffix(parserType.get())) {
return createObject(name.capitalized()) {
for ((key, value) in contents) {
when (value) {
is Map<*, *> -> {
this.addType(
generateKotlinObject(
key,
value as Map<String, Any>,
(children + name.toDefaultLowerCase().stripSuffix(parserType.get())).toMutableList().apply {
(children + name.toDefaultLowerCase()).toMutableList().apply {
this.add(key)
}
)
Expand Down Expand Up @@ -174,7 +174,10 @@ public abstract class GenerateI18nFileTask: DefaultTask() {
@Synchronized
private fun getFiles(directory: File): List<File> {
val fileList = mutableListOf<File>()
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) {
Expand All @@ -194,14 +197,4 @@ public abstract class GenerateI18nFileTask: DefaultTask() {
ParserType.Toml -> ObjectMapper(TomlFactory()).readValue(file, object: TypeReference<Map<String, Any>>() {})
}
}

// 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")
}
}

0 comments on commit cbcb7ce

Please sign in to comment.