From f50a0d30230d2e6dbb8dbc0587a2dd8950761ae5 Mon Sep 17 00:00:00 2001 From: fredboy Date: Tue, 14 May 2024 20:37:29 +0700 Subject: [PATCH] Add RenderModule code generation --- .gitignore | 2 + core/build.gradle | 4 +- .../game/render/BackgroundBlocksRenderer.kt | 1 + .../cavedroid/game/render/DebugRenderer.kt | 2 +- .../cavedroid/game/render/DropsRenderer.kt | 1 + .../game/render/ForegroundBlocksRenderer.kt | 1 + .../cavedroid/game/render/GameRenderer.kt | 10 +++ .../cavedroid/game/render/HudRenderer.kt | 1 + .../cavedroid/game/render/MobsRenderer.kt | 1 + .../cavedroid/game/render/RenderModule.kt | 59 -------------- .../game/render/TouchControlsRenderer.kt | 1 + .../cavedroid/game/render/WindowsRenderer.kt | 1 + .../GenerateSetMultibindingsModule.kt | 11 +++ .../actions/PlaceBlockAction.kt | 5 -- .../actions/UpdateBlockAction.kt | 5 -- .../actions/UseBlockAction.kt | 5 -- .../actions/UseItemAction.kt | 5 -- .../input/KeyboardInputHandler.kt | 5 -- .../input/MouseInputHandler.kt | 5 -- .../render/Renderer.kt | 5 -- ...GenerateSetMultibindingsSymbolProcessor.kt | 81 +++++++++++++++++++ .../ksp/processor/RendererSymbolProcessor.kt | 66 --------------- .../ksp/processor/SymbolProcessorUtils.kt | 9 ++- ...etMultibindingsSymbolProcessorProvider.kt} | 7 +- ...ols.ksp.processing.SymbolProcessorProvider | 2 +- desktop/build.gradle | 4 +- 26 files changed, 127 insertions(+), 172 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/render/GameRenderer.kt delete mode 100644 core/src/ru/deadsoftware/cavedroid/game/render/RenderModule.kt create mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/GenerateSetMultibindingsModule.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/PlaceBlockAction.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UpdateBlockAction.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseBlockAction.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseItemAction.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/KeyboardInputHandler.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/MouseInputHandler.kt delete mode 100644 dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/render/Renderer.kt create mode 100644 dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/GenerateSetMultibindingsSymbolProcessor.kt delete mode 100644 dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/RendererSymbolProcessor.kt rename dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/{RendererSymbolProcessorProvider.kt => GenerateSetMultibindingsSymbolProcessorProvider.kt} (59%) diff --git a/.gitignore b/.gitignore index e03f558..b24a8ed 100644 --- a/.gitignore +++ b/.gitignore @@ -119,3 +119,5 @@ Thumbs.db release-*/ keystore.properties + +*/build/ diff --git a/core/build.gradle b/core/build.gradle index c160724..0cff94f 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -13,6 +13,9 @@ java.sourceCompatibility = JavaVersion.VERSION_17 sourceSets.main.java.srcDirs = ["src/"] dependencies { + implementation project(':dagger-multibind-annotations') + ksp project(':dagger-multibind-ksp') + api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.google.guava:guava:$guavaVersion-android" api "com.google.dagger:dagger:$daggerVersion" @@ -20,5 +23,4 @@ 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/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt index aae4f98..e86450c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea import javax.inject.Inject @GameScope +@GameRenderer class BackgroundBlocksRenderer @Inject constructor( gameWorld: GameWorld, mobsController: MobsController diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt index 03f6253..d1a1d16 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt @@ -10,13 +10,13 @@ import ru.deadsoftware.cavedroid.game.debug.DebugInfoStringsProvider import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.model.block.Block import ru.deadsoftware.cavedroid.game.world.GameWorld -import ru.deadsoftware.cavedroid.misc.Assets import ru.deadsoftware.cavedroid.misc.utils.bl import ru.deadsoftware.cavedroid.misc.utils.drawString import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea import ru.deadsoftware.cavedroid.misc.utils.px import javax.inject.Inject +@GameRenderer @GameScope class DebugRenderer @Inject constructor( private val mainConfig: MainConfig, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/DropsRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/DropsRenderer.kt index f14d36b..5e1da4a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/DropsRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/DropsRenderer.kt @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px import javax.inject.Inject @GameScope +@GameRenderer class DropsRenderer @Inject constructor( private val dropController: DropController, private val gameWorld: GameWorld, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt index ef8be71..0bb4001 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt @@ -10,6 +10,7 @@ import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea import javax.inject.Inject @GameScope +@GameRenderer class ForegroundBlocksRenderer @Inject constructor( gameWorld: GameWorld, mobsController: MobsController diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/GameRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/GameRenderer.kt new file mode 100644 index 0000000..7e40f17 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/render/GameRenderer.kt @@ -0,0 +1,10 @@ +package ru.deadsoftware.cavedroid.game.render + +import ru.fredboy.cavedroid.ksp.annotations.GenerateSetMultibindingsModule + +@GenerateSetMultibindingsModule( + interfaceClass = IGameRenderer::class, + modulePackage = "ru.deadsoftware.cavedroid.game.render", + moduleName = "RenderModule" +) +annotation class GameRenderer diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt index a2570e2..cb70e16 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt @@ -15,6 +15,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px import javax.inject.Inject @GameScope +@GameRenderer class HudRenderer @Inject constructor( private val gameWorld: GameWorld, private val mobsController: MobsController, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/MobsRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/MobsRenderer.kt index 6d388bb..ab1dd01 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/MobsRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/MobsRenderer.kt @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px import javax.inject.Inject @GameScope +@GameRenderer class MobsRenderer @Inject constructor( private val mobsController: MobsController, private val gameWorld: GameWorld, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/RenderModule.kt b/core/src/ru/deadsoftware/cavedroid/game/render/RenderModule.kt deleted file mode 100644 index 9ae3b46..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/render/RenderModule.kt +++ /dev/null @@ -1,59 +0,0 @@ -package ru.deadsoftware.cavedroid.game.render - -import dagger.Binds -import dagger.Module -import dagger.multibindings.IntoSet -import ru.deadsoftware.cavedroid.game.GameScope - -@Module -object RenderModule { - - @Binds - @IntoSet - @GameScope - fun bindBackGroundBlocksRenderer(renderer: BackgroundBlocksRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindForegroundBlocksRenderer(renderer: ForegroundBlocksRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindMobsRenderer(renderer: MobsRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindDropsRenderer(renderer: DropsRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindHudRenderer(renderer: HudRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindWindowsRenderer(renderer: WindowsRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindTouchControlsRenderer(renderer: TouchControlsRenderer): IGameRenderer = renderer - - @Binds - @IntoSet - @GameScope - fun bindDebugRenderer(renderer: DebugRenderer): IGameRenderer = renderer - -// @Provides -// @GameScope -// fun provideGameRenderers(renderers: Set<@JvmSuppressWildcards IGameRenderer>): List { -// return renderers.asSequence() -// .sortedWith(Comparator.comparingInt(IGameRenderer::renderLayer)) -// .toList() -// } - -} diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt index 63aa09f..29702ae 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt @@ -17,6 +17,7 @@ import ru.deadsoftware.cavedroid.misc.utils.drawSprite import javax.inject.Inject @GameScope +@GameRenderer class TouchControlsRenderer @Inject constructor( private val mainConfig: MainConfig, private val mobsController: MobsController, diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/WindowsRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/WindowsRenderer.kt index fa37f8d..4e65b41 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/WindowsRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/WindowsRenderer.kt @@ -11,6 +11,7 @@ import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager import javax.inject.Inject @GameScope +@GameRenderer class WindowsRenderer @Inject constructor( private val creativeWindowRenderer: CreativeWindowRenderer, private val survivalWindowRenderer: SurvivalWindowRenderer, diff --git a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/GenerateSetMultibindingsModule.kt b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/GenerateSetMultibindingsModule.kt new file mode 100644 index 0000000..2fb4915 --- /dev/null +++ b/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/GenerateSetMultibindingsModule.kt @@ -0,0 +1,11 @@ +package ru.fredboy.cavedroid.ksp.annotations + +import kotlin.reflect.KClass + +@Target(AnnotationTarget.ANNOTATION_CLASS) +@Retention(AnnotationRetention.SOURCE) +annotation class GenerateSetMultibindingsModule( + val interfaceClass: KClass<*>, + val modulePackage: String, + val moduleName: String, +) 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 deleted file mode 100644 index 2d3046e..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/PlaceBlockAction.kt +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index 1ba6b87..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UpdateBlockAction.kt +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index 4e53819..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseBlockAction.kt +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index fc47677..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/actions/UseItemAction.kt +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index 311a050..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/KeyboardInputHandler.kt +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index d3a630e..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/input/MouseInputHandler.kt +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index 8a813df..0000000 --- a/dagger-multibind-annotations/src/main/kotlin/ru.fredboy.cavedroid.ksp.annotations/render/Renderer.kt +++ /dev/null @@ -1,5 +0,0 @@ -package ru.fredboy.cavedroid.ksp.annotations.render - -@Target(AnnotationTarget.CLASS) -@Retention(AnnotationRetention.SOURCE) -annotation class Renderer diff --git a/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/GenerateSetMultibindingsSymbolProcessor.kt b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/GenerateSetMultibindingsSymbolProcessor.kt new file mode 100644 index 0000000..18cd3c9 --- /dev/null +++ b/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/GenerateSetMultibindingsSymbolProcessor.kt @@ -0,0 +1,81 @@ +package ru.fredboy.cavedroid.ksp.processor + +import com.google.devtools.ksp.processing.* +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.symbol.KSType +import com.squareup.kotlinpoet.* +import com.squareup.kotlinpoet.ksp.toClassName +import com.squareup.kotlinpoet.ksp.writeTo +import ru.fredboy.cavedroid.ksp.annotations.GenerateSetMultibindingsModule + +class GenerateSetMultibindingsSymbolProcessor( + private val codeGenerator: CodeGenerator, + private val logger: KSPLogger, +) : SymbolProcessor { + + private fun generateModule( + interfaceName: ClassName, + moduleName: ClassName, + classes: List + ): FileSpec? { + if (classes.isEmpty()) { + return null + } + + val bindings = classes.map { clazz -> + FunSpec.builder("bind${clazz.simpleName}") + .addAnnotation(ClassName("dagger", "Binds")) + .addAnnotation(ClassName("dagger.multibindings", "IntoSet")) + .addParameter(ParameterSpec("impl", clazz)) + .returns(interfaceName) + .addCode("return impl") + .build() + } + + val moduleObject = TypeSpec.objectBuilder(moduleName) + .addAnnotation(ClassName("dagger", "Module")) + .addFunctions(bindings) + .build() + + return FileSpec.builder(moduleName) + .addType(moduleObject) + .build() + + } + + private fun processAnnotation(resolver: Resolver, annotation: KSClassDeclaration) { + val args = annotation.annotations.first { + it.shortName.getShortName() == "GenerateSetMultibindingsModule" + }.arguments.takeIf { it.size == 3 } ?: run { + logger.error("GenerateSetMultibindingsModule should have 3 arguments") + throw IllegalArgumentException() + } + + val interfaceName = args.first { it.name?.getShortName() == "interfaceClass" }.value as KSType + val modulePackage = args.first { it.name?.getShortName() == "modulePackage" }.value as String + val moduleName = args.first { it.name?.getShortName() == "moduleName" }.value as String + + val moduleClassName = ClassName(modulePackage, moduleName) + val elements = resolver.getSymbolsWithAnnotation(annotation.qualifiedName!!.asString()) + .filterIsInstance() + .map(KSClassDeclaration::toClassName) + .toList() + + logger.info("Found elements: ${elements.joinToString()}") + + generateModule( + interfaceName = interfaceName.toClassName(), + moduleName = moduleClassName, + classes = elements + )?.writeTo(codeGenerator, Dependencies(true)) + } + + override fun process(resolver: Resolver): List { + val annotations = resolver.getAnnotatedClasses(GenerateSetMultibindingsModule::class.qualifiedName!!, logger) + logger.info("Found annotations: ${annotations.joinToString { it.qualifiedName!!.asString() }}") + annotations.forEach { processAnnotation(resolver, it) } + return emptyList() + } + +} \ No newline at end of file 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 deleted file mode 100644 index d97c2ab..0000000 --- a/dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/processor/RendererSymbolProcessor.kt +++ /dev/null @@ -1,66 +0,0 @@ -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 index 0b0e0b9..1ac8a6a 100644 --- 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 @@ -1,5 +1,6 @@ package ru.fredboy.cavedroid.ksp.processor +import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSNode @@ -7,11 +8,11 @@ import com.google.devtools.ksp.validate import kotlin.reflect.KClass internal fun Resolver.getAnnotatedClasses( - packageName: String, - annotationClass: KClass<*> + annotation: String, + logger: KSPLogger, ): Sequence { - return getSymbolsWithAnnotation(annotationClass.qualifiedName.orEmpty()) + logger.info("Resolving annotation $annotation") + return getSymbolsWithAnnotation(annotation) .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/GenerateSetMultibindingsSymbolProcessorProvider.kt similarity index 59% rename from dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/RendererSymbolProcessorProvider.kt rename to dagger-multibind-ksp/src/main/kotlin/ru/fredboy/cavedroid/ksp/provider/GenerateSetMultibindingsSymbolProcessorProvider.kt index eedf89b..c8f4e15 100644 --- 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/GenerateSetMultibindingsSymbolProcessorProvider.kt @@ -3,13 +3,14 @@ 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 +import ru.fredboy.cavedroid.ksp.processor.GenerateSetMultibindingsSymbolProcessor -internal class RendererSymbolProcessorProvider : SymbolProcessorProvider { +internal class GenerateSetMultibindingsSymbolProcessorProvider : SymbolProcessorProvider { override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { - return RendererSymbolProcessor( + return GenerateSetMultibindingsSymbolProcessor( codeGenerator = environment.codeGenerator, + logger = environment.logger, ) } 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 index a8a853f..293ff25 100644 --- 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 @@ -1 +1 @@ -ru.fredboy.cavedroid.ksp.provider.RendererSymbolProcessorProvider +ru.fredboy.cavedroid.ksp.provider.GenerateSetMultibindingsSymbolProcessorProvider diff --git a/desktop/build.gradle b/desktop/build.gradle index 04317e4..58553a3 100644 --- a/desktop/build.gradle +++ b/desktop/build.gradle @@ -46,12 +46,12 @@ task dist(type: Jar) { attributes 'Main-Class': project.mainClassName } from { - configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } + configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } with jar } -dist.dependsOn classes +dist.dependsOn build dependencies { implementation project(":core") -- 2.29.2