summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 5883a84)
raw | patch | inline | side by side (parent: 5883a84)
author | fredboy <fredboy@protonmail.com> | |
Tue, 14 May 2024 12:42:52 +0000 (19:42 +0700) | ||
committer | fredboy <fredboy@protonmail.com> | |
Tue, 14 May 2024 12:42:52 +0000 (19:42 +0700) |
16 files changed:
diff --git a/build.gradle b/build.gradle
index e782b875c13cf50c9771e87959f406be7cdb817f..1e317b0f0d66e9c5d161d8b875f32efccfbaf1a3 100644 (file)
--- a/build.gradle
+++ b/build.gradle
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 4837be72094456afd7345ef288a2a9b0140a35bf..c16072417b3af6ca26908531e628898aed54765e 100644 (file)
--- a/core/build.gradle
+++ b/core/build.gradle
id "kotlin"
id "idea"
id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlinVersion"
+ id 'com.google.devtools.ksp' version "$kspVersion"
}
java.targetCompatibility = JavaVersion.VERSION_17
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
--- /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
--- /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
--- /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
--- /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
--- /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
--- /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
--- /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
--- /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
index ea5375f54bb767ca6a0bdb8ae02a724616c61e6f..e73405db1b01954a17aaf51af3c697a8cec1bf5d 100644 (file)
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
--- /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<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
--- /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<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
--- /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
--- /dev/null
@@ -0,0 +1 @@
+ru.fredboy.cavedroid.ksp.provider.RendererSymbolProcessorProvider
diff --git a/settings.gradle b/settings.gradle
index 257dfd5805709bf98862e73c84176e8fb8eb88ee..c441146968383ca2536e679ad206c33dd4fac27a 100644 (file)
--- a/settings.gradle
+++ b/settings.gradle
}
include 'desktop', 'android', 'core'
+include 'dagger-multibind-annotations'
include 'dagger-multibind-ksp'