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

builder.KotlinFileSpecBuilder.kt Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
@file:Suppress(SUPPRESS_UNUSED)

package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier
import io.toolisticon.kotlin.generation.poet.FileSpecBuilder
import io.toolisticon.kotlin.generation.poet.FileSpecBuilder.Companion.wrap
import io.toolisticon.kotlin.generation.poet.FileSpecBuilderReceiver
import io.toolisticon.kotlin.generation.poet.TypeAliasSpecSupplier
import io.toolisticon.kotlin.generation.poet.TypeSpecSupplier
import io.toolisticon.kotlin.generation.spec.*
import io.toolisticon.kotlin.generation.support.SUPPRESS_UNUSED
import kotlin.reflect.KClass

/**
 * Builder for [KotlinFileSpec].
 */
@ExperimentalKotlinPoetApi
class KotlinFileSpecBuilder internal constructor(
  private val delegate: FileSpecBuilder
) : BuilderSupplier, KotlinFileSpecSupplier, DelegatingBuilder,
  KotlinAnnotatableBuilder,
  KotlinMemberSpecHolderBuilder,
  KotlinTypeSpecHolderBuilder {
  companion object {
    /**
     * Creates new builder.
     */
    fun builder(className: ClassName): KotlinFileSpecBuilder {
      require(className.packageName.isNotEmpty()) { "cannot build file for empty package." }
      return KotlinFileSpecBuilder(delegate = FileSpecBuilder.builder(className))
    }

    /**
     * Creates new builder.
     */
    fun builder(memberName: MemberName): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
      delegate = FileSpecBuilder.builder(memberName)
    )

    /**
     * Creates new builder.
     */
    fun builder(packageName: String, fileName: String): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
      delegate = FileSpecBuilder.builder(packageName, fileName)
    )

    /**
     * Creates new builder.
     */
    fun scriptBuilder(fileName: String, packageName: String = ""): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
      delegate = FileSpecBuilder.scriptBuilder(fileName, packageName)
    )

    /**
     * Creates new builder.
     */
    fun builder(spec: KotlinFileSpec) = builder(spec.get())

    /**
     * Creates new builder.
     */
    fun builder(spec: FileSpec) = KotlinFileSpecBuilder(delegate = spec.toBuilder().wrap())
  }

  fun addAliasedImport(kclass: KClass<*>, alias: String) = builder { this.addAliasedImport(kclass, alias) }
  fun addAliasedImport(className: ClassName, alias: String) = builder { this.addAliasedImport(className, alias) }
  fun addAliasedImport(className: ClassName, memberName: String, alias: String) = builder { this.addAliasedImport(className, memberName, alias) }
  fun addAliasedImport(memberName: MemberName, alias: String) = builder { this.addAliasedImport(memberName, alias) }
  fun addBodyComment(format: String, vararg args: Any) = builder { this.addBodyComment(format, *args) }
  fun addCode(format: String, vararg args: Any?) = builder { this.addCode(format, *args) }
  fun addCode(codeBlock: CodeBlock) = builder { this.addCode(codeBlock) }
  fun addDefaultPackageImport(packageName: String) = builder { this.addDefaultPackageImport(packageName) }
  fun addFileComment(format: String, vararg args: Any) = builder { this.addFileComment(format, *args) }
  fun addImport(constant: Enum<*>) = builder { this.addImport(constant) }
  fun addImport(kclass: KClass<*>, vararg names: String) = builder { this.addImport(kclass, *names) }
  fun addImport(className: ClassName, vararg names: String) = builder { this.addImport(className, *names) }
  fun addImport(kclass: KClass<*>, names: Iterable) = builder { this.addImport(kclass, names) }
  fun addImport(className: ClassName, names: Iterable) = builder { this.addImport(className, names) }
  fun addImport(packageName: String, vararg names: String) = builder { this.addImport(packageName, *names) }
  fun addImport(packageName: String, names: Iterable) = builder { this.addImport(packageName, names) }
  fun addImport(import: Import) = builder { this.addImport(import) }
  fun addKotlinDefaultImports(includeJvm: Boolean = true, includeJs: Boolean = true) = builder { this.addKotlinDefaultImports(includeJvm, includeJs) }
  fun addNamedCode(format: String, args: Map) = builder { this.addNamedCode(format, args) }
  fun addStatement(format: String, vararg args: Any) = builder { this.addStatement(format, *args) }
  fun addTypeAlias(typeAliasSpec: TypeAliasSpecSupplier) = builder { this.addTypeAlias(typeAliasSpec.get()) }
  fun beginControlFlow(controlFlow: String, vararg args: Any) = builder { this.beginControlFlow(controlFlow, *args) }
  fun nextControlFlow(controlFlow: String, vararg args: Any) = builder { this.nextControlFlow(controlFlow, *args) }
  fun endControlFlow() = builder { this.endControlFlow() }

  override fun build(): KotlinFileSpec {
    val spec = delegate.build()
    return KotlinFileSpec(spec = spec)
  }

  // region [overrides]
  override val className: ClassName = delegate.className
  override fun addAnnotation(spec: KotlinAnnotationSpecSupplier) = apply { delegate.addAnnotation(spec.get()) }
  override fun addFunction(funSpec: KotlinFunSpecSupplier) = apply { delegate.addFunction(funSpec.get()) }
  override fun addProperty(propertySpec: KotlinPropertySpecSupplier) = apply { delegate.addProperty(propertySpec.get()) }
  override fun addType(typeSpec: TypeSpecSupplier) = builder { this.addType(typeSpec.get()) }
  override fun addTag(type: KClass<*>, tag: Any?) = builder { this.tag(type, tag) }
  override fun builder(block: FileSpecBuilderReceiver) = apply { delegate.builder.block() }
  override fun get(): FileSpec = build().get()
  override fun spec(): KotlinFileSpec = build()
  // endregion [overrides]
}

@ExperimentalKotlinPoetApi
typealias KotlinFileSpecBuilderReceiver = KotlinFileSpecBuilder.() -> Unit




© 2015 - 2024 Weber Informatics LLC | Privacy Policy