temporal solution of generating problem

This commit is contained in:
InsanusMokrassar 2023-02-26 14:37:41 +06:00
parent d2e6d2ec80
commit 8215f9d2c6
4 changed files with 178 additions and 148 deletions

View File

@ -89,17 +89,17 @@ publishing {
} }
if (project.hasProperty("signing.gnupg.keyName")) { if (project.hasProperty("signing.gnupg.keyName")) {
apply plugin: 'signing' // apply plugin: 'signing'
//
signing { // signing {
useGpgCmd() // useGpgCmd()
//
sign publishing.publications // sign publishing.publications
} // }
//
task signAll { // task signAll {
tasks.withType(Sign).forEach { // tasks.withType(Sign).forEach {
dependsOn(it) // dependsOn(it)
} // }
} // }
} }

View File

@ -8,8 +8,13 @@ import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSClassifierReference
import com.google.devtools.ksp.symbol.KSPropertyDeclaration import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSReferenceElement
import com.google.devtools.ksp.symbol.KSType import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeAlias
import com.google.devtools.ksp.symbol.KSValueArgument
import com.google.devtools.ksp.symbol.Nullability
import com.squareup.kotlinpoet.AnnotationSpec import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.CodeBlock
@ -18,6 +23,7 @@ import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asTypeName import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.ksp.toAnnotationSpec import com.squareup.kotlinpoet.ksp.toAnnotationSpec
@ -31,15 +37,32 @@ import java.io.File
import kotlin.reflect.KProperty1 import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties import kotlin.reflect.full.memberProperties
private fun KSClassifierReference.quilifiedName(): String = "${qualifier ?.let { "${it.quilifiedName()}." } ?: ""}${referencedName()}"
class Processor( class Processor(
private val codeGenerator: CodeGenerator private val codeGenerator: CodeGenerator
) : SymbolProcessor { ) : SymbolProcessor {
private val KSPropertyDeclaration.typeName: TypeName
get() {
return runCatching {
type.toTypeName()
}.getOrElse {
val element = type.element as KSClassifierReference
(type.element as KSClassifierReference).let {
ClassName(
element.qualifier ?.quilifiedName() ?: "",
element.referencedName()
)
}
}
}
@OptIn(KspExperimental::class) @OptIn(KspExperimental::class)
override fun process(resolver: Resolver): List<KSAnnotated> { override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getSymbolsWithAnnotation( val toRetry = resolver.getSymbolsWithAnnotation(
GenerateCRUDModel::class.qualifiedName!! GenerateCRUDModel::class.qualifiedName!!
).filterIsInstance<KSClassDeclaration>().forEach { ksClassDeclaration -> ).filterIsInstance<KSClassDeclaration>().filterNot { ksClassDeclaration ->
val ksFile = ksClassDeclaration.containingFile ?: return@forEach val ksFile = ksClassDeclaration.containingFile ?: return@filterNot false
runCatching {
FileSpec.builder( FileSpec.builder(
ksClassDeclaration.packageName.asString(), ksClassDeclaration.packageName.asString(),
"GeneratedModels${ksFile.fileName.removeSuffix(".kt")}" "GeneratedModels${ksFile.fileName.removeSuffix(".kt")}"
@ -77,9 +100,9 @@ class Processor(
primaryConstructor( primaryConstructor(
FunSpec.constructorBuilder().apply { FunSpec.constructorBuilder().apply {
ksClassProperties.forEach { ksClassProperties.forEach {
addParameter(it.simpleName.getShortName(), it.type.toTypeName()) addParameter(it.simpleName.getShortName(), it.typeName)
typeBuilder.addProperty( typeBuilder.addProperty(
PropertySpec.builder(it.simpleName.getShortName(), it.type.toTypeName(), KModifier.OVERRIDE).apply { PropertySpec.builder(it.simpleName.getShortName(), it.typeName, KModifier.OVERRIDE).apply {
initializer(it.simpleName.getShortName()) initializer(it.simpleName.getShortName())
}.build() }.build()
) )
@ -131,12 +154,12 @@ class Processor(
FunSpec.constructorBuilder().apply { FunSpec.constructorBuilder().apply {
propertiesToOverrideInRegistered.forEach { propertiesToOverrideInRegistered.forEach {
addParameter( addParameter(
ParameterSpec.builder(it.simpleName.getShortName(), it.type.toTypeName()).apply { ParameterSpec.builder(it.simpleName.getShortName(), it.typeName).apply {
annotations += it.annotations.map { it.toAnnotationSpec() } annotations += it.annotations.map { it.toAnnotationSpec() }
}.build() }.build()
) )
typeBuilder.addProperty( typeBuilder.addProperty(
PropertySpec.builder(it.simpleName.getShortName(), it.type.toTypeName(), KModifier.OVERRIDE).apply { PropertySpec.builder(it.simpleName.getShortName(), it.typeName, KModifier.OVERRIDE).apply {
initializer(it.simpleName.getShortName()) initializer(it.simpleName.getShortName())
}.build() }.build()
) )
@ -164,7 +187,7 @@ class Processor(
FunSpec.builder("asRegistered").apply { FunSpec.builder("asRegistered").apply {
receiver(ksClassDeclaration.toClassName()) receiver(ksClassDeclaration.toClassName())
(registeredTypesProperties.filter { it.simpleName.asString() !in ksClassPropertiesNames }).forEach { (registeredTypesProperties.filter { it.simpleName.asString() !in ksClassPropertiesNames }).forEach {
addParameter(it.simpleName.asString(), it.type.toTypeName()) addParameter(it.simpleName.asString(), it.typeName)
} }
addCode( addCode(
CodeBlock.of( CodeBlock.of(
@ -186,8 +209,9 @@ class Processor(
} }
} }
} }
} }.isSuccess
}.toList()
return emptyList() return toRetry
} }
} }

View File

@ -13,6 +13,7 @@ import kotlinx.serialization.Serializable
public data class NewTest( public data class NewTest(
public override val property1: String, public override val property1: String,
public override val property2: Int, public override val property2: Int,
public override val parent: ParentTypeId?,
) : Test ) : Test
@Serializable @Serializable
@ -21,8 +22,10 @@ public data class RegisteredTest(
public override val id: TestId, public override val id: TestId,
public override val property1: String, public override val property1: String,
public override val property2: Int, public override val property2: Int,
public override val parent: ParentTypeId?,
) : Test, IRegisteredTest ) : Test, IRegisteredTest
public fun Test.asNew(): NewTest = NewTest(property1, property2) public fun Test.asNew(): NewTest = NewTest(property1, property2, parent)
public fun Test.asRegistered(id: TestId): RegisteredTest = RegisteredTest(id, property1, property2) public fun Test.asRegistered(id: TestId): RegisteredTest = RegisteredTest(id, property1, property2,
parent)

View File

@ -11,10 +11,13 @@ value class TestId(
val long: Long val long: Long
) )
typealias ParentTypeId = TestId
@GenerateCRUDModel(IRegisteredTest::class) @GenerateCRUDModel(IRegisteredTest::class)
sealed interface Test { sealed interface Test {
val property1: String val property1: String
val property2: Int val property2: Int
val parent: ParentTypeId?
@GenerateCRUDModelExcludeOverride @GenerateCRUDModelExcludeOverride
val excludedProperty: String val excludedProperty: String