Skip to content

Commit

Permalink
fixing ksp multi round support to properly work with other code gener…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
DevSrSouza committed Jul 11, 2023
1 parent 9ee4c03 commit ec1e00f
Showing 1 changed file with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,40 @@ internal class LyricistSymbolProcessor(
) : SymbolProcessor {

private val declarations = mutableListOf<KSPropertyDeclaration>()
private val previousRoundDeclarations = mutableSetOf<KSPropertyDeclaration>()

private val visitor = LyricistVisitor(declarations)
private var generatedInPreviousRounds: Boolean = false

override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getSymbolsWithAnnotation(ANNOTATION_PACKAGE)
declarations.clear()
val lyricistSymbols = resolver.getSymbolsWithAnnotation(ANNOTATION_PACKAGE)
.filter { it is KSPropertyDeclaration && it.validate() }
.forEach { it.accept(visitor, Unit) }
.toList()

return emptyList()
}
lyricistSymbols.forEach { it.accept(visitor, Unit) }

val roundDeclaration =
declarations.filterNot { dec -> previousRoundDeclarations.any { it.qualifiedName == dec.qualifiedName } }
previousRoundDeclarations += declarations

override fun finish() {
if (validate().not()) return
if (validate(roundDeclaration).not()) return emptyList()

val fileName = "${config.moduleName.toUpperCamelCase()}Strings"

val stringsName = "${config.moduleName.toLowerCamelCase()}Strings"

val visibility = if (config.internalVisibility) "internal" else "public"

val defaultLanguageTag = declarations
val defaultLanguageTag = roundDeclaration
.firstNotNullOfOrNull { it.annotations.getDefaultLanguageTag() }
?.let { "\"$it\"" }
?: "Locale.current.toLanguageTag()"

val defaultStrings = declarations
val defaultStrings = roundDeclaration
.first { it.annotations.getValue<Boolean>(ANNOTATION_PARAM_DEFAULT) == true }

val packagesOutput = declarations
val packagesOutput = roundDeclaration
.mapNotNull { it.qualifiedName?.asString() }
.plus(defaultStrings.getClassQualifiedName())
.joinToString(separator = "\n") { packageName -> "import $packageName" }
Expand All @@ -57,7 +62,7 @@ internal class LyricistSymbolProcessor(

val defaultStringsOutput = defaultStrings.simpleName.getShortName()

val translationMappingOutput = declarations
val translationMappingOutput = roundDeclaration
.map {
it.annotations.getValue<String>(ANNOTATION_PARAM_LANGUAGE_TAG)!! to it.simpleName.getShortName()
}.joinToString(",\n") { (languageTag, property) ->
Expand All @@ -67,7 +72,7 @@ internal class LyricistSymbolProcessor(
codeGenerator.createNewFile(
dependencies = Dependencies(
aggregating = true,
sources = declarations.map { it.containingFile!! }.toTypedArray()
sources = roundDeclaration.map { it.containingFile!! }.toTypedArray()
),
packageName = config.packageName,
fileName = fileName
Expand Down Expand Up @@ -108,29 +113,41 @@ internal class LyricistSymbolProcessor(
""".trimMargin().toByteArray()
)
}

generatedInPreviousRounds = true

return emptyList()
}

private fun validate(): Boolean {
val defaultCount = declarations
private fun validate(properties: List<KSPropertyDeclaration>): Boolean {
val defaultCount = properties
.count { it.annotations.getValue<Boolean>(ANNOTATION_PARAM_DEFAULT) == true }

val differentTypeCount = declarations
val differentTypeCount = properties
.groupBy { it.getClassQualifiedName() }
.count()

return when {
generatedInPreviousRounds -> {
// ignore code generation if is a new round and did already generated previously
false
}

defaultCount == 0 -> {
logger.warn("No @LyricistStrings(default = true) found")
false
}

defaultCount > 1 -> {
logger.exception(IllegalArgumentException("More than one @LyricistStrings(default = true) found"))
false
}

differentTypeCount != 1 -> {
logger.exception(IllegalArgumentException("All @LyricistStrings must have the same type"))
false
}

else -> true
}
}
Expand Down

0 comments on commit ec1e00f

Please sign in to comment.