spec.KotlinFileSpec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-code-generation Show documentation
Show all versions of kotlin-code-generation Show documentation
Wrapping core components for kotlin code generation with kotlin-poet.
The newest version!
package io.toolisticon.kotlin.generation.spec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import io.toolisticon.kotlin.generation.WithClassName
import io.toolisticon.kotlin.generation.poet.FileSpecSupplier
import kotlin.reflect.KClass
/**
* Represents a file.
*/
data class KotlinFileSpec(
private val spec: FileSpec
) : KotlinGeneratorSpec, KotlinFileSpecSupplier, TaggableSpec, KotlinFileSpecIterable {
val packageName: String = spec.packageName
val rootName: String = spec.name
override fun tag(type: KClass): T? = get().tag(type)
override val className: ClassName = ClassName(packageName, rootName)
val fqn: String = "$packageName.$rootName"
val fileName: String = "$fqn.kt"
override fun iterator(): Iterator = listOf(this).listIterator()
override fun spec(): KotlinFileSpec = this
override fun get(): FileSpec = spec
}
/**
* Marks the builder and the spec so they are interchangeable.
*/
interface KotlinFileSpecSupplier : KotlinGeneratorSpecSupplier, FileSpecSupplier, WithClassName {
override fun get(): FileSpec = spec().get()
}
/**
* List that contains multiple [KotlinFileSpec]s.
*/
@JvmInline
value class KotlinFileSpecList(private val fileSpecs: List) : List by fileSpecs, KotlinFileSpecIterable {
companion object {
/**
* Empty Specs.
*/
val EMPTY = KotlinFileSpecList(emptyList())
/**
* Create [KotlinFileSpecList] from given vararg iterables.
*/
fun of(vararg iterables: KotlinFileSpecIterable) = of(iterables.toList())
/**
* Create [KotlinFileSpecList] from given iterables.
*/
fun of(iterables: List) = KotlinFileSpecList(iterables.flatten())
/**
* Collect file spec suppliers
*/
fun collect(vararg fns: () -> KotlinFileSpec) = collect(fns.toList())
/**
* Collect file spec suppliers.
*/
fun collect(specs: List<() -> KotlinFileSpec>) = specs.fold(EMPTY) { acc, cur -> acc + cur() }
}
/**
* Create new instance from single spec.
*/
constructor(vararg fileSpec: KotlinFileSpec) : this(fileSpec.toList())
/**
* Create copy of this list and add new spec(s).
*/
operator fun plus(fileSpec: KotlinFileSpecIterable) = KotlinFileSpecList(fileSpecs + fileSpec)
}
/**
* Iterable that provides instances of [KotlinFileSpec].
*/
sealed interface KotlinFileSpecIterable : Iterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy