Skip to content

Commit

Permalink
chore: refactor - move to poet package
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Jun 4, 2024
1 parent 47ef360 commit f694e04
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.toolisticon.kotlin.generation.BuilderSupplier
@JvmInline
value class CodeBlockBuilder(private val builder: CodeBlock.Builder) : BuilderSupplier<CodeBlock, Builder> {


fun isEmpty(): Boolean = builder.isEmpty()

fun isNotEmpty(): Boolean = builder.isNotEmpty()
Expand Down
64 changes: 64 additions & 0 deletions kotlin-code-generation/src/main/kotlin/poet/TypeSpecBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.toolisticon.kotlin.generation.poet

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.TypeSpec.Companion.anonymousClassBuilder
import io.toolisticon.kotlin.generation.BuilderSupplier
import kotlin.reflect.KClass

@JvmInline
@ExperimentalKotlinPoetApi
Expand Down Expand Up @@ -35,6 +37,68 @@ value class TypeSpecBuilder(private val builder: TypeSpec.Builder) : BuilderSupp
builder.addType(typeSpec)
}

fun addModifiers(vararg modifiers: KModifier): TypeSpecBuilder = apply {
builder.addModifiers(*modifiers)
}

fun addModifiers(modifiers: Iterable<KModifier>): TypeSpecBuilder = apply {
builder.addModifiers(modifiers)
}

fun addTypeVariables(typeVariables: Iterable<TypeVariableName>): TypeSpecBuilder = apply {
builder.addTypeVariables(typeVariables)
}

fun addTypeVariable(typeVariable: TypeVariableName): TypeSpecBuilder = apply {
builder.addTypeVariable(typeVariable)
}

fun primaryConstructor(primaryConstructor: FunSpec): TypeSpecBuilder = apply {
builder.primaryConstructor(primaryConstructor)
}

fun superclass(superclass: TypeName): TypeSpecBuilder = apply {
builder.superclass(superclass)
}

fun superclass(superclass: KClass<*>): TypeSpecBuilder = superclass(superclass.asTypeName())

fun addSuperclassConstructorParameter(format: String, vararg args: Any): TypeSpecBuilder = apply {
builder.addSuperclassConstructorParameter(format, *args)
}

fun addSuperclassConstructorParameter(codeBlock: CodeBlock): TypeSpecBuilder = apply {
builder.addSuperclassConstructorParameter(codeBlock)
}

fun addSuperclassConstructorParameter(builder: CodeBlockBuilder): TypeSpecBuilder = addSuperclassConstructorParameter(builder.build())

fun addSuperinterfaces(superinterfaces: Iterable<TypeName>): TypeSpecBuilder = apply {
builder.addSuperinterfaces(superinterfaces)
}

fun addSuperinterface(superinterface: TypeName, delegate: CodeBlock): TypeSpecBuilder = apply {
builder.addSuperinterface(superinterface, delegate)
}

fun addSuperinterface(superinterface: KClass<*>, delegate: CodeBlock): TypeSpecBuilder = addSuperinterface(superinterface.asTypeName(), delegate)

fun addSuperinterface(superinterface: KClass<*>, constructorParameterName: String): TypeSpecBuilder = addSuperinterface(superinterface.asTypeName(), constructorParameterName)

fun addSuperinterface(superinterface: TypeName, constructorParameter: String): TypeSpecBuilder = apply {
builder.addSuperinterface(superinterface, constructorParameter)
}

fun addEnumConstant(name: String, typeSpec: TypeSpec = anonymousClassBuilder().build()): TypeSpecBuilder = apply {
builder.addEnumConstant(name, typeSpec)
}

fun addInitializerBlock(block: CodeBlock): TypeSpecBuilder = apply {
builder.addInitializerBlock(block)
}

fun addInitializerBlock(builder: CodeBlockBuilder): TypeSpecBuilder = addInitializerBlock(builder.build())

override fun get(): TypeSpec.Builder = builder

override fun build(): TypeSpec = builder.build()
Expand Down

0 comments on commit f694e04

Please sign in to comment.