All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.darvld.wireframe.ProcessingEnvironment.kt Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package io.github.darvld.wireframe

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.TypeSpec
import graphql.schema.GraphQLNamedType
import io.github.darvld.wireframe.WireframeCompiler.Output
import io.github.darvld.wireframe.extensions.generateNameFor

/**Provides information about the environment in which the code will be generated.*/
public class ProcessingEnvironment internal constructor(
    public val projectName: String,
    public val basePackage: String,
) {
    @JvmInline
    private value class OutputKey(@Suppress("unused") val qualifiedName: String) {
        constructor(packageName: String, fileName: String) : this("$packageName.${fileName}")
    }

    private val packageNameCache: MutableMap = mutableMapOf()
    private val nameResolutionCache: MutableMap = mutableMapOf()

    private val outputs: MutableMap = mutableMapOf()

    public fun registerPackageFor(typeName: String, packageName: String) {
        packageNameCache.put(typeName, packageName)
    }

    public fun resolve(type: GraphQLNamedType): ClassName = nameResolutionCache.getOrPut(type) {
        ClassName(packageNameCache.getOrDefault(type.name, basePackage), generateNameFor(type))
    }

    public fun output(packageName: String, fileName: String, block: FileSpec.Builder.() -> Unit) {
        outputs.getOrPut(OutputKey(packageName, fileName)) { FileSpec.builder(packageName, fileName) }.apply(block)
    }

    public fun getOutputs(): Sequence {
        return outputs.values.asSequence().map { Output(it.build()) }
    }
}

public fun ProcessingEnvironment.output(className: ClassName, spec: TypeSpec) {
    output(className.packageName, className.simpleName, spec)
}

public fun ProcessingEnvironment.output(packageName: String, fileName: String, spec: TypeSpec) {
    output(packageName, fileName) { addType(spec) }
}

public fun ProcessingEnvironment.output(packageName: String, fileName: String, spec: FunSpec) {
    output(packageName, fileName) { addFunction(spec) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy