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

com.ancientlightstudios.quarkus.kotlin.openapi.emitter.EmitterContext.kt Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package com.ancientlightstudios.quarkus.kotlin.openapi.emitter

import com.ancientlightstudios.quarkus.kotlin.openapi.Config
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.KotlinFile
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformable.TransformableSpec
import kotlin.io.path.Path

class EmitterContext(val spec: TransformableSpec, private val config: Config) {

    private val outputDirectory = Path(config.outputDirectory)

    fun getAdditionalImports() = config.additionalImports()

    fun  runEmitter(codeEmitter: T): T = codeEmitter.apply { emit() }

    fun KotlinFile.writeFile() {
        val packageName = fileName.packageName
        val targetPath = if (packageName.isNotBlank()) {
            val subPath = packageName.split(".")
            outputDirectory.resolve(Path(subPath.first(), *subPath.drop(1).toTypedArray()))
        } else {
            outputDirectory
        }

        targetPath.toFile().mkdirs()

        val outputFile = targetPath.resolve("${fileName.value}.kt").toFile()
        check(outputFile.exists() || outputFile.createNewFile()) { "Could not create file $outputFile" }

        outputFile.bufferedWriter().use {
            render(CodeWriter(it))
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy