From: fredboy Date: Tue, 14 May 2024 12:42:52 +0000 (+0700) Subject: Add module code gen X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=4e63a1e672fcf47fab405eb660f7020b6f5c0283;p=cavedroid.git Add module code gen --- diff --git a/build.gradle b/build.gradle index e782b87..1e317b0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,18 @@ buildscript { ext { appName = "CaveDroid" + gdxVersion = '1.12.0' + guavaVersion = '28.1' + daggerVersion = '2.51.1' + kotlinVersion = '1.9.24' kspVersion = '1.9.24-1.0.20' kotlinSerializationVersion = '1.6.3' + + kotlinpoetKspVersion = '1.16.0' } repositories { diff --git a/core/build.gradle b/core/build.gradle index 4837be7..c160724 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -4,6 +4,7 @@ plugins { id "kotlin" id "idea" id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlinVersion" + id 'com.google.devtools.ksp' version "$kspVersion" } java.targetCompatibility = JavaVersion.VERSION_17 @@ -19,4 +20,5 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinSerializationVersion" annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion" + ksp project(':dagger-multibind-ksp') } \ No newline at end of file diff --git a/dagger-multibind-annotations/build.gradle b/dagger-multibind-annotations/build.gradle new file mode 100644 index 0000000..27133f0 --- /dev/null +++ b/dagger-multibind-annotations/build.gradle @@ -0,0 +1,7 @@ +plugins { + id 'kotlin' +} + +kotlin { + jvmToolchain(17) +} diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/PlaceBlockAction.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/PlaceBlockAction.kt new file mode 100644 index 0000000..2d3046e --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/PlaceBlockAction.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.actions + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class PlaceBlockAction(val actionKey: String) diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UpdateBlockAction.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UpdateBlockAction.kt new file mode 100644 index 0000000..1ba6b87 --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UpdateBlockAction.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.actions + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class UpdateBlockAction(val blockKey: String) diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseBlockAction.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseBlockAction.kt new file mode 100644 index 0000000..4e53819 --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseBlockAction.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.actions + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class UseBlockAction(val blockKey: String) diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseItemAction.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseItemAction.kt new file mode 100644 index 0000000..fc47677 --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseItemAction.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.actions + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class UseItemAction(val actionKey: String) diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/KeyboardInputHandler.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/KeyboardInputHandler.kt new file mode 100644 index 0000000..311a050 --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/KeyboardInputHandler.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.input + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class KeyboardInputHandler diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/MouseInputHandler.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/MouseInputHandler.kt new file mode 100644 index 0000000..d3a630e --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/MouseInputHandler.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.input + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class MouseInputHandler diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/render/Renderer.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/render/Renderer.kt new file mode 100644 index 0000000..8a813df --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/render/Renderer.kt @@ -0,0 +1,5 @@ +package ru.fredboy.cavedroid.ksp.annotations.render + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class Renderer diff --git a/dagger-multibind-ksp/build.gradle b/dagger-multibind-ksp/build.gradle index ea5375f..e73405d 100644 --- a/dagger-multibind-ksp/build.gradle +++ b/dagger-multibind-ksp/build.gradle @@ -3,10 +3,12 @@ plugins { id 'com.google.devtools.ksp' version "$kspVersion" } -repositories { - mavenCentral() -} - kotlin { jvmToolchain(17) -} \ No newline at end of file +} + +dependencies { + implementation project(':dagger-multibind-annotations') + implementation "com.squareup:kotlinpoet-ksp:$kotlinpoetKspVersion" + implementation "com.google.devtools.ksp:symbol-processing-api:$kspVersion" +} diff --git a/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/RendererSymbolProcessor.kt b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/RendererSymbolProcessor.kt new file mode 100644 index 0000000..d97c2ab --- /dev/null +++ b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/RendererSymbolProcessor.kt @@ -0,0 +1,66 @@ +package ru.fredboy.cavedroid.ksp.processor + +import com.google.devtools.ksp.processing.CodeGenerator +import com.google.devtools.ksp.processing.Dependencies +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.squareup.kotlinpoet.* +import com.squareup.kotlinpoet.ksp.toClassName +import com.squareup.kotlinpoet.ksp.writeTo +import ru.fredboy.cavedroid.ksp.annotations.render.Renderer +import java.lang.reflect.Type + +internal class RendererSymbolProcessor( + private val codeGenerator: CodeGenerator, +) : SymbolProcessor { + + private fun generateModule(renderers: List): FileSpec? { + if (renderers.isEmpty()) { + return null + } + + val bindings = renderers.map { renderer -> + FunSpec.builder("bind${renderer.simpleName}") + .addAnnotation(ClassName("dagger", "Binds")) + .addAnnotation(ClassName("dagger", "IntoSet")) + .addAnnotations(renderer.annotations) + .addParameter(ParameterSpec("renderer", renderer)) + .returns(IGameRendererType) + .beginControlFlow("return renderer") + .build() + } + + val moduleObject = TypeSpec.objectBuilder(MODULE_NAME) + .addAnnotation(ClassName("dagger", "Module")) + .addFunctions(bindings) + .build() + + return FileSpec.builder(MODULE_PACKAGE, MODULE_NAME) +// .addImport("dagger", "Binds", "Module", "IntoSet") +// .addImport("ru.deadsoftware.cavedroid.game", "GameScope") + .addType(moduleObject) + .build() + + } + + override fun process(resolver: Resolver): List { + generateModule( + resolver.getAnnotatedClasses(MODULE_PACKAGE, Renderer::class) + .map(KSClassDeclaration::toClassName) + .toList() + )?.writeTo(codeGenerator, Dependencies(true)) + + return emptyList() + } + + private object IGameRendererType : Type { + override fun getTypeName(): String = "IGameRenderer" + } + + companion object { + private const val MODULE_PACKAGE = "ru.deadsoftware.cavedroid.game.render" + private const val MODULE_NAME = "RenderModule" + } +} \ No newline at end of file diff --git a/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/SymbolProcessorUtils.kt b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/SymbolProcessorUtils.kt new file mode 100644 index 0000000..0b0e0b9 --- /dev/null +++ b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/SymbolProcessorUtils.kt @@ -0,0 +1,17 @@ +package ru.fredboy.cavedroid.ksp.processor + +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.symbol.KSNode +import com.google.devtools.ksp.validate +import kotlin.reflect.KClass + +internal fun Resolver.getAnnotatedClasses( + packageName: String, + annotationClass: KClass<*> +): Sequence { + return getSymbolsWithAnnotation(annotationClass.qualifiedName.orEmpty()) + .filterIsInstance() + .filter { it.packageName.getShortName() == packageName } + .filter(KSNode::validate) +} \ No newline at end of file diff --git a/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/RendererSymbolProcessorProvider.kt b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/RendererSymbolProcessorProvider.kt new file mode 100644 index 0000000..eedf89b --- /dev/null +++ b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/RendererSymbolProcessorProvider.kt @@ -0,0 +1,16 @@ +package ru.fredboy.cavedroid.ksp.provider + +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.processing.SymbolProcessorEnvironment +import com.google.devtools.ksp.processing.SymbolProcessorProvider +import ru.fredboy.cavedroid.ksp.processor.RendererSymbolProcessor + +internal class RendererSymbolProcessorProvider : SymbolProcessorProvider { + + override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { + return RendererSymbolProcessor( + codeGenerator = environment.codeGenerator, + ) + } + +} \ No newline at end of file diff --git a/dagger-multibind-ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider b/dagger-multibind-ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider new file mode 100644 index 0000000..a8a853f --- /dev/null +++ b/dagger-multibind-ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider @@ -0,0 +1 @@ +ru.fredboy.cavedroid.ksp.provider.RendererSymbolProcessorProvider diff --git a/settings.gradle b/settings.gradle index 257dfd5..c441146 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,5 +3,6 @@ plugins { } include 'desktop', 'android', 'core' +include 'dagger-multibind-annotations' include 'dagger-multibind-ksp'