mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 08:13:49 +00:00
modules initialization
This commit is contained in:
parent
98ad6dbeb2
commit
827cf32c1b
@ -2,6 +2,13 @@
|
||||
|
||||
## 0.21.1
|
||||
|
||||
* `KSP`:
|
||||
* Module has been initialized
|
||||
* `Generator`:
|
||||
* Module has been initialized
|
||||
* `Sealed`:
|
||||
* Module has been initialized
|
||||
|
||||
## 0.21.0
|
||||
|
||||
**THIS UPDATE CONTAINS BREAKING CHANGES IN `safely*`-ORIENTED FUNCTIONS**
|
||||
|
20
ksp/generator/build.gradle
Normal file
20
ksp/generator/build.gradle
Normal 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
|
||||
}
|
49
ksp/generator/src/main/kotlin/FilesWorkaround.kt
Normal file
49
ksp/generator/src/main/kotlin/FilesWorkaround.kt
Normal 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
7
ksp/sealed/build.gradle
Normal file
@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppJvmJsAndroidLinuxMingwLinuxArm64ProjectPresetPath"
|
21
ksp/sealed/generator/build.gradle
Normal file
21
ksp/sealed/generator/build.gradle
Normal 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
|
||||
}
|
110
ksp/sealed/generator/src/main/kotlin/Processor.kt
Normal file
110
ksp/sealed/generator/src/main/kotlin/Processor.kt
Normal 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()
|
||||
}
|
||||
}
|
11
ksp/sealed/generator/src/main/kotlin/Provider.kt
Normal file
11
ksp/sealed/generator/src/main/kotlin/Provider.kt
Normal 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
|
||||
)
|
||||
}
|
@ -0,0 +1 @@
|
||||
dev.inmo.micro_utils.ksp.sealed.generator.Provider
|
27
ksp/sealed/generator/test/build.gradle
Normal file
27
ksp/sealed/generator/test/build.gradle
Normal 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 {
|
||||
}
|
16
ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt
Normal file
16
ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt
Normal 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
|
||||
}
|
@ -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
|
14
ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt
Normal file
14
ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt
Normal 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
|
||||
}
|
@ -49,6 +49,12 @@ String[] includes = [
|
||||
":fsm:common",
|
||||
":fsm:repos:common",
|
||||
|
||||
":ksp:generator",
|
||||
|
||||
":ksp:sealed",
|
||||
":ksp:sealed:generator",
|
||||
":ksp:sealed:generator:test",
|
||||
|
||||
":dokka"
|
||||
]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user