Skip to content

Commit

Permalink
processor: multi-engine support #35
Browse files Browse the repository at this point in the history
  • Loading branch information
Bethibande committed May 1, 2024
1 parent 2f4603e commit 824cc43
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 59 deletions.
43 changes: 41 additions & 2 deletions processor/src/main/kotlin/com/bethibande/actors/KSPProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
package com.bethibande.actors

import com.bethibande.actors.generation.ActorGenerator
import com.bethibande.actors.collectors.ActorStateCollector
import com.bethibande.actors.collectors.ActorStateFieldCollector
import com.bethibande.actors.engine.EngineData
import com.bethibande.actors.engine.EngineEnvironment
import com.bethibande.actors.engine.GenerationEngine
import com.bethibande.actors.engine.kotlinpoet.KotlinPoetEngine
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSAnnotated
import java.util.ServiceLoader

class KSPProcessor(private val environment: SymbolProcessorEnvironment): SymbolProcessor {

companion object {
const val DEFAULT_ENGINE = KotlinPoetEngine.ENGINE_NAME
}

private fun findEngine(name: String): GenerationEngine? = ServiceLoader.load(
GenerationEngine::class.java,
GenerationEngine::class.java.classLoader
)
.firstOrNull { it.name().equals(name, true) }

private fun listEngines(): String = ServiceLoader.load(
GenerationEngine::class.java,
GenerationEngine::class.java.classLoader
).joinToString { it.name() }

private fun collectData(resolver: Resolver, environment: SymbolProcessorEnvironment): EngineData {
val fieldResolver = ActorStateFieldCollector(resolver)

val stateTypes = ActorStateCollector().collect(resolver, environment)
stateTypes.forEach { type -> fieldResolver.collectFields(type) }

return EngineData(stateTypes)
}

override fun process(resolver: Resolver): List<KSAnnotated> {
ActorGenerator().generate(resolver, environment)
val data = collectData(resolver, environment)

val engineName = DEFAULT_ENGINE
val engine = findEngine(engineName) ?: throw IllegalArgumentException("Unknown engine: $engineName, available engines are: ${listEngines()}")

engine.generate(
EngineEnvironment(environment, resolver, emptyMap()),
data
)

return emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bethibande.actors.engine

import com.bethibande.actors.struct.ActorStateType

data class EngineData(
val types: List<ActorStateType>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bethibande.actors.engine

import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment

data class EngineEnvironment(
val environment: SymbolProcessorEnvironment,
val resolver: Resolver,
val options: Map<String, String>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bethibande.actors.engine

interface GenerationEngine {

fun name(): String
fun generate(context: EngineEnvironment, data: EngineData)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bethibande.actors.engine.kotlinpoet

import com.bethibande.actors.engine.kotlinpoet.commands.CommandGenerator
import com.bethibande.actors.struct.ActorStateType
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment

class ActorGenerator {

fun generate(value: ActorStateType, environment: SymbolProcessorEnvironment) {
CommandGenerator.generate(environment, value)
ReferenceGenerator.generate(environment, value)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bethibande.actors.generation
package com.bethibande.actors.engine.kotlinpoet

import com.google.devtools.ksp.processing.SymbolProcessorEnvironment

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bethibande.actors.engine.kotlinpoet

import com.bethibande.actors.engine.EngineData
import com.bethibande.actors.engine.EngineEnvironment
import com.bethibande.actors.engine.GenerationEngine

class KotlinPoetEngine: GenerationEngine {

companion object {
const val ENGINE_NAME = "kotlinpoet"
}

override fun name(): String = ENGINE_NAME

override fun generate(context: EngineEnvironment, data: EngineData) {
val generator = ActorGenerator()
data.types.forEach { type -> generator.generate(type, context.environment) }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bethibande.actors.generation
package com.bethibande.actors.engine.kotlinpoet

import com.bethibande.actors.Actor
import com.bethibande.actors.ActorReference
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bethibande.actors.generation
package com.bethibande.actors.engine.kotlinpoet

import com.bethibande.actors.struct.ActorStateField
import com.bethibande.actors.struct.ActorStateType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bethibande.actors.generation
package com.bethibande.actors.engine.kotlinpoet

import com.squareup.kotlinpoet.TypeSpec

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bethibande.actors.generation.behavior
package com.bethibande.actors.engine.kotlinpoet.behavior

import com.bethibande.actors.Actor
import com.bethibande.actors.behavior.Behavior
import com.bethibande.actors.generation.TypeSpecGenerator
import com.bethibande.actors.generation.commands.CloseCommandGenerator
import com.bethibande.actors.engine.kotlinpoet.TypeSpecGenerator
import com.bethibande.actors.engine.kotlinpoet.commands.CloseCommandGenerator
import com.bethibande.actors.struct.ActorStateType
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bethibande.actors.generation.behavior
package com.bethibande.actors.engine.kotlinpoet.behavior

import com.bethibande.actors.Actor
import com.bethibande.actors.behavior.Behavior
import com.bethibande.actors.generation.TypeSpecGenerator
import com.bethibande.actors.generation.commands.ContainsCommandGenerator
import com.bethibande.actors.engine.kotlinpoet.TypeSpecGenerator
import com.bethibande.actors.engine.kotlinpoet.commands.ContainsCommandGenerator
import com.bethibande.actors.struct.ActorStateField
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bethibande.actors.generation.behavior
package com.bethibande.actors.engine.kotlinpoet.behavior

import com.bethibande.actors.Actor
import com.bethibande.actors.behavior.Behavior
import com.bethibande.actors.generation.TypeSpecGenerator
import com.bethibande.actors.engine.kotlinpoet.TypeSpecGenerator
import com.bethibande.actors.struct.ActorStateField
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bethibande.actors.generation.behavior
package com.bethibande.actors.engine.kotlinpoet.behavior

import com.bethibande.actors.Actor
import com.bethibande.actors.behavior.Behavior
import com.bethibande.actors.generation.TypeSpecGenerator
import com.bethibande.actors.engine.kotlinpoet.TypeSpecGenerator
import com.bethibande.actors.struct.ActorStateField
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bethibande.actors.generation.commands
package com.bethibande.actors.engine.kotlinpoet.commands

import com.bethibande.actors.generation.FileGenerator
import com.bethibande.actors.engine.kotlinpoet.FileGenerator
import com.bethibande.actors.struct.ActorStateType
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bethibande.actors.generation.commands
package com.bethibande.actors.engine.kotlinpoet.commands

import com.bethibande.actors.generation.FileGenerator
import com.bethibande.actors.engine.kotlinpoet.FileGenerator
import com.bethibande.actors.struct.ActorStateType
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bethibande.actors.generation.commands
package com.bethibande.actors.engine.kotlinpoet.commands

import com.bethibande.actors.generation.FileGenerator
import com.bethibande.actors.engine.kotlinpoet.FileGenerator
import com.bethibande.actors.struct.ActorStateField
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bethibande.actors.generation.commands
package com.bethibande.actors.engine.kotlinpoet.commands

import com.bethibande.actors.generation.FileGenerator
import com.bethibande.actors.engine.kotlinpoet.FileGenerator
import com.bethibande.actors.struct.ActorStateField
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bethibande.actors.generation.commands
package com.bethibande.actors.engine.kotlinpoet.commands

import com.bethibande.actors.generation.FileGenerator
import com.bethibande.actors.engine.kotlinpoet.FileGenerator
import com.bethibande.actors.struct.ActorStateType
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bethibande.actors.generation.commands
package com.bethibande.actors.engine.kotlinpoet.commands

import com.bethibande.actors.generation.FileGenerator
import com.bethibande.actors.engine.kotlinpoet.FileGenerator
import com.bethibande.actors.struct.ActorStateField
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bethibande.actors.struct

import com.bethibande.actors.generation.behavior.ContainsBehaviorGenerator
import com.bethibande.actors.generation.behavior.GetBehaviorGenerator
import com.bethibande.actors.generation.behavior.SetBehaviorGenerator
import com.bethibande.actors.generation.getValue
import com.bethibande.actors.engine.kotlinpoet.behavior.ContainsBehaviorGenerator
import com.bethibande.actors.engine.kotlinpoet.behavior.GetBehaviorGenerator
import com.bethibande.actors.engine.kotlinpoet.behavior.SetBehaviorGenerator
import com.bethibande.actors.engine.kotlinpoet.getValue
import com.bethibande.actors.util.KSPUtil
import com.bethibande.actors.util.StringUtil
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bethibande.actors.struct

import com.bethibande.actors.Actor
import com.bethibande.actors.generation.behavior.CloseBehaviorGenerator
import com.bethibande.actors.generation.getValue
import com.bethibande.actors.engine.kotlinpoet.behavior.CloseBehaviorGenerator
import com.bethibande.actors.engine.kotlinpoet.getValue
import com.bethibande.actors.util.KSPUtil
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.bethibande.actors.engine.kotlinpoet.KotlinPoetEngine

0 comments on commit 824cc43

Please sign in to comment.