Hi! I have a quick kapt questions. I'm currently c...
# kapt
e
Hi! I have a quick kapt questions. I'm currently creating a multi-project build with gradle and kotlin. I've currently split it up so that I have one library of shared files, and then a bunch of subprojects that import that shared lib project. One of the subprojects should take a list of classes with an annotation of @Deploy from the shared library, and for each of those classes, wrap it in the code for an AWS lambda, and then put that lambda in it's own jar. In short, what I'm trying to do is: 1. Find all the files in the shared lib that have an annotation of @Deploy. This @Deploy annotation lives in the shared library. 2. Based off of https://willowtreeapps.com/ideas/generating-code-via-annotations-in-kotlin, I've created a project for the annotation processor. That project should create a generated file for each class with @Deploy. 3. Generate a jar for each of those files that I can deploy to AWS as a lambda. I've got 1 done, which works well. My problem is in step 2. Here's my processor code.
Copy code
package com._98point6.lambda.processors

import com._98point6.workflows.common.Workflow
import <http://java.io|java.io>.File
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.RoundEnvironment
import javax.annotation.processing.SupportedOptions
import javax.annotation.processing.SupportedSourceVersion
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic
import com._98point6.workflows.annotations.Workflow as WorkflowAnnotation

@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions(LambdaWorkflowProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME)
class LambdaWorkflowProcessor : AbstractProcessor() {
    override fun getSupportedAnnotationTypes(): MutableSet<String> {
        return mutableSetOf(WorkflowAnnotation::class.java.canonicalName)
    }

    override fun getSupportedSourceVersion(): SourceVersion {
        return SourceVersion.latest()
    }

    override fun process(set: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean {        roundEnvironment?.getElementsAnnotatedWith(WorkflowAnnotation::class.java)?.forEach {
            val workflow = it as Workflow
            val packageName = "com._98point6.lambda.workflows"
            val className = "${workflow.type}_${workflow.version.replace(".", "_")}".toUpperCase()
            val fileContent = buildClass(className, packageName)
            val kaptKotlinGeneratedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME]
            if (kaptKotlinGeneratedDir?.isEmpty() != true) {
                processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, "Can't find the target directory for generated Kotlin files.")
                return false
            }
            val file = File(kaptKotlinGeneratedDir, "$className.kt")
            file.writeText(fileContent)
        }
        return true
    }

    companion object {
        const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
    }

    private fun buildClass(className: String, packageName: String): String {
        return """
            package $packageName
            
            import com._98point6.lambda.workflow.common.Manifest
            import com._98point6.workflows.workflows.${className.toLowerCase()}.Workflow
            import com._98point6.lambda.workflow.common.base.Lambda as BaseLambda
            
            class $className {
                class Lambda : BaseLambda(workflow = Workflow) {
                    companion object {
                        @JvmStatic
                        fun main(args: Array<String>) {
                            Manifest(Workflow).writeManifest(args[0])
                        }
                    }
                }
            }
            """.trimIndent()
    }
}