Skip to content

Commit

Permalink
refactor: wip, failing
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Jun 5, 2024
1 parent f694e04 commit b077abf
Show file tree
Hide file tree
Showing 56 changed files with 865 additions and 1,133 deletions.
4 changes: 2 additions & 2 deletions _itest/src/test/kotlin/HelloWorldExampleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.TypeSpec
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.builder.KotlinFileBuilder
import io.toolisticon.kotlin.generation.builder.KotlinFileSpecBuilder
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
Expand All @@ -30,7 +30,7 @@ internal class HelloWorldExampleTest {
)
.build()

val file = KotlinFileBuilder.builder(name).invoke {
val file = KotlinFileSpecBuilder.builder(name).invoke {
addType(type)
}.build()

Expand Down
37 changes: 16 additions & 21 deletions _itest/src/test/kotlin/KotlinDataClassSpecTest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.tschuchort.compiletesting.KotlinCompilation
import com.squareup.kotlinpoet.asTypeName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildDataClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.constructorPropertyBuilder
import io.toolisticon.kotlin.generation.spec.toFileSpec
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import io.toolisticon.kotlin.generation.builder.KotlinConstructorPropertyBuilder
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test
import kotlin.reflect.full.primaryConstructor

@OptIn(ExperimentalCompilerApi::class)
internal class KotlinDataClassSpecTest {
Expand All @@ -21,20 +15,21 @@ internal class KotlinDataClassSpecTest {
val className = ClassName("foo.bar", "Bar")

val spec = buildDataClass(className) {
addConstructorProperty(constructorPropertyBuilder("name", String::class))
addConstructorProperty(constructorPropertyBuilder("age", Int::class))
addConstructorProperty(KotlinConstructorPropertyBuilder.builder("name", String::class.asTypeName()))
addConstructorProperty(KotlinConstructorPropertyBuilder.builder("age", Int::class.asTypeName()))
}

val file = spec.toFileSpec()
println(file.code)

val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(file))

assertThat(result).errorMessages().isEmpty()
assertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)

val klass = result.loadClass()
assertThat(klass.primaryConstructor!!.call("hello world", 25))
.hasToString("Bar(name=hello world, age=25)")
// val file = spec.toFileSpec()
// println(file.code)

// val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(file))
//
// assertThat(result).errorMessages().isEmpty()
// assertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)
//
// val klass = result.loadClass()
// assertThat(klass.primaryConstructor!!.call("hello world", 25))
// .hasToString("Bar(name=hello world, age=25)")
// }
}
}
4 changes: 2 additions & 2 deletions _itest/src/test/kotlin/KotlinFileSpecTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import io.toolisticon.kotlin.generation.builder.KotlinFileBuilder
import io.toolisticon.kotlin.generation.builder.KotlinFileSpecBuilder
import org.junit.jupiter.api.Test

