mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-19 15:29:24 +00:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
ef9828d817 | |||
d1a5bdd04d |
@@ -1,13 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## 0.24.2
|
|
||||||
|
|
||||||
* `Versions`:
|
|
||||||
* `Exposed`: `0.57.0` -> `0.58.0`
|
|
||||||
* `Ksp`:
|
|
||||||
* `Sealed`:
|
|
||||||
* Add annotation `GenerateSealedTypesWorkaround` which allow to generate `subtypes` lists
|
|
||||||
|
|
||||||
## 0.24.1
|
## 0.24.1
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.24.2
|
version=0.24.1
|
||||||
android_code_version=281
|
android_code_version=280
|
||||||
|
@@ -7,7 +7,7 @@ kt-coroutines = "1.10.1"
|
|||||||
kslog = "1.4.0"
|
kslog = "1.4.0"
|
||||||
|
|
||||||
jb-compose = "1.7.3"
|
jb-compose = "1.7.3"
|
||||||
jb-exposed = "0.58.0"
|
jb-exposed = "0.57.0"
|
||||||
jb-dokka = "2.0.0"
|
jb-dokka = "2.0.0"
|
||||||
|
|
||||||
sqlite = "3.47.2.0"
|
sqlite = "3.47.2.0"
|
||||||
|
@@ -3,15 +3,9 @@ package dev.inmo.micro_utils.ksp.sealed.generator
|
|||||||
import com.google.devtools.ksp.KspExperimental
|
import com.google.devtools.ksp.KspExperimental
|
||||||
import com.google.devtools.ksp.getAnnotationsByType
|
import com.google.devtools.ksp.getAnnotationsByType
|
||||||
import com.google.devtools.ksp.symbol.KSClassDeclaration
|
import com.google.devtools.ksp.symbol.KSClassDeclaration
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedTypesWorkaround
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround as OldGenerateSealedWorkaround
|
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround as OldGenerateSealedWorkaround
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
@OptIn(KspExperimental::class)
|
||||||
val KSClassDeclaration.getGenerateSealedWorkaroundAnnotation
|
val KSClassDeclaration.getGenerateSealedWorkaroundAnnotation
|
||||||
get() = (getAnnotationsByType(GenerateSealedWorkaround::class).firstOrNull() ?: getAnnotationsByType(OldGenerateSealedWorkaround::class).firstOrNull())
|
get() = (getAnnotationsByType(GenerateSealedWorkaround::class).firstOrNull() ?: getAnnotationsByType(OldGenerateSealedWorkaround::class).firstOrNull())
|
||||||
|
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
|
||||||
val KSClassDeclaration.getGenerateSealedTypesWorkaroundAnnotation
|
|
||||||
get() = getAnnotationsByType(GenerateSealedTypesWorkaround::class).firstOrNull()
|
|
||||||
|
@@ -6,17 +6,21 @@ import com.google.devtools.ksp.processing.CodeGenerator
|
|||||||
import com.google.devtools.ksp.processing.Resolver
|
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.*
|
import com.google.devtools.ksp.symbol.*
|
||||||
import com.squareup.kotlinpoet.*
|
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.ParameterizedTypeName.Companion.parameterizedBy
|
||||||
|
import com.squareup.kotlinpoet.PropertySpec
|
||||||
|
import com.squareup.kotlinpoet.asTypeName
|
||||||
import com.squareup.kotlinpoet.ksp.toClassName
|
import com.squareup.kotlinpoet.ksp.toClassName
|
||||||
import dev.inmo.micro_ksp.generator.buildSubFileName
|
import dev.inmo.micro_ksp.generator.buildSubFileName
|
||||||
import dev.inmo.micro_ksp.generator.companion
|
import dev.inmo.micro_ksp.generator.companion
|
||||||
import dev.inmo.micro_ksp.generator.findSubClasses
|
import dev.inmo.micro_ksp.generator.findSubClasses
|
||||||
import dev.inmo.micro_ksp.generator.writeFile
|
import dev.inmo.micro_ksp.generator.writeFile
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedTypesWorkaround
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
class Processor(
|
class Processor(
|
||||||
private val codeGenerator: CodeGenerator
|
private val codeGenerator: CodeGenerator
|
||||||
@@ -105,62 +109,6 @@ class Processor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
|
||||||
private fun FileSpec.Builder.generateSealedTypesWorkaround(
|
|
||||||
ksClassDeclaration: KSClassDeclaration,
|
|
||||||
resolver: Resolver
|
|
||||||
) {
|
|
||||||
val annotation = ksClassDeclaration.getGenerateSealedTypesWorkaroundAnnotation
|
|
||||||
val subClasses = ksClassDeclaration.resolveSubclasses(
|
|
||||||
searchIn = resolver.getAllFiles(),
|
|
||||||
allowNonSealed = annotation ?.includeNonSealedSubTypes ?: false
|
|
||||||
).distinct()
|
|
||||||
val subClassesNames = subClasses.filter {
|
|
||||||
it.getAnnotationsByType(GenerateSealedWorkaround.Exclude::class).count() == 0
|
|
||||||
}.sortedBy {
|
|
||||||
(it.getAnnotationsByType(GenerateSealedWorkaround.Order::class).firstOrNull()) ?.order ?: 0
|
|
||||||
}.map {
|
|
||||||
it.toClassName()
|
|
||||||
}.toList()
|
|
||||||
val className = ksClassDeclaration.toClassName()
|
|
||||||
val setType = Set::class.asTypeName().parameterizedBy(
|
|
||||||
KClass::class.asTypeName().parameterizedBy(
|
|
||||||
TypeVariableName(
|
|
||||||
"out ${ksClassDeclaration.asStarProjectedType().toClassName().simpleNames.joinToString(".")}",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
addProperty(
|
|
||||||
PropertySpec.builder(
|
|
||||||
"subtypes",
|
|
||||||
setType
|
|
||||||
).apply {
|
|
||||||
modifiers.add(
|
|
||||||
KModifier.PRIVATE
|
|
||||||
)
|
|
||||||
initializer(
|
|
||||||
CodeBlock.of(
|
|
||||||
"""setOf(${subClassesNames.joinToString(",\n") { it.simpleNames.joinToString(".") + "::class" }})"""
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}.build()
|
|
||||||
)
|
|
||||||
addFunction(
|
|
||||||
FunSpec.builder("subtypes").apply {
|
|
||||||
val companion = ksClassDeclaration.takeIf { it.isCompanionObject } ?.toClassName()
|
|
||||||
?: ksClassDeclaration.companion ?.toClassName()
|
|
||||||
?: ClassName(className.packageName, *className.simpleNames.toTypedArray(), "Companion")
|
|
||||||
receiver(companion)
|
|
||||||
returns(setType)
|
|
||||||
addCode(
|
|
||||||
CodeBlock.of(
|
|
||||||
"""return subtypes"""
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}.build()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(KspExperimental::class)
|
@OptIn(KspExperimental::class)
|
||||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||||
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
||||||
@@ -183,26 +131,6 @@ class Processor(
|
|||||||
}.build()
|
}.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(resolver.getSymbolsWithAnnotation(GenerateSealedTypesWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
|
||||||
val prefix = (it.getGenerateSealedTypesWorkaroundAnnotation) ?.prefix ?.takeIf {
|
|
||||||
it.isNotEmpty()
|
|
||||||
} ?: it.buildSubFileName.replaceFirst(it.simpleName.asString(), "")
|
|
||||||
it.writeFile(prefix = prefix, suffix = "SealedTypesWorkaround") {
|
|
||||||
FileSpec.builder(
|
|
||||||
it.packageName.asString(),
|
|
||||||
"${it.simpleName.getShortName()}SealedTypesWorkaround"
|
|
||||||
).apply {
|
|
||||||
addFileComment(
|
|
||||||
"""
|
|
||||||
THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
|
|
||||||
TO REGENERATE IT JUST DELETE FILE
|
|
||||||
ORIGINAL FILE: ${it.containingFile ?.fileName}
|
|
||||||
""".trimIndent()
|
|
||||||
)
|
|
||||||
generateSealedTypesWorkaround(it, resolver)
|
|
||||||
}.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
@@ -1,19 +1,14 @@
|
|||||||
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedTypesWorkaround
|
|
||||||
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
import dev.inmo.micro_utils.ksp.sealed.GenerateSealedWorkaround
|
||||||
|
|
||||||
@GenerateSealedWorkaround
|
@GenerateSealedWorkaround
|
||||||
@GenerateSealedTypesWorkaround
|
|
||||||
sealed interface Test {
|
sealed interface Test {
|
||||||
@GenerateSealedWorkaround.Order(2)
|
@GenerateSealedWorkaround.Order(2)
|
||||||
@GenerateSealedTypesWorkaround.Order(2)
|
|
||||||
object A : Test
|
object A : Test
|
||||||
@GenerateSealedWorkaround.Exclude
|
@GenerateSealedWorkaround.Exclude
|
||||||
@GenerateSealedTypesWorkaround.Exclude
|
|
||||||
object B : Test
|
object B : Test
|
||||||
@GenerateSealedWorkaround.Order(0)
|
@GenerateSealedWorkaround.Order(0)
|
||||||
@GenerateSealedTypesWorkaround.Order(0)
|
|
||||||
object C : Test
|
object C : Test
|
||||||
|
|
||||||
// Required for successful sealed workaround generation
|
// Required for successful sealed workaround generation
|
||||||
|
@@ -1,12 +0,0 @@
|
|||||||
// 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
|
|
||||||
import kotlin.reflect.KClass
|
|
||||||
|
|
||||||
private val subtypes: Set<KClass<out Test>> = setOf(Test.C::class,
|
|
||||||
Test.A::class)
|
|
||||||
|
|
||||||
public fun Test.Companion.subtypes(): Set<KClass<out Test>> = subtypes
|
|
@@ -1,15 +0,0 @@
|
|||||||
package dev.inmo.micro_utils.ksp.sealed
|
|
||||||
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
annotation class GenerateSealedTypesWorkaround(
|
|
||||||
val prefix: String = "",
|
|
||||||
val includeNonSealedSubTypes: Boolean = false,
|
|
||||||
) {
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
annotation class Order(val order: Int)
|
|
||||||
@Retention(AnnotationRetention.BINARY)
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
annotation class Exclude
|
|
||||||
}
|
|
@@ -4,7 +4,7 @@ package dev.inmo.micro_utils.ksp.sealed
|
|||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
annotation class GenerateSealedWorkaround(
|
annotation class GenerateSealedWorkaround(
|
||||||
val prefix: String = "",
|
val prefix: String = "",
|
||||||
val includeNonSealedSubTypes: Boolean = false,
|
val includeNonSealedSubTypes: Boolean = false
|
||||||
) {
|
) {
|
||||||
@Retention(AnnotationRetention.BINARY)
|
@Retention(AnnotationRetention.BINARY)
|
||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
"extends": [
|
"extends": [
|
||||||
"config:recommended"
|
"config:base"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user