Merge pull request #460 from InsanusMokrassar/0.21.1

0.21.1
This commit is contained in:
InsanusMokrassar 2024-06-25 22:11:26 +06:00 committed by GitHub
commit 374a5a1a37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 304 additions and 2 deletions

View File

@ -1,5 +1,14 @@
# Changelog # Changelog
## 0.21.1
* `KSP`:
* Module has been initialized
* `Generator`:
* Module has been initialized
* `Sealed`:
* Module has been initialized
## 0.21.0 ## 0.21.0
**THIS UPDATE CONTAINS BREAKING CHANGES IN `safely*`-ORIENTED FUNCTIONS** **THIS UPDATE CONTAINS BREAKING CHANGES IN `safely*`-ORIENTED FUNCTIONS**

View File

@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.21.0 version=0.21.1
android_code_version=259 android_code_version=260

View File

@ -0,0 +1,20 @@
plugins {
id "org.jetbrains.kotlin.jvm"
}
apply from: "$publishJvmOnlyPath"
repositories {
mavenCentral()
}
dependencies {
api project(":micro_utils.common")
api libs.kotlin.poet
api libs.ksp
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

View File

@ -0,0 +1,49 @@
package dev.inmo.micro_ksp.generator
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.squareup.kotlinpoet.FileSpec
import java.io.File
fun KSClassDeclaration.writeFile(
prefix: String = "",
suffix: String = "",
relatedPath: String = "",
force: Boolean = false,
fileSpecBuilder: () -> FileSpec
) {
val containingFile = containingFile!!
File(
File(
File(containingFile.filePath).parent,
relatedPath
),
"$prefix${simpleName.asString()}$suffix.kt"
).takeIf { force || !it.exists() } ?.apply {
parentFile.mkdirs()
writer().use { writer ->
fileSpecBuilder().writeTo(writer)
}
}
}
fun KSFile.writeFile(
prefix: String = "",
suffix: String = "",
relatedPath: String = "",
force: Boolean = false,
fileSpecBuilder: () -> FileSpec
) {
File(
File(
File(filePath).parent,
relatedPath
),
"$prefix${fileName.dropLastWhile { it != '.' }.removeSuffix(".")}$suffix.kt"
).takeIf { force || !it.exists() } ?.apply {
parentFile.mkdirs()
writer().use { writer ->
fileSpecBuilder().writeTo(writer)
}
}
}

7
ksp/sealed/build.gradle Normal file
View File

@ -0,0 +1,7 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppJvmJsAndroidLinuxMingwLinuxArm64ProjectPresetPath"

View File

@ -0,0 +1,21 @@
plugins {
id "org.jetbrains.kotlin.jvm"
}
apply from: "$publishJvmOnlyPath"
repositories {
mavenCentral()
}
dependencies {
api project(":micro_utils.ksp.generator")
api project(":micro_utils.ksp.sealed")
api libs.kotlin.poet
api libs.ksp
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

View File

@ -0,0 +1,110 @@
package dev.inmo.micro_utils.ksp.sealed.generator
import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.ksp.toClassName
import dev.inmo.micro_ksp.generator.writeFile
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround
class Processor(
private val codeGenerator: CodeGenerator
) : SymbolProcessor {
private fun KSClassDeclaration.resolveSubclasses(): List<KSClassDeclaration> {
return (getSealedSubclasses().flatMap {
it.resolveSubclasses()
}.ifEmpty {
sequenceOf(this)
}).toList()
}
@OptIn(KspExperimental::class)
private fun FileSpec.Builder.generateSealedWorkaround(
ksClassDeclaration: KSClassDeclaration,
resolver: Resolver
) {
val subClasses = ksClassDeclaration.resolveSubclasses().distinct()
val subClassesNames = subClasses.filter {
when (it.classKind) {
ClassKind.ENUM_ENTRY,
ClassKind.OBJECT -> true
ClassKind.INTERFACE,
ClassKind.CLASS,
ClassKind.ENUM_CLASS,
ClassKind.ANNOTATION_CLASS -> false
}
}.filter {
it.getAnnotationsByType(GenerateSealedWorkaround.Exclude::class).count() == 0
}.sortedBy {
(it.getAnnotationsByType(GenerateSealedWorkaround.Order::class).firstOrNull()) ?.order ?: 0
}.map {
it.toClassName()
}
val className = ksClassDeclaration.toClassName()
val setType = Set::class.asTypeName().parameterizedBy(
ksClassDeclaration.toClassName()
)
addProperty(
PropertySpec.builder(
"values",
setType
).apply {
modifiers.add(
KModifier.PRIVATE
)
initializer(
CodeBlock.of(
"""setOf(${subClassesNames.joinToString(",\n") {it.simpleNames.joinToString(".")}})"""
)
)
}.build()
)
addFunction(
FunSpec.builder("values").apply {
receiver(ClassName(className.packageName, *className.simpleNames.toTypedArray(), "Companion"))
returns(setType)
addCode(
CodeBlock.of(
"""return values"""
)
)
}.build()
)
}
@OptIn(KspExperimental::class)
override fun process(resolver: Resolver): List<KSAnnotated> {
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
val prefix = it.getAnnotationsByType(GenerateSealedWorkaround::class).first().prefix
it.writeFile(prefix = prefix, suffix = "SealedWorkaround") {
FileSpec.builder(
it.packageName.asString(),
"${it.simpleName.getShortName()}SealedWorkaround"
).apply {
addFileComment(
"""
THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
TO REGENERATE IT JUST DELETE FILE
ORIGINAL FILE: ${it.containingFile ?.fileName}
""".trimIndent()
)
generateSealedWorkaround(it, resolver)
}.build()
}
}
return emptyList()
}
}

View File

@ -0,0 +1,11 @@
package dev.inmo.micro_utils.ksp.sealed.generator
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider
class Provider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = Processor(
environment.codeGenerator
)
}

View File

@ -0,0 +1 @@
dev.inmo.micro_utils.ksp.sealed.generator.Provider

View File

@ -0,0 +1,27 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
id "com.google.devtools.ksp"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":micro_utils.ksp.sealed")
}
}
}
}
dependencies {
add("kspCommonMainMetadata", project(":micro_utils.ksp.sealed.generator"))
}
ksp {
}

View File

@ -0,0 +1,16 @@
package dev.inmo.micro_utils.ksp.sealed.generator.test
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround
@GenerateSealedWorkaround
sealed interface Test {
@GenerateSealedWorkaround.Order(2)
object A : Test
@GenerateSealedWorkaround.Exclude
object B : Test
@GenerateSealedWorkaround.Order(0)
object C : Test
// Required for successful sealed workaround generation
companion object
}

View File

@ -0,0 +1,11 @@
// THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
// TO REGENERATE IT JUST DELETE FILE
// ORIGINAL FILE: Test.kt
package dev.inmo.micro_utils.ksp.`sealed`.generator.test
import kotlin.collections.Set
private val values: Set<Test> = setOf(Test.C,
Test.A)
public fun Test.Companion.values(): Set<Test> = values

View File

@ -0,0 +1,14 @@
package dev.inmo.microutils.kps.sealed
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS)
annotation class GenerateSealedWorkaround(
val prefix: String = ""
) {
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS)
annotation class Order(val order: Int)
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS)
annotation class Exclude
}

View File

@ -49,6 +49,12 @@ String[] includes = [
":fsm:common", ":fsm:common",
":fsm:repos:common", ":fsm:repos:common",
":ksp:generator",
":ksp:sealed",
":ksp:sealed:generator",
":ksp:sealed:generator:test",
":dokka" ":dokka"
] ]