Skip to content

Commit

Permalink
Merge pull request #6 from toolisticon/restart
Browse files Browse the repository at this point in the history
Restart
  • Loading branch information
jangalinski authored Jul 3, 2024
2 parents f79eadc + 10d618b commit 2335259
Show file tree
Hide file tree
Showing 108 changed files with 3,114 additions and 1,163 deletions.
21 changes: 3 additions & 18 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.1/maven-wrapper-3.3.1.jar
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.8/apache-maven-3.9.8-bin.zip
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@

* see [kotlin-poet](https://square.github.io/kotlinpoet/)

## Features

* KotlinAnnotationSpec
* KotlinAnnotationSpecBuilder
* KotlinFileSpec
* KotlinFileSpecBuilder
* KotlinFunSpec
* KotlinFunSpecBuilder
* KotlinParameterSpec
* KotlinParameterSpecBuilder
* KotlinPropertySpec
* KotlinPropertySpecBuilder



## Roadmap

### Specs to support

* com/squareup/kotlinpoet/AnnotationSpec
* com/squareup/kotlinpoet/FileSpec
* com/squareup/kotlinpoet/FunSpec
* com/squareup/kotlinpoet/ParameterSpec
* com/squareup/kotlinpoet/PropertySpec
* com/squareup/kotlinpoet/TypeAliasSpec
* com/squareup/kotlinpoet/TypeSpec

Expand Down
9 changes: 8 additions & 1 deletion _itest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-root</artifactId>
<artifactId>kotlin-code-generation-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../_mvn/parent/pom.xml</relativePath>
</parent>

<groupId>io.toolisticon.kotlin.generation.itest</groupId>
Expand Down Expand Up @@ -72,6 +73,12 @@
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi</arg>
<arg>-opt-in=com.squareup.kotlinpoet.ExperimentalKotlinPoetApi</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
2 changes: 1 addition & 1 deletion _itest/src/main/kotlin/KotlinCodeGenerationITest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.toolisticon.kotlin.generation.itest

object KotlinCodeGenerationITest {
// empty
const val ROOT_PACKAGE = "io.toolisticon.kotlin.generation.itest.created"
}
21 changes: 21 additions & 0 deletions _itest/src/main/kotlin/MyCustomAnnotationSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotationClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.itest.KotlinCodeGenerationITest.ROOT_PACKAGE

object MyCustomAnnotationSpec {
val name = ClassName(ROOT_PACKAGE, "MyCustomAnnotation")

val spec = buildAnnotationClass(name) {
mustBeDocumented()
retention(AnnotationRetention.RUNTIME)
target(AnnotationTarget.CLASS)
addConstructorProperty("value", String::class)
}

val file = buildFile(name) {
addType(spec)
}
}
44 changes: 19 additions & 25 deletions _itest/src/test/kotlin/HelloWorldExampleTest.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
@file:OptIn(ExperimentalCompilerApi::class)

package io.toolisticon.kotlin.generation.itest

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.KotlinCodeGeneration.buildClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFun
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
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class HelloWorldExampleTest {
Expand All @@ -20,30 +16,28 @@ internal class HelloWorldExampleTest {
fun `hello world from kotlin poet talk`() {
val name = ClassName("foo.bar", "HelloWorld")

val type = TypeSpec.classBuilder(name)
.addKdoc("%L", "Simple hello world class")
.addFunction(
FunSpec.builder("helloWorld")
.returns(String::class)
.addCode("return %S", "Hello World!")
.build()
)
.build()

val file = KotlinFileBuilder.builder(name).invoke {
val type = buildClass(name) {
addFunction(
buildFun("helloWorld") {
builder {
returns(String::class)
addCode("return %S", "Hello World!")
}
})
addKdoc("%L", "Simple hello world class")
}

val file = buildFile(name) {
addType(type)
}.build()

}
println(file.code)

Assertions.assertThat(file.packageName).isEqualTo("foo.bar")
assertThat(file.packageName).isEqualTo("foo.bar")

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

println(result)

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


KotlinCodeGenerationTest.assertThat(result).hasExitCode(KotlinCompilation.ExitCode.OK)
}
}
42 changes: 20 additions & 22 deletions _itest/src/test/kotlin/KotlinDataClassSpecTest.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.constructorPropertyBuilder
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.dataClassBuilder
import com.squareup.kotlinpoet.asTypeName
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildDataClass
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.builder.dataClassBuilder
import io.toolisticon.kotlin.generation.builder.KotlinDataClassSpecBuilder
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 org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.Test
import kotlin.reflect.full.primaryConstructor

@OptIn(ExperimentalCompilerApi::class)
internal class KotlinDataClassSpecTest {

@Test
fun `create simple data class`() {
val className = ClassName("foo.bar", "Bar")
val builder: KotlinDataClassSpecBuilder = dataClassBuilder(className)
.addConstructorProperty(constructorPropertyBuilder("name", String::class))
.addConstructorProperty(constructorPropertyBuilder("age", Int::class))
.addConstructorProperty("name", String::class)
.addConstructorProperty("age", Int::class)

val file = builder.build().toFileSpec()
println(file.code)
val spec = buildDataClass(className) {
addConstructorProperty("name", String::class.asTypeName())
addConstructorProperty("age", Int::class.asTypeName())
}

val result = KotlinCodeGenerationTest.compile(KotlinCompilationCommand(file))
// val file = spec.toFileSpec()
// println(file.code)

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 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)")
// }
}
}
19 changes: 15 additions & 4 deletions _itest/src/test/kotlin/KotlinFileSpecTest.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
package io.toolisticon.kotlin.generation.itest

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

internal class KotlinFileSpecTest {

@Target(AnnotationTarget.FILE)
annotation class Foo

@Test
fun name() {
val spec = FileSpec.builder(ClassName("some","Stuff"))
.addFileComment("%L", "this is a comment")
.addAnnotation(Foo::class)
.build()

println(spec)
}

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

val b = builder {
val b = builder.builder {
addAnnotation(Foo::class)
}

val spec = b.build()

println(spec.code)
println(spec)
}
}
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()
}
}
46 changes: 46 additions & 0 deletions _itest/src/test/kotlin/MyCustomAnnotationSpecTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.toolisticon.kotlin.generation.itest