internal class KotlinFileSpecTest {
Expand All @@ -11,7 +11,7 @@ internal class KotlinFileSpecTest {

@Test
fun `build filespec`() {
val builder: KotlinFileBuilder = KotlinFileBuilder.builder(ClassName("foo", "Bar")) {
val builder: KotlinFileSpecBuilder = KotlinFileSpecBuilder.builder(ClassName("foo", "Bar")).invoke {
addFileComment("%L", "this is a comment.")
}

Expand Down
8 changes: 1 addition & 7 deletions _itest/src/test/kotlin/KotlinValueClassIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ internal class KotlinValueClassIT {

@Test
fun `generate value class wrapping string`() {
val builder = KotlinCodeGeneration.valueClassBuilder("Foo") {

//primaryConstructor(FUns)

}

println(builder.build().code)
TODO()
}
}
21 changes: 10 additions & 11 deletions kotlin-code-generation/src/main/kotlin/KotlinCodeGeneration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ package io.toolisticon.kotlin.generation
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.buildCodeBlock
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.Supressions.CLASS_NAME
import io.toolisticon.kotlin.generation.builder.*
import io.toolisticon.kotlin.generation.builder.KotlinAnnotationSpecBuilder
import io.toolisticon.kotlin.generation.builder.KotlinDataClassBuilder
import io.toolisticon.kotlin.generation.builder.KotlinValueClassBuilder
import io.toolisticon.kotlin.generation.spec.KotlinDataClassSpec

object KotlinCodeGeneration {

fun x() {
buildCodeBlock { }
}
fun buildValueClass(className: ClassName, block: KotlinValueClassBuilder.() -> Unit) = KotlinValueClassBuilder.builder(className)
.also(block)
.build()

inline fun buildDataClass(className: ClassName, receiver: KotlinDataClassBuilder.() -> Unit): KotlinDataClassSpec = KotlinDataClassBuilder.builder(className).also(receiver).build()
inline fun buildAnnotation(type: ClassName, block: KotlinAnnotationSpecBuilder.() -> Unit) = KotlinAnnotationSpecBuilder.builder(type)
.also(block)
.build()

val annotationBuilder = KotlinAnnotationBuilder.builder
val constructorPropertyBuilder = KotlinConstructorPropertyBuilder.builder
val parameterBuilder = KotlinParameterBuilder.builder
val propertyBuilder = KotlinPropertyBuilder.builder
val valueClassBuilder = KotlinValueClassBuilder.builder
inline fun buildDataClass(className: ClassName, receiver: KotlinDataClassBuilder.() -> Unit): KotlinDataClassSpec = KotlinDataClassBuilder.builder(className).also(receiver).build()

internal object Supressions {
const val CLASS_NAME = "ClassName"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@ package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.Supressions.CLASS_NAME
import io.toolisticon.kotlin.generation.builder.bak.ToKotlinPoetTypeSpecBuilder
import io.toolisticon.kotlin.generation.poet.ParameterSpecBuilder
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder
import io.toolisticon.kotlin.generation.spec.KotlinAnnotationClassSpec
import io.toolisticon.kotlin.generation.spec.TypeSpecSupplier

class KotlinAnnotationClassBuilder internal constructor(className: ClassName, delegate: TypeSpec.Builder) : KotlinPoetNamedTypeSpecBuilder<KotlinAnnotationClassSpec>(
className = className,
delegate = delegate
), TypeSpecSupplier {
class KotlinAnnotationClassBuilder internal constructor(
val className: ClassName,
private val delegate: TypeSpecBuilder
) : Builder<KotlinAnnotationClassSpec>, TypeSpecSupplier {

@Suppress(CLASS_NAME)
object builder : ToKotlinPoetTypeSpecBuilder<KotlinAnnotationClassSpec, KotlinAnnotationClassBuilder> {
override fun invoke(spec: KotlinAnnotationClassSpec, kind: TypeSpec.Kind, name: String?): KotlinAnnotationClassBuilder = TODO() // FIXME KotlinAnnotationClassBuilder(spec.get().toBuilder(kind,
// name))
companion object {
fun builder(className: ClassName) = KotlinAnnotationClassBuilder(
className = className,
delegate = TypeSpecBuilder.annotationBuilder(className)
)
}

operator fun invoke(block: TypeSpecBuilder.() -> Unit): KotlinAnnotationClassBuilder = apply {
delegate.block()
}

override fun build(): KotlinAnnotationClassSpec = KotlinAnnotationClassSpec(
className = className,
spec = delegate.build()
)

override fun get(): TypeSpec = build().get()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.ParameterizedTypeName
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.poet.AnnotationSpecBuilder
import io.toolisticon.kotlin.generation.spec.AnnotationSpecSupplier
import io.toolisticon.kotlin.generation.spec.KotlinAnnotationSpec
import kotlin.reflect.KClass

class KotlinAnnotationSpecBuilder internal constructor(
private val delegate: AnnotationSpecBuilder
) : Builder<KotlinAnnotationSpec>, AnnotationSpecSupplier {
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))
}

operator fun invoke(block: AnnotationSpecBuilder.() -> Unit): KotlinAnnotationSpecBuilder = apply {
delegate.block()
}

fun addMember(format: String, vararg args: Any): KotlinAnnotationSpecBuilder = apply {
delegate.addMember(format, *args)
}

fun addMember(codeBlock: CodeBlock): KotlinAnnotationSpecBuilder = apply {
delegate.addMember(codeBlock)
}

fun addKClassMember(name: String, klass: KClass<*>) = addMember("$name = %T::class", klass)

fun addStringMember(name: String, value: String) = addMember("$name = %S", value)


override fun build(): KotlinAnnotationSpec = KotlinAnnotationSpec(spec = delegate.build())
override fun get(): AnnotationSpec = build().get()
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.Supressions.CLASS_NAME
import io.toolisticon.kotlin.generation.builder.bak.BAK_KotlinPoetTypeSpecBuilder
import io.toolisticon.kotlin.generation.builder.bak.ToKotlinPoetTypeSpecBuilder
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder
import io.toolisticon.kotlin.generation.spec.KotlinAnonymousClassSpec
import io.toolisticon.kotlin.generation.spec.TypeSpecSupplier

@Deprecated("Not implemented yet!")
class KotlinAnonymousClassBuilder internal constructor(delegate: TypeSpec.Builder) : KotlinPoetTypeSpecBuilder<KotlinAnonymousClassSpec>(
delegate = delegate
), TypeSpecSupplier {
@Deprecated("not implemented yet")
class KotlinAnonymousClassBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : Builder<KotlinAnonymousClassSpec>, TypeSpecSupplier {


@Suppress(CLASS_NAME)
object builder : ToKotlinPoetTypeSpecBuilder<KotlinAnonymousClassSpec, KotlinAnonymousClassBuilder> {
override fun invoke(spec: KotlinAnonymousClassSpec, kind: TypeSpec.Kind, name: String?): KotlinAnonymousClassBuilder = KotlinAnonymousClassBuilder(spec.get().toBuilder(kind,name))
override fun build(): KotlinAnonymousClassSpec {
TODO("Not yet implemented")
}

override fun build(): KotlinAnonymousClassSpec {
override fun get(): TypeSpec {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.Supressions.CLASS_NAME
import io.toolisticon.kotlin.generation.builder.bak.BAK_KotlinPoetTypeSpecBuilder
import io.toolisticon.kotlin.generation.builder.bak.ToKotlinPoetTypeSpecBuilder
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder
import io.toolisticon.kotlin.generation.spec.KotlinClassSpec
import io.toolisticon.kotlin.generation.spec.TypeSpecSupplier

@Deprecated("Not implemented yet!")
class KotlinClassBuilder internal constructor(delegate: TypeSpec.Builder) : KotlinPoetTypeSpecBuilder<KotlinClassSpec>(
delegate = delegate
), TypeSpecSupplier {
class KotlinClassBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : Builder<KotlinClassSpec>, TypeSpecSupplier {


@Suppress(CLASS_NAME)
object builder : ToKotlinPoetTypeSpecBuilder<KotlinClassSpec, KotlinClassBuilder> {
override fun invoke(spec: KotlinClassSpec, kind: TypeSpec.Kind, name: String?): KotlinClassBuilder = KotlinClassBuilder(spec.get().toBuilder(kind,name))
companion object {
fun builder(className: ClassName) = KotlinClassBuilder(
delegate = TypeSpecBuilder.classBuilder(className)
)
}

operator fun invoke(block: TypeSpecBuilder.() -> Unit): KotlinClassBuilder = apply {
delegate.block()
}

override fun build(): KotlinClassSpec = KotlinClassSpec(spec = delegate.build())
override fun get(): TypeSpec = build().get()
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package io.toolisticon.kotlin.generation.builder

import com.squareup.kotlinpoet.TypeSpec
import io.toolisticon.kotlin.generation.Builder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.Supressions.CLASS_NAME
import io.toolisticon.kotlin.generation.builder.bak.ToKotlinPoetTypeSpecBuilder
import io.toolisticon.kotlin.generation.poet.TypeSpecBuilder
import io.toolisticon.kotlin.generation.spec.KotlinCompanionObjectSpec
import io.toolisticon.kotlin.generation.spec.TypeSpecSupplier

@Deprecated("Not implemented yet!")
class KotlinCompanionObjectBuilder internal constructor(delegate: TypeSpec.Builder) : KotlinPoetTypeSpecBuilder<KotlinCompanionObjectSpec>(
delegate = delegate
), TypeSpecSupplier {
class KotlinCompanionObjectBuilder internal constructor(
private val delegate: TypeSpecBuilder
) : Builder<KotlinCompanionObjectSpec>, TypeSpecSupplier {


@Suppress(CLASS_NAME)
object builder : ToKotlinPoetTypeSpecBuilder<KotlinCompanionObjectSpec, KotlinCompanionObjectBuilder> {
override fun invoke(spec: KotlinCompanionObjectSpec, kind: TypeSpec.Kind, name: String?): KotlinCompanionObjectBuilder = KotlinCompanionObjectBuilder(spec.get().toBuilder(kind,name))
companion object {
fun builder(name: String? = null) = KotlinCompanionObjectBuilder(
delegate = TypeSpecBuilder.companionObjectBuilder(name)
)
}
operator fun invoke(block: TypeSpecBuilder.() -> Unit): KotlinCompanionObjectBuilder = apply {
delegate.block()
}

override fun build(): KotlinCompanionObjectSpec = KotlinCompanionObjectSpec(spec = delegate.build())
override fun get(): TypeSpec = build().get()
}
Loading

0 comments on commit b077abf

Please sign in to comment.