Check MangaSource locale and name
This commit is contained in:
@@ -8,6 +8,7 @@ import com.google.devtools.ksp.symbol.KSClassDeclaration
|
|||||||
import com.google.devtools.ksp.symbol.KSVisitorVoid
|
import com.google.devtools.ksp.symbol.KSVisitorVoid
|
||||||
import com.google.devtools.ksp.validate
|
import com.google.devtools.ksp.validate
|
||||||
import java.io.Writer
|
import java.io.Writer
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class ParserProcessor(
|
class ParserProcessor(
|
||||||
private val codeGenerator: CodeGenerator,
|
private val codeGenerator: CodeGenerator,
|
||||||
@@ -15,6 +16,9 @@ class ParserProcessor(
|
|||||||
private val options: Map<String, String>,
|
private val options: Map<String, String>,
|
||||||
) : SymbolProcessor {
|
) : SymbolProcessor {
|
||||||
|
|
||||||
|
private val availableLocales = Locale.getAvailableLocales().toSet()
|
||||||
|
private val sourceNamePattern = Regex("[A-Z_][A-Z0-9_]{3,}")
|
||||||
|
|
||||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||||
val symbols = resolver
|
val symbols = resolver
|
||||||
.getSymbolsWithAnnotation("org.koitharu.kotatsu.parsers.MangaSourceParser")
|
.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 title = annotation.arguments.single { it.name?.asString() == "title" }.value as String
|
||||||
val locale = annotation.arguments.single { it.name?.asString() == "locale" }.value as String
|
val locale = annotation.arguments.single { it.name?.asString() == "locale" }.value as String
|
||||||
val localeString = if (locale.isEmpty()) "null" else "\"$locale\""
|
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")
|
logger.error("Manga source name must be uppercase: $name")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +145,8 @@ class ParserProcessor(
|
|||||||
|
|
||||||
val className = classDeclaration.qualifiedName?.asString()
|
val className = classDeclaration.qualifiedName?.asString()
|
||||||
factoryWriter?.write("\tMangaSource.$name -> $className(context)\n")
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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) }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user