import com.squareup.kotlinpoet.ClassName
import com.tschuchort.compiletesting.KotlinCompilation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildAnnotation
import io.toolisticon.kotlin.generation.KotlinCodeGeneration.buildFile
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest
import io.toolisticon.kotlin.generation.test.model.KotlinCompilationCommand
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import io.toolisticon.kotlin.generation.test.KotlinCodeGenerationTest.assertThat as assertThatCompilation


internal class MyCustomAnnotationSpecTest {

@Test
fun `generate class with custom annotation`() {
val name = ClassName(KotlinCodeGenerationITest.ROOT_PACKAGE, "MyClass")

val customAnnotation = buildAnnotation(MyCustomAnnotationSpec.name) {
addStringMember("value", "hello")
}

val myClass = KotlinCodeGeneration.buildClass(name) {
addAnnotation(customAnnotation)
}

val file = buildFile(name) {
addType(myClass)
}

println(myClass.code)

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

assertThatCompilation(result).hasExitCode(KotlinCompilation.ExitCode.OK);

val klass: KClass<out Any> = result.loadClass(name)
assertThat(klass.annotations).hasSize(1)
val annotation: Annotation = klass.annotations[0]

//assertThat(annotation::class.)
}
}
56 changes: 56 additions & 0 deletions _mvn/parent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.toolisticon.kotlin.generation._</groupId>
<artifactId>kotlin-code-generation-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>kotlin-code-generation-parent</artifactId>
<name>pom: ${project.artifactId}</name>
<description>Common parent pom for all modules.</description>
<packaging>pom</packaging>

<properties>
<kotlin-poet.version>1.17.0</kotlin-poet.version>
<kotlin-compile-testing.version>0.5.0-alpha08</kotlin-compile-testing.version>

<!-- TEST -->
<assertj.version>3.26.0</assertj.version>
<logback.version>1.5.6</logback.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>kotlinpoet-jvm</artifactId>
<version>${kotlin-poet.version}</version>
</dependency>

<dependency>
<groupId>dev.zacsweers.kctfork</groupId>
<artifactId>core</artifactId>
<scope>test</scope>
<version>${kotlin-compile-testing.version}</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Loading

0 comments on commit 2335259

Please sign in to comment.