DEADSOFTWARE

Add module code gen
authorfredboy <fredboy@protonmail.com>
Tue, 14 May 2024 12:42:52 +0000 (19:42 +0700)
committerfredboy <fredboy@protonmail.com>
Tue, 14 May 2024 12:42:52 +0000 (19:42 +0700)
16 files changed:
build.gradle
core/build.gradle
dagger-multibind-annotations/build.gradle [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/PlaceBlockAction.kt [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UpdateBlockAction.kt [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseBlockAction.kt [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseItemAction.kt [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/KeyboardInputHandler.kt [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/MouseInputHandler.kt [new file with mode: 0644]
dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/render/Renderer.kt [new file with mode: 0644]
dagger-multibind-ksp/build.gradle
dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/RendererSymbolProcessor.kt [new file with mode: 0644]
dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/SymbolProcessorUtils.kt [new file with mode: 0644]
dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/RendererSymbolProcessorProvider.kt [new file with mode: 0644]
dagger-multibind-ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider [new file with mode: 0644]
settings.gradle

index e782b875c13cf50c9771e87959f406be7cdb817f..1e317b0f0d66e9c5d161d8b875f32efccfbaf1a3 100644 (file)
@@ -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 {
index 4837be72094456afd7345ef288a2a9b0140a35bf..c16072417b3af6ca26908531e628898aed54765e 100644 (file)
@@ -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 (file)
index 0000000..27133f0
--- /dev/null
@@ -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 (file)
index 0000000..2d3046e
--- /dev/null
@@ -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 (file)
index 0000000..1ba6b87
--- /dev/null
@@ -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 (file)
index 0000000..4e53819
--- /dev/null
@@ -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 (file)
index 0000000..fc47677
--- /dev/null
@@ -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 (file)
index 0000000..311a050
--- /dev/null
@@ -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 (file)
index 0000000..d3a630e
--- /dev/null
@@ -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 (file)
index 0000000..8a813df
--- /dev/null
@@ -0,0 +1,5 @@
+package ru.fredboy.cavedroid.ksp.annotations.render
+
+@Target(AnnotationTarget.CLASS)
+@Retention(AnnotationRetention.SOURCE)
+annotation class Renderer
index ea5375f54bb767ca6a0bdb8ae02a724616c61e6f..e73405db1b01954a17aaf51af3c697a8cec1bf5d 100644 (file)
@@ -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 (file)
index 0000000..d97c2ab
--- /dev/null
@@ -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<ClassName>): 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<KSAnnotated> {
+        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 (file)
index 0000000..0b0e0b9
--- /dev/null
@@ -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<KSClassDeclaration> {
+    return getSymbolsWithAnnotation(annotationClass.qualifiedName.orEmpty())
+        .filterIsInstance<KSClassDeclaration>()
+        .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 (file)
index 0000000..eedf89b
--- /dev/null
@@ -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 (file)
index 0000000..a8a853f
--- /dev/null
@@ -0,0 +1 @@
+ru.fredboy.cavedroid.ksp.provider.RendererSymbolProcessorProvider
index 257dfd5805709bf98862e73c84176e8fb8eb88ee..c441146968383ca2536e679ad206c33dd4fac27a 100644 (file)
@@ -3,5 +3,6 @@ plugins {
 }
 
 include 'desktop', 'android', 'core'
+include 'dagger-multibind-annotations'
 include 'dagger-multibind-ksp'