Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Jun 18, 2024
1 parent 8c284ad commit 7c48575
Show file tree
Hide file tree
Showing 26 changed files with 549 additions and 62 deletions.
4 changes: 2 additions & 2 deletions kotlin-code-generation/src/main/kotlin/_builder.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.toolisticon.kotlin.generation

import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.spec.KotlinConstructorProperty
import io.toolisticon.kotlin.generation.spec.KotlinConstructorPropertySpec
import java.util.function.Supplier

interface BuilderSupplier<P : Any, B : Any> : Builder<P>, Supplier<B>

interface SpecSupplier<T> : Supplier<T>
interface AnnotationSpecSupplier1 : SpecSupplier<AnnotationSpec>
interface ConstructorPropertySupplier1 : SpecSupplier<KotlinConstructorProperty>
interface ConstructorPropertySupplier1 : SpecSupplier<KotlinConstructorPropertySpec>
interface FileSpecSupplier1 : SpecSupplier<FileSpec>
interface FunSpecSupplier1 : SpecSupplier<FunSpec>
interface ParameterSpecSupplier1 : SpecSupplier<ParameterSpec>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ class KotlinAnnotationClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinAnnotationClassSpec, TypeSpec>, KotlinAnnotationClassSpecSupplier, DelegatingBuilder<KotlinAnnotationClassSpecBuilder, TypeSpecBuilderReceiver> {

// companion object {
// fun builder(className: ClassName) = KotlinAnnotationClassBuilder(
// className = className,
// delegate = TypeSpecBuilder.annotationBuilder(className)
// )
// }

override fun builder(block: TypeSpecBuilderReceiver) = apply{
companion object {

@JvmStatic
fun annotationBuilder(name: String): KotlinAnnotationClassSpecBuilder = annotationBuilder(ClassName("", name))

@JvmStatic
fun annotationBuilder(className: ClassName): KotlinAnnotationClassSpecBuilder = KotlinAnnotationClassSpecBuilder(
className = className,
delegate = TypeSpecBuilder.annotationBuilder(className.simpleName)
)
}

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.*
import io.toolisticon.kotlin.generation.BuilderSupplier
import io.toolisticon.kotlin.generation.poet.AnnotationSpecBuilder
import io.toolisticon.kotlin.generation.poet.AnnotationSpecBuilderReceiver
Expand All @@ -13,13 +12,26 @@ import kotlin.reflect.KClass
class KotlinAnnotationSpecBuilder internal constructor(
private val delegate: AnnotationSpecBuilder
) : BuilderSupplier<KotlinAnnotationSpec, AnnotationSpec>, KotlinAnnotationSpecSupplier, DelegatingBuilder<KotlinAnnotationSpecBuilder, AnnotationSpecBuilderReceiver> {
// companion object {
// fun builder(type: ClassName): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(AnnotationSpecBuilder.builder(type))
// fun builder(type: ParameterizedTypeName): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(AnnotationSpecBuilder.Companion.builder(type))
// fun builder(type: KClass<out Annotation>): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(AnnotationSpecBuilder.builder(type))
//
// fun from(spec: KotlinAnnotationSpec) = KotlinAnnotationSpecBuilder(delegate = spec.get().toBuilder().wrap())
// }
companion object {
@JvmStatic
fun builder(
type: ClassName
): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(
delegate = AnnotationSpecBuilder.builder(type)
)

@JvmStatic
fun builder(
type: ParameterizedTypeName
): KotlinAnnotationSpecBuilder = KotlinAnnotationSpecBuilder(
delegate = AnnotationSpecBuilder.builder(type)
)

@JvmStatic
fun builder(
type: KClass<out Annotation>
): KotlinAnnotationSpecBuilder = builder(type.asClassName())
}

fun addMember(format: String, vararg args: Any): KotlinAnnotationSpecBuilder = apply {
TODO() //delegate.addMember(format, *args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class KotlinAnonymousClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinAnonymousClassSpec, TypeSpec>, KotlinAnonymousClassSpecSupplier, DelegatingBuilder<KotlinAnonymousClassSpecBuilder, TypeSpecBuilderReceiver> {

companion object {
@JvmStatic
fun anonymousClassBuilder(): KotlinAnonymousClassSpecBuilder = KotlinAnonymousClassSpecBuilder(
delegate = TypeSpecBuilder.anonymousClassBuilder()
)
}

override fun build(): KotlinAnonymousClassSpec = KotlinAnonymousClassSpec(delegate.build())
override fun get(): TypeSpec = build().get()
override fun spec(): KotlinAnonymousClassSpec = build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ class KotlinClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinClassSpec, TypeSpec>, KotlinClassSpecSupplier, DelegatingBuilder<KotlinClassSpecBuilder, TypeSpecBuilderReceiver> {

// companion object {
// fun builder(className: ClassName) = KotlinClassBuilder(
// delegate = TypeSpecBuilder.classBuilder(className)
// )
// }
companion object {
@JvmStatic
fun classBuilder(name: String): KotlinClassSpecBuilder = KotlinClassSpecBuilder(
className = ClassName("", name),
delegate = TypeSpecBuilder.classBuilder(name)
)

@JvmStatic
fun classBuilder(className: ClassName): KotlinClassSpecBuilder = KotlinClassSpecBuilder(
className = className,
delegate = TypeSpecBuilder.classBuilder(className.simpleName)
)

}

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class KotlinCompanionObjectSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinCompanionObjectSpec, TypeSpec>, KotlinCompanionObjectSpecSupplier, DelegatingBuilder<KotlinCompanionObjectSpecBuilder, TypeSpecBuilderReceiver> {

// companion object {
// fun builder(className: ClassName) = KotlinClassBuilder(
// delegate = TypeSpecBuilder.classBuilder(className)
// )
// }
companion object {
@JvmStatic
@JvmOverloads
fun companionObjectBuilder(name: String? = null): KotlinCompanionObjectSpecBuilder = KotlinCompanionObjectSpecBuilder(
delegate = TypeSpecBuilder.companionObjectBuilder(name)
)
}

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.TypeName
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.ConstructorPropertySupplier
import io.toolisticon.kotlin.generation.spec.KotlinConstructorProperty
import io.toolisticon.kotlin.generation.spec.KotlinConstructorPropertySpec
import io.toolisticon.kotlin.generation.spec.KotlinConstructorPropertySpecSupplier

class KotlinConstructorPropertyBuilder internal constructor(
class KotlinConstructorPropertySpecBuilder internal constructor(
val name: String,
private val type: TypeName,
private val propertyBuilder: KotlinPropertySpecBuilder,
private val parameterBuilder: KotlinParameterSpecBuilder
) : Builder<KotlinConstructorProperty>, ConstructorPropertySupplier {
) : Builder<KotlinConstructorPropertySpec>, KotlinConstructorPropertySpecSupplier {

// companion object {
// fun builder(name: String, type: TypeName): KotlinConstructorPropertyBuilder = KotlinConstructorPropertyBuilder(
Expand All @@ -32,16 +32,16 @@ class KotlinConstructorPropertyBuilder internal constructor(
// }
// }

override fun build(): KotlinConstructorProperty {
override fun build(): KotlinConstructorPropertySpec {
val parameter = parameterBuilder.build()
val property = propertyBuilder
.builder {
initializer(parameter.name)
}
.build()

return KotlinConstructorProperty(parameter = parameter, property = property)
return KotlinConstructorPropertySpec(parameter = parameter, property = property)
}

override fun get(): KotlinConstructorProperty = build().get()
override fun spec(): KotlinConstructorPropertySpec = build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ import io.toolisticon.kotlin.generation.spec.KotlinDataClassSpec
import io.toolisticon.kotlin.generation.spec.KotlinDataClassSpecSupplier
import mu.KLogging


class KotlinDataClassSpecBuilder internal constructor(
private val className: ClassName,
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinDataClassSpec, TypeSpec>, KotlinDataClassSpecSupplier, DelegatingBuilder<KotlinDataClassSpecBuilder, TypeSpecBuilderReceiver> {
companion object : KLogging() {}
companion object : KLogging() {

@JvmStatic
fun builder(name: String): KotlinDataClassSpecBuilder = KotlinDataClassSpecBuilder(
className = ClassName("", name),
delegate = TypeSpecBuilder.classBuilder(name)
)

@JvmStatic
fun builder(className: ClassName): KotlinDataClassSpecBuilder = KotlinDataClassSpecBuilder(className)
}

internal constructor(className: ClassName) : this(className = className, delegate = TypeSpecBuilder(TypeSpec.classBuilder(className)))
internal constructor(className: ClassName) : this(
className = className,
delegate = TypeSpecBuilder.classBuilder(className)
)

init {
delegate { addModifiers(KModifier.DATA) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.BuilderSupplier
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder.Companion.wrap
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilderReceiver
import io.toolisticon.kotlin.generation.spec.KotlinEnumClassSpec
import io.toolisticon.kotlin.generation.spec.KotlinEnumClassSpecSupplier
Expand All @@ -11,6 +13,17 @@ class KotlinEnumClassSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinEnumClassSpec, TypeSpec>, KotlinEnumClassSpecSupplier, DelegatingBuilder<KotlinEnumClassSpecBuilder, TypeSpecBuilderReceiver> {

companion object {
@JvmStatic
fun enumBuilder(name: String): KotlinEnumClassSpecBuilder = KotlinEnumClassSpecBuilder(
delegate = TypeSpecBuilder.enumBuilder(name)
)

@JvmStatic
fun enumBuilder(className: ClassName): KotlinEnumClassSpecBuilder = enumBuilder(className.simpleName)

}

// companion object :KLogging() {
// fun builder(className: ClassName) = KotlinEnumClassSpecBuilder(
// delegate = TypeSpecBuilder.enumBuilder(className)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.MemberName
import io.toolisticon.kotlin.generation.BuilderSupplier
import io.toolisticon.kotlin.generation.poet.FileSpecBuilder
import io.toolisticon.kotlin.generation.poet.FileSpecBuilderReceiver
Expand All @@ -11,7 +13,37 @@ import io.toolisticon.kotlin.generation.spec.KotlinFileSpecSupplier
class KotlinFileSpecBuilder internal constructor(
private val delegate: FileSpecBuilder
) : BuilderSupplier<KotlinFileSpec, FileSpec>, KotlinFileSpecSupplier, DelegatingBuilder<KotlinFileSpecBuilder, FileSpecBuilderReceiver> {
companion object {
@JvmStatic
fun builder(
className: ClassName
): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.builder(className)
)

@JvmStatic
fun builder(
memberName: MemberName
): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.builder(memberName)
)

@JvmStatic
fun builder(
packageName: String,
fileName: String
): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.builder(packageName, fileName)
)

@JvmStatic
fun scriptBuilder(
fileName: String,
packageName: String = ""
): KotlinFileSpecBuilder = KotlinFileSpecBuilder(
delegate = FileSpecBuilder.scriptBuilder(fileName, packageName)
)
}

override fun builder(block: FileSpecBuilderReceiver) = apply {
delegate { block() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.MemberName
import io.toolisticon.kotlin.generation.BuilderSupplier
import io.toolisticon.kotlin.generation.poet.FunSpecBuilder
import io.toolisticon.kotlin.generation.poet.FunSpecBuilderReceiver
Expand All @@ -13,9 +14,36 @@ class KotlinFunSpecBuilder internal constructor(
KotlinFunSpecSupplier,
DelegatingBuilder<KotlinFunSpecBuilder, FunSpecBuilderReceiver> {

//companion object {
// fun builder(name: String) = KotlinFunSpecBuilder(FunSpecBuilder.builder(name))
// }
companion object {
@JvmStatic
fun builder(
name: String
): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.builder(name)
)

@JvmStatic
fun builder(
memberName: MemberName
): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.builder(memberName)
)

@JvmStatic
fun constructorBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.constructorBuilder()
)

@JvmStatic
fun getterBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.getterBuilder()
)

@JvmStatic
fun setterBuilder(): KotlinFunSpecBuilder = KotlinFunSpecBuilder(
delegate = FunSpecBuilder.setterBuilder()
)
}

override fun builder(block: FunSpecBuilderReceiver): KotlinFunSpecBuilder = apply {
delegate { block() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.BuilderSupplier
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder
Expand All @@ -10,11 +11,24 @@ import io.toolisticon.kotlin.generation.spec.KotlinInterfaceSpecSupplier
class KotlinInterfaceSpecBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : BuilderSupplier<KotlinInterfaceSpec, TypeSpec>, KotlinInterfaceSpecSupplier, DelegatingBuilder<KotlinInterfaceSpecBuilder, TypeSpecBuilderReceiver> {
// companion object {
// fun builder(className: ClassName) = KotlinInterfaceSpecBuilder(
// delegate = TypeSpecBuilder.Companion.interfaceBuilder(className)
// )
// }
companion object {
@JvmStatic
fun interfaceBuilder(name: String): KotlinInterfaceSpecBuilder = KotlinInterfaceSpecBuilder(
delegate = TypeSpecBuilder.interfaceBuilder(name)
)

@JvmStatic
fun interfaceBuilder(className: ClassName): KotlinInterfaceSpecBuilder = interfaceBuilder(className.simpleName)

@JvmStatic
fun funInterfaceBuilder(name: String): KotlinInterfaceSpecBuilder = KotlinInterfaceSpecBuilder(
delegate = TypeSpecBuilder.funInterfaceBuilder(name)
)

@JvmStatic
fun funInterfaceBuilder(className: ClassName): KotlinInterfaceSpecBuilder = funInterfaceBuilder(className.simpleName)

}

override fun builder(block: TypeSpecBuilderReceiver) = apply {
delegate.builder.block()
Expand Down
Loading

0 comments on commit 7c48575

Please sign in to comment.