From fb1499d3a7c3996587283924e473fe9609dc7f1d Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 23 May 2022 09:59:56 +0300 Subject: [PATCH] Check MangaSource locale and name --- .../kotatsu/parsers/ksp/ParserProcessor.kt | 14 ++++++++++++-- .../org/koitharu/kotatsu/parsers/ksp/Utils.kt | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/Utils.kt diff --git a/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt b/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt index 12cf1246..bbce8409 100644 --- a/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt +++ b/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/ParserProcessor.kt @@ -8,6 +8,7 @@ import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSVisitorVoid import com.google.devtools.ksp.validate import java.io.Writer +import java.util.* class ParserProcessor( private val codeGenerator: CodeGenerator, @@ -15,6 +16,9 @@ class ParserProcessor( private val options: Map, ) : SymbolProcessor { + private val availableLocales = Locale.getAvailableLocales().toSet() + private val sourceNamePattern = Regex("[A-Z_][A-Z0-9_]{3,}") + override fun process(resolver: Resolver): List { val symbols = resolver .getSymbolsWithAnnotation("org.koitharu.kotatsu.parsers.MangaSourceParser") @@ -121,8 +125,13 @@ class ParserProcessor( val title = annotation.arguments.single { it.name?.asString() == "title" }.value as String val locale = annotation.arguments.single { it.name?.asString() == "locale" }.value as String val localeString = if (locale.isEmpty()) "null" else "\"$locale\"" + val localeObj = if (locale.isEmpty()) null else Locale(locale) + val localeTitle = localeObj?.getDisplayLanguage(localeObj) + if (localeObj != null && localeObj !in availableLocales) { + logger.error("Manga source $name has wrong locale: $localeTitle") + } - if (name.any { it.isLowerCase() }) { + if (!sourceNamePattern.matches(name)) { logger.error("Manga source name must be uppercase: $name") } @@ -136,7 +145,8 @@ class ParserProcessor( val className = classDeclaration.qualifiedName?.asString() factoryWriter?.write("\tMangaSource.$name -> $className(context)\n") - sourcesWriter?.write("\t$name(\"$title\", $localeString),\n") + val localeComment = localeTitle?.toTitleCase(localeObj)?.let { " /* $it */" }.orEmpty() + sourcesWriter?.write("\t$name(\"$title\", $localeString$localeComment),\n") } } } \ No newline at end of file diff --git a/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/Utils.kt b/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/Utils.kt new file mode 100644 index 00000000..2a2fa364 --- /dev/null +++ b/kotatsu-parsers-ksp/src/main/kotlin/org/koitharu/kotatsu/parsers/ksp/Utils.kt @@ -0,0 +1,7 @@ +package org.koitharu.kotatsu.parsers.ksp + +import java.util.* + +fun String.toTitleCase(locale: Locale?): String { + return replaceFirstChar { x -> x.uppercase(locale ?: Locale.ROOT) } +} \ No newline at end of file