Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: Make build fail on wrong annotation #2024

Merged
merged 6 commits into from
Jun 8, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package at.hannibal2.skyhanni.skyhannimodule

import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.KSPLogger
Expand All @@ -8,13 +10,24 @@ import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.validate
import java.io.OutputStreamWriter

class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logger: KSPLogger) : SymbolProcessor {

// TODO remove once all events are migrated to SkyHanniEvent
private var skyHanniEvent: KSType? = null
private var minecraftForgeEvent: KSType? = null
private val warnings = mutableListOf<String>()

override fun process(resolver: Resolver): List<KSAnnotated> {

skyHanniEvent =
resolver.getClassDeclarationByName("at.hannibal2.skyhanni.api.event.SkyHanniEvent")?.asStarProjectedType()
minecraftForgeEvent = resolver.getClassDeclarationByName("net.minecraftforge.fml.common.eventhandler.Event")
?.asStarProjectedType()

val symbols = resolver.getSymbolsWithAnnotation(SkyHanniModule::class.qualifiedName!!).toList()
val validSymbols = symbols.mapNotNull { validateSymbol(it) }

Expand All @@ -41,10 +54,36 @@ class ModuleProcessor(private val codeGenerator: CodeGenerator, private val logg
return null
}

// TODO remove once all events are migrated to SkyHanniEvent
val className = symbol.qualifiedName?.asString() ?: "unknown"

for (function in symbol.getDeclaredFunctions()) {
if (function.annotations.any { it.shortName.asString() == "SubscribeEvent" }) {
val firstParameter = function.parameters.firstOrNull()?.type?.resolve()!!
if (!minecraftForgeEvent!!.isAssignableFrom(firstParameter)) {
warnings.add("Function in $className must have an event assignable from $minecraftForgeEvent because it is annotated with @SubscribeEvent")
}
}

if (function.annotations.any { it.shortName.asString() == "HandleEvent" }) {
val firstParameter = function.parameters.firstOrNull()?.type?.resolve()!!
if (!skyHanniEvent!!.isAssignableFrom(firstParameter)) {
warnings.add("Function in $className must have an event assignable from $skyHanniEvent because it is annotated with @HandleEvent")
}
}
}

return symbol
}

// TODO use Kotlin Poet once KMixins is merged
private fun generateFile(symbols: List<KSClassDeclaration>) {

if (warnings.isNotEmpty()) {
warnings.forEach { logger.warn(it) }
error("${warnings.size} errors related to event annotations found, please fix them before continuing")
}

val dependencies = symbols.mapNotNull { it.containingFile }.toTypedArray()
val deps = Dependencies(true, *dependencies)

Expand Down
Loading