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

poet.FileSpecBuilder.kt Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.className
import io.toolisticon.kotlin.generation.WithClassName
import kotlin.reflect.KClass

/**
 * Wraps [FileSpec.Builder] for typesafe access.
 */
@ExperimentalKotlinPoetApi
class FileSpecBuilder(
  override val builder: FileSpec.Builder
) : PoetSpecBuilder,
  FileSpecSupplier,
  PoetAnnotatableBuilder,
  PoetMemberSpecHolderBuilder,
  PoetTypeSpecHolderBuilder,
  PoetTaggableBuilder,
  WithClassName {
  companion object {
    internal fun FileSpec.Builder.wrap() = FileSpecBuilder(this)

    fun get(packageName: String, typeSpec: TypeSpec): FileSpec = FileSpec.get(packageName, typeSpec)
    fun builder(className: ClassName): FileSpecBuilder = FileSpec.builder(className).wrap()
    fun builder(memberName: MemberName): FileSpecBuilder = FileSpec.builder(memberName).wrap()
    fun builder(packageName: String, fileName: String): FileSpecBuilder = FileSpec.builder(packageName, fileName).wrap()
    fun scriptBuilder(fileName: String, packageName: String = ""): FileSpecBuilder = FileSpec.scriptBuilder(fileName, packageName).wrap()
  }

  // AnnotatableBuilder
  override fun addAnnotation(annotationSpec: AnnotationSpec) = apply { builder.addAnnotation(annotationSpec) }
  override fun addAnnotations(annotationSpecs: Iterable) = apply { builder.addAnnotations(annotationSpecs) }

  // MemberSpecHolderBuilder
  override fun addFunction(funSpec: FunSpec) = apply { builder.addFunction(funSpec) }
  override fun addFunctions(funSpecs: Iterable) = apply { builder.addFunctions(funSpecs) }
  override fun addProperty(propertySpec: PropertySpec) = apply { builder.addProperty(propertySpec) }
  override fun addProperties(propertySpecs: Iterable) = apply { builder.addProperties(propertySpecs) }

  // TypeSpecHolderBuilder
  override fun addType(typeSpec: TypeSpec) = apply { builder.addType(typeSpec) }
  override fun addTypes(typeSpecs: Iterable) = apply { builder.addTypes(typeSpecs) }

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

  override fun tag(type: KClass<*>, tag: Any?): FileSpecBuilder = apply { builder.tag(type, tag) }

  override fun build(): FileSpec = builder.build()
  override val className: ClassName = className(builder.packageName, builder.name)
}

interface FileSpecSupplier : PoetSpecSupplier
typealias FileSpecBuilderReceiver = FileSpec.Builder.() -> Unit




© 2015 - 2024 Weber Informatics LLC | Privacy Policy