Skip to content

Commit

Permalink
Merge pull request #14 from SchwarzIT/feature/ksp
Browse files Browse the repository at this point in the history
Feature/ksp
  • Loading branch information
sbra0902 authored Jul 26, 2022
2 parents d2f285f + a0051ee commit 0be65dd
Show file tree
Hide file tree
Showing 29 changed files with 551 additions and 71 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: "org.jlleitschuh.gradle.ktlint"
buildscript {
ext.kotlin_version = '1.6.0'
ext.kotlin_version = '1.7.0'
repositories {
google()
jcenter()
Expand All @@ -12,8 +12,9 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0'
classpath "org.jlleitschuh.gradle:ktlint-gradle:10.2.0"
classpath "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.7.0-1.0.6"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
11 changes: 6 additions & 5 deletions demo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: "com.google.devtools.ksp"

android {
compileSdkVersion 31
Expand Down Expand Up @@ -40,9 +40,10 @@ android {
}
}


kapt {
correctErrorTypes = true
kotlin {
sourceSets.all {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}

dependencies {
Expand All @@ -57,6 +58,6 @@ dependencies {
implementation project(path: ':demolibrary')
implementation project(path: ':kokain-di')
implementation project(path: ':kokain-core-api')
kapt project(path: ':kokain-processor')
ksp ksp(project(':kokain-ksp',))
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
11 changes: 9 additions & 2 deletions demolibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//apply plugin: 'kotlin-kapt'
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: "com.google.devtools.ksp"

android {
compileSdkVersion 31
Expand All @@ -27,14 +28,20 @@ android {

}

kotlin {
sourceSets.all {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation project(path: ':kokain-di')
implementation project(path: ':kokain-core-api')
kapt project(path: ':kokain-processor')
ksp ksp(project(':kokain-ksp',))
testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand Down
1 change: 1 addition & 0 deletions kokain-generator-lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
53 changes: 53 additions & 0 deletions kokain-generator-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apply plugin: 'kotlin'
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
apply plugin: 'pmd'
apply plugin: 'kotlin-kapt'
apply plugin: "org.jlleitschuh.gradle.ktlint"

targetCompatibility = '1.8'
sourceCompatibility = '1.8'

publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'com.schwarz.kokain'
artifactId = 'kokain-generator-lib'
version = '0.1'
from components.java
}
}
}


jacoco {
toolVersion = "0.8.7"
}

jacocoTestReport {
group 'verification'
dependsOn 'test'

reports {

xml.enabled = true
html.enabled = false
}
}

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation 'com.squareup:kotlinpoet:1.12.0'
api project(path: ':kokain-core-api', configuration: 'default')
implementation 'org.apache.commons:commons-lang3:3.4'
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.schwarz.kokain.processor.generation
package com.schwarz.kokain.kokaingeneratorlib.generation

import com.schwarz.kokain.api.EBean
import com.schwarz.kokain.processor.model.EBeanModel
import com.schwarz.kokain.processor.model.EFactoryModel
import com.schwarz.kokain.processor.util.TypeUtil
import com.schwarz.kokain.kokaingeneratorlib.model.IEBeanModel
import com.schwarz.kokain.kokaingeneratorlib.model.IEFactoryModel
import com.schwarz.kokain.kokaingeneratorlib.util.TypeUtil
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asTypeName
import java.lang.StringBuilder
import java.util.StringJoiner
import kotlin.collections.ArrayList
Expand All @@ -20,32 +19,32 @@ class FactoryGenerator {

private final val ADDITIONAL_FACTORY_PROPERTY_NAME = "additonalFactories"

fun generateModel(factory: EFactoryModel, beans: List<EBeanModel>): FileSpec {
fun generateModel(factory: IEFactoryModel, beans: List<IEBeanModel>): FileSpec {
val typeBuilder = TypeSpec.classBuilder("GeneratedFactory").addModifiers(KModifier.PUBLIC).addSuperinterface(TypeUtil.kdiFactory())
.addProperty(propAdditionalFactories(factory))
.addFunction(create(beans))

return FileSpec.get(factory.`package`, typeBuilder.build())
}

private fun propAdditionalFactories(factory: EFactoryModel): PropertySpec {
private fun propAdditionalFactories(factory: IEFactoryModel): PropertySpec {

val builder = StringBuilder("arrayOf<%T>")
val types = ArrayList<Any>()
val joiner = StringJoiner(",", "(", ")")
types.add(TypeUtil.kdiFactory())
for (factory in factory.additionalFactories) {
if (factory.asTypeName() != TypeUtil.void()) {
if (factory != TypeUtil.void()) {
joiner.add("%T()")
types.add(factory.asTypeName())
types.add(factory)
}
}
builder.append(joiner)

return PropertySpec.builder(ADDITIONAL_FACTORY_PROPERTY_NAME, TypeUtil.arrayKdiFactories()).initializer(builder.toString(), *types.toTypedArray()).build()
}

private fun create(beans: List<EBeanModel>): FunSpec {
private fun create(beans: List<IEBeanModel>): FunSpec {

var builder = FunSpec.builder("createInstance").addModifiers(KModifier.PUBLIC, KModifier.OVERRIDE).addParameter("clazz", TypeUtil.classStar()).returns(TypeUtil.any().copy(true))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.schwarz.kokain.processor.generation
package com.schwarz.kokain.kokaingeneratorlib.generation

import com.schwarz.kokain.processor.model.EBeanModel
import com.schwarz.kokain.processor.util.TypeUtil
import com.schwarz.kokain.kokaingeneratorlib.model.IEBeanModel
import com.schwarz.kokain.kokaingeneratorlib.util.TypeUtil
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
Expand All @@ -11,7 +11,7 @@ import com.squareup.kotlinpoet.TypeSpec

class ShadowBeanGenerator {

fun generateModel(holder: EBeanModel): FileSpec {
fun generateModel(holder: IEBeanModel): FileSpec {
val typeBuilder = TypeSpec.classBuilder(holder.generatedClazzSimpleName).addModifiers(holder.classVisibility).superclass(
ClassName(holder.`package`, holder.sourceClazzSimpleName)
).addSuperinterface(TypeUtil.activityRefered()).addSuperinterface(TypeUtil.beanScope())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.schwarz.kokain.kokaingeneratorlib.model

import com.schwarz.kokain.api.EBean
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.TypeName

interface IEBeanModel {

val scope: EBean.Scope

val sourceClazzSimpleName: String

val generatedClazzSimpleName: String
get() = sourceClazzSimpleName + "Shadow"

val `package`: String

val generatedClazzTypeName: TypeName
get() = ClassName(`package`, generatedClazzSimpleName)

val classVisibility: KModifier
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.schwarz.kokain.kokaingeneratorlib.model

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeName

interface IEFactoryModel {

val additionalFactories: List<TypeName>

val sourceClazzSimpleName: String

val generatedClazzSimpleName: String
get() = sourceClazzSimpleName + "Shadow"

val `package`: String

val generatedClazzTypeName: TypeName
get() = ClassName(`package`, generatedClazzSimpleName)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.schwarz.kokain.processor.util
package com.schwarz.kokain.kokaingeneratorlib.util

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.WildcardTypeName
import javax.lang.model.type.TypeMirror

object TypeUtil {

Expand Down Expand Up @@ -83,16 +82,6 @@ object TypeUtil {
return ClassName("kotlin", "Array").parameterizedBy(kdiFactory())
}

fun getSimpleName(type: TypeMirror): String {
val parts = type.toString().split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
return if (parts.size > 1) parts[parts.size - 1] else parts[0]
}

fun getPackage(type: TypeMirror): String {
val lastIndexOf = type.toString().lastIndexOf(".")
return if (lastIndexOf >= 0) type.toString().substring(0, lastIndexOf) else type.toString()
}

fun classStar(): ParameterizedTypeName {
return ClassName("kotlin.reflect", "KClass").parameterizedBy(star())
}
Expand Down
1 change: 1 addition & 0 deletions kokain-ksp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
59 changes: 59 additions & 0 deletions kokain-ksp/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apply plugin: 'kotlin'
apply plugin: 'maven-publish'
apply plugin: 'jacoco'
apply plugin: 'pmd'
apply plugin: 'kotlin-kapt'
apply plugin: "org.jlleitschuh.gradle.ktlint"

targetCompatibility = '1.8'
sourceCompatibility = '1.8'

publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'com.schwarz.kokain'
artifactId = 'kokain-ksp'
version = '0.1'
from components.java
}
}
}


jacoco {
toolVersion = "0.8.7"
}

jacocoTestReport {
group 'verification'
dependsOn 'test'

reports {

xml.enabled = true
html.enabled = false
}
}

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.squareup:kotlinpoet:1.12.0'
implementation("com.squareup:kotlinpoet-ksp:1.12.0")
implementation("com.google.devtools.ksp:symbol-processing-api:1.6.21-1.0.6")
testImplementation 'junit:junit:4.12'
testImplementation project(path: ':kokain-core-api', configuration: 'default')
testImplementation 'com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.9'
testImplementation 'org.mockito:mockito-core:1.10.19'
implementation project(path: ':kokain-generator-lib', configuration: 'default')
}
Loading

0 comments on commit 0be65dd

Please sign in to comment.