From c0262f1e3491b6f458365969b340d7e5b1649390 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Sun, 11 Aug 2024 19:00:01 +0200 Subject: [PATCH 1/5] Switch to codegen.kt --- buildSrc/src/main/kotlin/Projects.kt | 2 +- .../main/kotlin/kord-publishing.gradle.kts | 23 +- gradle/libs.versions.toml | 3 + ksp-processors/build.gradle.kts | 7 +- ksp-processors/src/main/kotlin/KSPUtils.kt | 84 +++--- .../src/main/kotlin/KotlinPoetDsl.kt | 242 +++++++++--------- .../kotlin/generation/GenerationEntity.kt | 2 +- .../kotlin/generation/GenerationProcessor.kt | 8 +- .../generation/bitflags/BitFlagsGeneration.kt | 12 +- .../kotlin/generation/bitflags/Builder.kt | 6 +- .../bitflags/ContainsPlusMinusCopy.kt | 6 +- .../bitflags/DeprecatedNewCompanion.kt | 7 +- .../generation/bitflags/FactoryFunctions.kt | 8 +- .../kotlin/generation/bitflags/Serializer.kt | 8 +- .../generation/kordenum/KordEnumGeneration.kt | 8 +- .../kotlin/generation/kordenum/Serializer.kt | 6 +- .../shared/EqualsHashCodeToString.kt | 6 +- .../shared/FileSpecForGenerationEntity.kt | 4 +- .../main/kotlin/generation/shared/Names.kt | 16 +- .../main/kotlin/generation/shared/OptIns.kt | 2 +- .../kotlin/generation/shared/SharedContent.kt | 8 + 21 files changed, 264 insertions(+), 204 deletions(-) diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index be8c41f12ee..14d33789d2f 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -29,5 +29,5 @@ val Project.isRelease get() = tag != null object Repo { const val releasesUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" - const val snapshotsUrl = "https://oss.sonatype.org/content/repositories/snapshots/" + const val snapshotsUrl = "https://repo.kord.dev/snapshots" } diff --git a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts index a001f7113cc..2d14e99d69c 100644 --- a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts @@ -60,12 +60,23 @@ publishing { } repositories { - maven { - url = uri(if (isRelease) Repo.releasesUrl else Repo.snapshotsUrl) + if(isRelease) { + maven { + url = uri(Repo.releasesUrl) - credentials { - username = getenv("NEXUS_USER") - password = getenv("NEXUS_PASSWORD") + credentials { + username = getenv("NEXUS_USER") + password = getenv("NEXUS_PASSWORD") + } + } + } else { + maven { + url = uri(Repo.snapshotsUrl) + + credentials { + username = getenv("REPOSILITE_USER") + password = getenv("REPOSILITE_PASSWORD") + } } } } @@ -75,5 +86,5 @@ signing { val secretKey = getenv("SIGNING_KEY")?.let { String(Base64.getDecoder().decode(it)) } val password = getenv("SIGNING_PASSWORD") useInMemoryPgpKeys(secretKey, password) - sign(publishing.publications) +// sign(publishing.publications) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index de5358755f9..38d72bb88b5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -32,6 +32,7 @@ dokka = "1.9.20" # https://github.com/Kotlin/dokka kotlinx-atomicfu = "0.25.0" # https://github.com/Kotlin/kotlinx-atomicfu binary-compatibility-validator = "0.15.1" # https://github.com/Kotlin/binary-compatibility-validator buildconfig = "5.4.0" # https://github.com/gmazzo/gradle-buildconfig-plugin +codegen-kt = "main-SNAPSHOT" # https://github.com/kordlib/codegen-kt [libraries] @@ -71,6 +72,8 @@ stately-collections = { module = "co.touchlab:stately-concurrent-collections", v ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinpoet" } kotlinpoet-ksp = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlinpoet" } +codegen-kt-dsl = { module = "dev.kord.codegen:kotlinpoet", version.ref = "codegen-kt" } +codegen-kt-ksp = { module = "dev.kord.codegen:ksp", version.ref = "codegen-kt" } # tests kotlin-test-annotations-common = { module = "org.jetbrains.kotlin:kotlin-test-annotations-common", version.ref = "kotlin" } diff --git a/ksp-processors/build.gradle.kts b/ksp-processors/build.gradle.kts index 5df3edcbbfe..e2da8091fe1 100644 --- a/ksp-processors/build.gradle.kts +++ b/ksp-processors/build.gradle.kts @@ -2,6 +2,10 @@ plugins { `kord-internal-module` } +repositories { + maven("https://oss.sonatype.org/content/repositories/snapshots") +} + kotlin { compilerOptions { freeCompilerArgs.add("-Xcontext-receivers") @@ -12,8 +16,9 @@ dependencies { implementation(projects.kspAnnotations) implementation(libs.ksp.api) - implementation(libs.kotlinpoet) implementation(libs.kotlinpoet.ksp) + implementation(libs.codegen.kt.dsl) + implementation(libs.codegen.kt.ksp) implementation(libs.kotlinx.serialization.json) // use types directly } diff --git a/ksp-processors/src/main/kotlin/KSPUtils.kt b/ksp-processors/src/main/kotlin/KSPUtils.kt index bab27d53157..0486d482a37 100644 --- a/ksp-processors/src/main/kotlin/KSPUtils.kt +++ b/ksp-processors/src/main/kotlin/KSPUtils.kt @@ -8,45 +8,45 @@ import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.symbol.* import kotlin.reflect.KProperty1 -internal inline fun Resolver.getSymbolsWithAnnotation(inDepth: Boolean = false) = - getSymbolsWithAnnotation(A::class.qualifiedName!!, inDepth) - -internal fun Resolver.getNewClasses() = getNewFiles().flatMap { it.declarations.filterIsInstance() } - -internal inline fun KSAnnotation.isOfType() = isOfType(A::class.qualifiedName!!) - -internal fun KSAnnotation.isOfType(qualifiedName: String) = annotationType.resolve() - .declaration.let { if (it is KSTypeAlias) it.findActualType() else it } - .qualifiedName?.asString() == qualifiedName - -@OptIn(KspExperimental::class) -internal inline fun KSAnnotated.getAnnotationsByType() = getAnnotationsByType(A::class) - -internal class AnnotationArguments private constructor( - private val arguments: Map, -) { - private fun getArgument(parameter: KProperty1) = arguments.getValue(parameter.name) - private val KProperty1.value get() = getArgument(this).value - - fun isDefault(parameter: KProperty1) = getArgument(parameter).isDefault() - - // can't return non-nullable values because of https://github.com/google/ksp/issues/885 - operator fun get(parameter: KProperty1) = parameter.value as KSAnnotation? - operator fun get(parameter: KProperty1>) = - @Suppress("UNCHECKED_CAST") (parameter.value as List?) - - companion object { - fun KSAnnotation.arguments() = - AnnotationArguments(arguments.associateBy { it.name!!.asString() }) - } -} - -@Suppress("RecursivePropertyAccessor") -internal val KSReferenceElement.isClassifierReference: Boolean - get() = when (this) { - is KSDynamicReference, is KSCallableReference -> false - is KSClassifierReference -> true - is KSDefNonNullReference -> enclosedType.isClassifierReference - is KSParenthesizedReference -> element.isClassifierReference - else -> error("Unexpected KSReferenceElement: $this") - } +//internal inline fun Resolver.getSymbolsWithAnnotation(inDepth: Boolean = false) = +// getSymbolsWithAnnotation(A::class.qualifiedName!!, inDepth) +// +//internal fun Resolver.getNewClasses() = getNewFiles().flatMap { it.declarations.filterIsInstance() } +// +//internal inline fun KSAnnotation.isOfType() = isOfType(A::class.qualifiedName!!) +// +//internal fun KSAnnotation.isOfType(qualifiedName: String) = annotationType.resolve() +// .declaration.let { if (it is KSTypeAlias) it.findActualType() else it } +// .qualifiedName?.asString() == qualifiedName +// +//@OptIn(KspExperimental::class) +//internal inline fun KSAnnotated.getAnnotationsByType() = getAnnotationsByType(A::class) +// +//internal class AnnotationArguments private constructor( +// private val arguments: Map, +//) { +// private fun getArgument(parameter: KProperty1) = arguments.getValue(parameter.name) +// private val KProperty1.value get() = getArgument(this).value +// +// fun isDefault(parameter: KProperty1) = getArgument(parameter).isDefault() +// +// // can't return non-nullable values because of https://github.com/google/ksp/issues/885 +// operator fun get(parameter: KProperty1) = parameter.value as KSAnnotation? +// operator fun get(parameter: KProperty1>) = +// @Suppress("UNCHECKED_CAST") (parameter.value as List?) +// +// companion object { +// fun KSAnnotation.arguments() = +// AnnotationArguments(arguments.associateBy { it.name!!.asString() }) +// } +//} +// +//@Suppress("RecursivePropertyAccessor") +//internal val KSReferenceElement.isClassifierReference: Boolean +// get() = when (this) { +// is KSDynamicReference, is KSCallableReference -> false +// is KSClassifierReference -> true +// is KSDefNonNullReference -> enclosedType.isClassifierReference +// is KSParenthesizedReference -> element.isClassifierReference +// else -> error("Unexpected KSReferenceElement: $this") +// } diff --git a/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt b/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt index 5176abe6d84..9d73919edc4 100644 --- a/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt +++ b/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt @@ -1,122 +1,122 @@ package dev.kord.ksp - -import com.squareup.kotlinpoet.* -import com.squareup.kotlinpoet.MemberName.Companion.member -import kotlin.annotation.AnnotationRetention.SOURCE -import kotlin.annotation.AnnotationTarget.TYPE - -// for scope control, see https://kotlinlang.org/docs/type-safe-builders.html#scope-control-dslmarker -@DslMarker -@Retention(SOURCE) -@Target(TYPE) -internal annotation class KotlinPoetDsl - -internal typealias FileSpecBuilder = (@KotlinPoetDsl FileSpec.Builder).() -> Unit -internal typealias TypeSpecBuilder = (@KotlinPoetDsl TypeSpec.Builder).() -> Unit -internal typealias AnnotationSpecBuilder = (@KotlinPoetDsl AnnotationSpec.Builder).() -> Unit -internal typealias FunSpecBuilder = (@KotlinPoetDsl FunSpec.Builder).() -> Unit -internal typealias PropertySpecBuilder = (@KotlinPoetDsl PropertySpec.Builder).() -> Unit -internal typealias ParameterSpecBuilder = (@KotlinPoetDsl ParameterSpec.Builder).() -> Unit -internal typealias CodeBlockBuilder = (@KotlinPoetDsl CodeBlock.Builder).() -> Unit - - -// miscellaneous - -internal inline fun > E.asMemberName() = E::class.member(name) - -internal inline fun FileSpec(packageName: String, fileName: String, builder: FileSpecBuilder) = - FileSpec.builder(packageName, fileName).apply(builder).build() - - -// extensions for `Annotatable.Builder` - -@DelicateKotlinPoetApi("See 'AnnotationSpec.get'") -internal fun > T.addAnnotation( - annotation: Annotation, - includeDefaultValues: Boolean = false, -) = addAnnotation(AnnotationSpec.get(annotation, includeDefaultValues)) - -internal inline fun > T.addAnnotation(type: ClassName, builder: AnnotationSpecBuilder) = - addAnnotation(AnnotationSpec.builder(type).apply(builder).build()) - -internal inline fun Annotatable.Builder<*>.addAnnotation(builder: AnnotationSpecBuilder) = - addAnnotation(AnnotationSpec.builder(A::class).apply(builder).build()) - - -// extensions for `TypeSpecHolder.Builder` - -internal inline fun > T.addClass(name: String, builder: TypeSpecBuilder) = - addType(TypeSpec.classBuilder(name).apply(builder).build()) - -internal inline fun > T.addClass(className: ClassName, builder: TypeSpecBuilder) = - addType(TypeSpec.classBuilder(className).apply(builder).build()) - -internal inline fun > T.addObject(name: String, builder: TypeSpecBuilder) = - addType(TypeSpec.objectBuilder(name).apply(builder).build()) - - -// extensions for `MemberSpecHolder.Builder` - -internal inline fun > T.addFunction(name: String, builder: FunSpecBuilder) = - addFunction(FunSpec.builder(name).apply(builder).build()) - -internal inline fun MemberSpecHolder.Builder<*>.addProperty( - name: String, - vararg modifiers: KModifier, - builder: PropertySpecBuilder, -) = addProperty(PropertySpec.builder(name, typeNameOf(), *modifiers).apply(builder).build()) - -internal inline fun > T.addProperty( - name: String, - type: TypeName, - vararg modifiers: KModifier, - builder: PropertySpecBuilder, -) = addProperty(PropertySpec.builder(name, type, *modifiers).apply(builder).build()) - - -// extensions for `TypeSpec.Builder` - -internal inline fun TypeSpec.Builder.addCompanionObject(name: String? = null, builder: TypeSpecBuilder) = - addType(TypeSpec.companionObjectBuilder(name).apply(builder).build()) - -internal inline fun TypeSpec.Builder.primaryConstructor(builder: FunSpecBuilder) = - primaryConstructor(FunSpec.constructorBuilder().apply(builder).build()) - -internal inline fun TypeSpec.Builder.addInitializerBlock(builder: CodeBlockBuilder) = - addInitializerBlock(CodeBlock.builder().apply(builder).build()) - - -// extensions for `FunSpec.Builder` - -internal inline fun FunSpec.Builder.addParameter( - name: String, - type: TypeName, - vararg modifiers: KModifier, - builder: ParameterSpecBuilder, -) = addParameter(ParameterSpec.builder(name, type, *modifiers).apply(builder).build()) - -internal inline fun FunSpec.Builder.addParameter(name: String, vararg modifiers: KModifier) = - addParameter(name, typeNameOf(), *modifiers) - -internal inline fun FunSpec.Builder.returns() = returns(typeNameOf()) - -internal inline fun FunSpec.Builder.withControlFlow(controlFlow: String, vararg args: Any, builder: FunSpecBuilder) = - beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() - - -// extensions for `PropertySpec.Builder` - -internal inline fun PropertySpec.Builder.delegate(builder: CodeBlockBuilder) = - delegate(CodeBlock.builder().apply(builder).build()) - -internal inline fun PropertySpec.Builder.getter(builder: FunSpecBuilder) = - getter(FunSpec.getterBuilder().apply(builder).build()) - - -// extensions for `CodeBlock.Builder` - -internal inline fun CodeBlock.Builder.withControlFlow( - controlFlow: String, - vararg args: Any?, - builder: CodeBlockBuilder, -) = beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() +// +//import com.squareup.kotlinpoet.* +//import com.squareup.kotlinpoet.MemberName.Companion.member +//import kotlin.annotation.AnnotationRetention.SOURCE +//import kotlin.annotation.AnnotationTarget.TYPE +// +//// for scope control, see https://kotlinlang.org/docs/type-safe-builders.html#scope-control-dslmarker +//@DslMarker +//@Retention(SOURCE) +//@Target(TYPE) +//internal annotation class KotlinPoetDsl +// +//internal typealias FileSpecBuilder = (@KotlinPoetDsl FileSpec.Builder).() -> Unit +//internal typealias TypeSpecBuilder = (@KotlinPoetDsl TypeSpec.Builder).() -> Unit +//internal typealias AnnotationSpecBuilder = (@KotlinPoetDsl AnnotationSpec.Builder).() -> Unit +//internal typealias FunSpecBuilder = (@KotlinPoetDsl FunSpec.Builder).() -> Unit +//internal typealias PropertySpecBuilder = (@KotlinPoetDsl PropertySpec.Builder).() -> Unit +//internal typealias ParameterSpecBuilder = (@KotlinPoetDsl ParameterSpec.Builder).() -> Unit +//internal typealias CodeBlockBuilder = (@KotlinPoetDsl CodeBlock.Builder).() -> Unit +// +// +//// miscellaneous +// +//internal inline fun > E.asMemberName() = E::class.member(name) +// +//internal inline fun FileSpec(packageName: String, fileName: String, builder: FileSpecBuilder) = +// FileSpec.builder(packageName, fileName).apply(builder).build() +// +// +//// extensions for `Annotatable.Builder` +// +//@DelicateKotlinPoetApi("See 'AnnotationSpec.get'") +//internal fun > T.addAnnotation( +// annotation: Annotation, +// includeDefaultValues: Boolean = false, +//) = addAnnotation(AnnotationSpec.get(annotation, includeDefaultValues)) +// +//internal inline fun > T.addAnnotation(type: ClassName, builder: AnnotationSpecBuilder) = +// addAnnotation(AnnotationSpec.builder(type).apply(builder).build()) +// +//internal inline fun Annotatable.Builder<*>.addAnnotation(builder: AnnotationSpecBuilder) = +// addAnnotation(AnnotationSpec.builder(A::class).apply(builder).build()) +// +// +//// extensions for `TypeSpecHolder.Builder` +// +//internal inline fun > T.addClass(name: String, builder: TypeSpecBuilder) = +// addType(TypeSpec.classBuilder(name).apply(builder).build()) +// +//internal inline fun > T.addClass(className: ClassName, builder: TypeSpecBuilder) = +// addType(TypeSpec.classBuilder(className).apply(builder).build()) +// +//internal inline fun > T.addObject(name: String, builder: TypeSpecBuilder) = +// addType(TypeSpec.objectBuilder(name).apply(builder).build()) +// +// +//// extensions for `MemberSpecHolder.Builder` +// +//internal inline fun > T.addFunction(name: String, builder: FunSpecBuilder) = +// addFunction(FunSpec.builder(name).apply(builder).build()) +// +//internal inline fun MemberSpecHolder.Builder<*>.addProperty( +// name: String, +// vararg modifiers: KModifier, +// builder: PropertySpecBuilder, +//) = addProperty(PropertySpec.builder(name, typeNameOf(), *modifiers).apply(builder).build()) +// +//internal inline fun > T.addProperty( +// name: String, +// type: TypeName, +// vararg modifiers: KModifier, +// builder: PropertySpecBuilder, +//) = addProperty(PropertySpec.builder(name, type, *modifiers).apply(builder).build()) +// +// +//// extensions for `TypeSpec.Builder` +// +//internal inline fun TypeSpec.Builder.addCompanionObject(name: String? = null, builder: TypeSpecBuilder) = +// addType(TypeSpec.companionObjectBuilder(name).apply(builder).build()) +// +//internal inline fun TypeSpec.Builder.primaryConstructor(builder: FunSpecBuilder) = +// primaryConstructor(FunSpec.constructorBuilder().apply(builder).build()) +// +//internal inline fun TypeSpec.Builder.addInitializerBlock(builder: CodeBlockBuilder) = +// addInitializerBlock(CodeBlock.builder().apply(builder).build()) +// +// +//// extensions for `FunSpec.Builder` +// +//internal inline fun FunSpec.Builder.addParameter( +// name: String, +// type: TypeName, +// vararg modifiers: KModifier, +// builder: ParameterSpecBuilder, +//) = addParameter(ParameterSpec.builder(name, type, *modifiers).apply(builder).build()) +// +//internal inline fun FunSpec.Builder.addParameter(name: String, vararg modifiers: KModifier) = +// addParameter(name, typeNameOf(), *modifiers) +// +//internal inline fun FunSpec.Builder.returns() = returns(typeNameOf()) +// +//internal inline fun FunSpec.Builder.withControlFlow(controlFlow: String, vararg args: Any, builder: FunSpecBuilder) = +// beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() +// +// +//// extensions for `PropertySpec.Builder` +// +//internal inline fun PropertySpec.Builder.delegate(builder: CodeBlockBuilder) = +// delegate(CodeBlock.builder().apply(builder).build()) +// +//internal inline fun PropertySpec.Builder.getter(builder: FunSpecBuilder) = +// getter(FunSpec.getterBuilder().apply(builder).build()) +// +// +//// extensions for `CodeBlock.Builder` +// +//internal inline fun CodeBlock.Builder.withControlFlow( +// controlFlow: String, +// vararg args: Any?, +// builder: CodeBlockBuilder, +//) = beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() diff --git a/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt b/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt index 1d887e879b7..e162737a39f 100644 --- a/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt +++ b/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt @@ -2,7 +2,7 @@ package dev.kord.ksp.generation import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.symbol.KSAnnotation -import dev.kord.ksp.AnnotationArguments.Companion.arguments +import dev.kord.codegen.ksp.annotations.AnnotationArguments.Companion.arguments import dev.kord.ksp.Generate import dev.kord.ksp.Generate.EntityType import dev.kord.ksp.Generate.EntityType.* diff --git a/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt b/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt index 288a07b9996..b89d14f0793 100644 --- a/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt +++ b/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt @@ -4,12 +4,12 @@ import com.google.devtools.ksp.processing.* import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSFile import com.squareup.kotlinpoet.ksp.writeTo +import dev.kord.codegen.ksp.getAnnotationsOfType +import dev.kord.codegen.ksp.getSymbolsWithAnnotation +import dev.kord.codegen.ksp.isOfType import dev.kord.ksp.Generate import dev.kord.ksp.generation.bitflags.generateFileSpec import dev.kord.ksp.generation.kordenum.generateFileSpec -import dev.kord.ksp.getAnnotationsByType -import dev.kord.ksp.getSymbolsWithAnnotation -import dev.kord.ksp.isOfType /** [SymbolProcessorProvider] for [GenerationProcessor]. */ class GenerationProcessorProvider : SymbolProcessorProvider { @@ -33,7 +33,7 @@ private class GenerationProcessor( } private fun processFile(file: KSFile) { - val generates = file.getAnnotationsByType() + val generates = file.getAnnotationsOfType() val annotations = file.annotations.filter { it.isOfType() } (generates zip annotations) .mapNotNull { (generate, annotation) -> generate.toGenerationEntityOrNull(logger, annotation) } diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt b/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt index 10ae5f009c2..8bb9012defe 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt @@ -6,7 +6,17 @@ import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.SET import com.squareup.kotlinpoet.ksp.addOriginatingKSFile -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addCompanionObject +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addInitializerBlock +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.getter +import dev.kord.codegen.kotlinpoet.primaryConstructor +import dev.kord.codegen.kotlinpoet.returns +import dev.kord.codegen.kotlinpoet.withControlFlow import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt b/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt index 5bccf4fe2b3..a029b20f590 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt @@ -3,7 +3,11 @@ package dev.kord.ksp.generation.bitflags import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.primaryConstructor import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt b/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt index 2ab53bcd710..e54d86f06e0 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt @@ -2,15 +2,15 @@ package dev.kord.ksp.generation.bitflags import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* -import dev.kord.ksp.addAnnotation -import dev.kord.ksp.addFunction +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.returns import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT import dev.kord.ksp.generation.shared.CONTRACT import dev.kord.ksp.generation.shared.EXACTLY_ONCE import dev.kord.ksp.generation.shared.GenerationContext -import dev.kord.ksp.returns context(BitFlags, GenerationContext) internal fun TypeSpec.Builder.addContains(parameterName: String, parameterType: TypeName) = addFunction("contains") { diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt b/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt index d3e4c0608d5..333c39c0857 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt @@ -5,7 +5,12 @@ import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.jvm.jvmField -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addCompanionObject +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.primaryConstructor import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.shared.GenerationContext import dev.kord.ksp.generation.shared.K_SERIALIZER diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt b/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt index 796e02c2750..fac609907fd 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt @@ -4,14 +4,14 @@ import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.jvm.jvmName -import dev.kord.ksp.addAnnotation -import dev.kord.ksp.addFunction -import dev.kord.ksp.addParameter +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.withControlFlow import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.shared.CONTRACT import dev.kord.ksp.generation.shared.EXACTLY_ONCE import dev.kord.ksp.generation.shared.GenerationContext -import dev.kord.ksp.withControlFlow context(GenerationContext) private val BitFlags.factoryFunctionName diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt b/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt index 1da43eefe47..d4bce6bcd2c 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt @@ -5,10 +5,10 @@ import com.squareup.kotlinpoet.KModifier.PRIVATE import com.squareup.kotlinpoet.MemberName import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.addFunction -import dev.kord.ksp.addObject -import dev.kord.ksp.addParameter -import dev.kord.ksp.addProperty +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addObject +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.addProperty import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt b/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt index a13724fbf57..2e1283107bc 100644 --- a/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt +++ b/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt @@ -4,7 +4,13 @@ import com.google.devtools.ksp.symbol.KSFile import com.squareup.kotlinpoet.KModifier.PUBLIC import com.squareup.kotlinpoet.KModifier.SEALED import com.squareup.kotlinpoet.ksp.addOriginatingKSFile -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addCompanionObject +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.primaryConstructor +import dev.kord.codegen.kotlinpoet.withControlFlow import dev.kord.ksp.generation.GenerationEntity.KordEnum import dev.kord.ksp.generation.shared.* import kotlinx.serialization.Serializable diff --git a/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt b/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt index 91a13cd0493..74f00a1ee02 100644 --- a/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt +++ b/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt @@ -2,9 +2,9 @@ package dev.kord.ksp.generation.kordenum import com.squareup.kotlinpoet.KModifier.OVERRIDE import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.addFunction -import dev.kord.ksp.addObject -import dev.kord.ksp.addParameter +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addObject +import dev.kord.codegen.kotlinpoet.addParameter import dev.kord.ksp.generation.GenerationEntity.KordEnum import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt b/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt index 79419e2fa17..d6da35ae2b8 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt @@ -4,10 +4,10 @@ import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.KModifier.FINAL import com.squareup.kotlinpoet.KModifier.OVERRIDE import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.addFunction -import dev.kord.ksp.addParameter +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.returns +import dev.kord.codegen.kotlinpoet.addParameter import dev.kord.ksp.generation.GenerationEntity -import dev.kord.ksp.returns internal fun TypeSpec.Builder.addEqualsAndHashCodeBasedOnClassAndSingleProperty( className: ClassName, diff --git a/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt b/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt index e59208b219f..dad64d35297 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt @@ -4,8 +4,8 @@ import com.google.devtools.ksp.symbol.KSFile import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.DelicateKotlinPoetApi import com.squareup.kotlinpoet.FileSpec -import dev.kord.ksp.FileSpec -import dev.kord.ksp.addAnnotation +import dev.kord.codegen.kotlinpoet.FileSpec +import dev.kord.codegen.kotlinpoet.addAnnotation import dev.kord.ksp.generation.GenerationEntity internal class GenerationContext( diff --git a/ksp-processors/src/main/kotlin/generation/shared/Names.kt b/ksp-processors/src/main/kotlin/generation/shared/Names.kt index d92fa341ce9..646262fcae3 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/Names.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/Names.kt @@ -1,16 +1,24 @@ +@file:OptIn(ExperimentalContracts::class, DelicateKotlinPoetApi::class) + package dev.kord.ksp.generation.shared import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.DelicateKotlinPoetApi import com.squareup.kotlinpoet.MemberName -import com.squareup.kotlinpoet.MemberName.Companion.member import com.squareup.kotlinpoet.asClassName +import dev.kord.codegen.kotlinpoet.asMemberName import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract internal val OPT_IN = ClassName("kotlin", "OptIn") -internal val CONTRACT = MemberName("kotlin.contracts", "contract") -internal val EXACTLY_ONCE = ClassName("kotlin.contracts", "InvocationKind").member("EXACTLY_ONCE") -internal val PRIMITIVE_SERIAL_DESCRIPTOR = MemberName("kotlinx.serialization.descriptors", "PrimitiveSerialDescriptor") +internal val CONTRACT = ::contract.asMemberName() +internal val EXACTLY_ONCE = InvocationKind.EXACTLY_ONCE.asMemberName() + +internal val PRIMITIVE_SERIAL_DESCRIPTOR = ::PrimitiveSerialDescriptor.asMemberName() internal val K_SERIALIZER = KSerializer::class.asClassName() internal val DISCORD_BIT_SET = ClassName("dev.kord.common", "DiscordBitSet") diff --git a/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt b/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt index 6231e76a2c4..d97eb28a327 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt @@ -4,7 +4,7 @@ import com.squareup.kotlinpoet.Annotatable import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.joinToCode -import dev.kord.ksp.addAnnotation +import dev.kord.codegen.kotlinpoet.addAnnotation context(GenerationContext) internal fun Annotatable.Builder<*>.addEntryOptIns() { diff --git a/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt b/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt index 5208a238370..21463f18a82 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt @@ -3,6 +3,14 @@ package dev.kord.ksp.generation.shared import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addObject +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.asMemberName +import dev.kord.codegen.kotlinpoet.delegate +import dev.kord.codegen.kotlinpoet.withControlFlow +import dev.kord.codegen.kotlinpoet.primaryConstructor import dev.kord.ksp.* import dev.kord.ksp.generation.GenerationEntity import kotlinx.serialization.descriptors.SerialDescriptor From f8ceadf63df6ca7bc90d3325e66102b737ab563c Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 12 Aug 2024 01:07:24 +0200 Subject: [PATCH 2/5] Delete obsolete files --- ksp-processors/src/main/kotlin/KSPUtils.kt | 52 -------- .../src/main/kotlin/KotlinPoetDsl.kt | 122 ------------------ 2 files changed, 174 deletions(-) delete mode 100644 ksp-processors/src/main/kotlin/KSPUtils.kt delete mode 100644 ksp-processors/src/main/kotlin/KotlinPoetDsl.kt diff --git a/ksp-processors/src/main/kotlin/KSPUtils.kt b/ksp-processors/src/main/kotlin/KSPUtils.kt deleted file mode 100644 index 0486d482a37..00000000000 --- a/ksp-processors/src/main/kotlin/KSPUtils.kt +++ /dev/null @@ -1,52 +0,0 @@ -package dev.kord.ksp - -import com.google.devtools.ksp.KspExperimental -import com.google.devtools.ksp.findActualType -import com.google.devtools.ksp.getAnnotationsByType -import com.google.devtools.ksp.isDefault -import com.google.devtools.ksp.processing.Resolver -import com.google.devtools.ksp.symbol.* -import kotlin.reflect.KProperty1 - -//internal inline fun Resolver.getSymbolsWithAnnotation(inDepth: Boolean = false) = -// getSymbolsWithAnnotation(A::class.qualifiedName!!, inDepth) -// -//internal fun Resolver.getNewClasses() = getNewFiles().flatMap { it.declarations.filterIsInstance() } -// -//internal inline fun KSAnnotation.isOfType() = isOfType(A::class.qualifiedName!!) -// -//internal fun KSAnnotation.isOfType(qualifiedName: String) = annotationType.resolve() -// .declaration.let { if (it is KSTypeAlias) it.findActualType() else it } -// .qualifiedName?.asString() == qualifiedName -// -//@OptIn(KspExperimental::class) -//internal inline fun KSAnnotated.getAnnotationsByType() = getAnnotationsByType(A::class) -// -//internal class AnnotationArguments private constructor( -// private val arguments: Map, -//) { -// private fun getArgument(parameter: KProperty1) = arguments.getValue(parameter.name) -// private val KProperty1.value get() = getArgument(this).value -// -// fun isDefault(parameter: KProperty1) = getArgument(parameter).isDefault() -// -// // can't return non-nullable values because of https://github.com/google/ksp/issues/885 -// operator fun get(parameter: KProperty1) = parameter.value as KSAnnotation? -// operator fun get(parameter: KProperty1>) = -// @Suppress("UNCHECKED_CAST") (parameter.value as List?) -// -// companion object { -// fun KSAnnotation.arguments() = -// AnnotationArguments(arguments.associateBy { it.name!!.asString() }) -// } -//} -// -//@Suppress("RecursivePropertyAccessor") -//internal val KSReferenceElement.isClassifierReference: Boolean -// get() = when (this) { -// is KSDynamicReference, is KSCallableReference -> false -// is KSClassifierReference -> true -// is KSDefNonNullReference -> enclosedType.isClassifierReference -// is KSParenthesizedReference -> element.isClassifierReference -// else -> error("Unexpected KSReferenceElement: $this") -// } diff --git a/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt b/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt deleted file mode 100644 index 9d73919edc4..00000000000 --- a/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt +++ /dev/null @@ -1,122 +0,0 @@ -package dev.kord.ksp -// -//import com.squareup.kotlinpoet.* -//import com.squareup.kotlinpoet.MemberName.Companion.member -//import kotlin.annotation.AnnotationRetention.SOURCE -//import kotlin.annotation.AnnotationTarget.TYPE -// -//// for scope control, see https://kotlinlang.org/docs/type-safe-builders.html#scope-control-dslmarker -//@DslMarker -//@Retention(SOURCE) -//@Target(TYPE) -//internal annotation class KotlinPoetDsl -// -//internal typealias FileSpecBuilder = (@KotlinPoetDsl FileSpec.Builder).() -> Unit -//internal typealias TypeSpecBuilder = (@KotlinPoetDsl TypeSpec.Builder).() -> Unit -//internal typealias AnnotationSpecBuilder = (@KotlinPoetDsl AnnotationSpec.Builder).() -> Unit -//internal typealias FunSpecBuilder = (@KotlinPoetDsl FunSpec.Builder).() -> Unit -//internal typealias PropertySpecBuilder = (@KotlinPoetDsl PropertySpec.Builder).() -> Unit -//internal typealias ParameterSpecBuilder = (@KotlinPoetDsl ParameterSpec.Builder).() -> Unit -//internal typealias CodeBlockBuilder = (@KotlinPoetDsl CodeBlock.Builder).() -> Unit -// -// -//// miscellaneous -// -//internal inline fun > E.asMemberName() = E::class.member(name) -// -//internal inline fun FileSpec(packageName: String, fileName: String, builder: FileSpecBuilder) = -// FileSpec.builder(packageName, fileName).apply(builder).build() -// -// -//// extensions for `Annotatable.Builder` -// -//@DelicateKotlinPoetApi("See 'AnnotationSpec.get'") -//internal fun > T.addAnnotation( -// annotation: Annotation, -// includeDefaultValues: Boolean = false, -//) = addAnnotation(AnnotationSpec.get(annotation, includeDefaultValues)) -// -//internal inline fun > T.addAnnotation(type: ClassName, builder: AnnotationSpecBuilder) = -// addAnnotation(AnnotationSpec.builder(type).apply(builder).build()) -// -//internal inline fun Annotatable.Builder<*>.addAnnotation(builder: AnnotationSpecBuilder) = -// addAnnotation(AnnotationSpec.builder(A::class).apply(builder).build()) -// -// -//// extensions for `TypeSpecHolder.Builder` -// -//internal inline fun > T.addClass(name: String, builder: TypeSpecBuilder) = -// addType(TypeSpec.classBuilder(name).apply(builder).build()) -// -//internal inline fun > T.addClass(className: ClassName, builder: TypeSpecBuilder) = -// addType(TypeSpec.classBuilder(className).apply(builder).build()) -// -//internal inline fun > T.addObject(name: String, builder: TypeSpecBuilder) = -// addType(TypeSpec.objectBuilder(name).apply(builder).build()) -// -// -//// extensions for `MemberSpecHolder.Builder` -// -//internal inline fun > T.addFunction(name: String, builder: FunSpecBuilder) = -// addFunction(FunSpec.builder(name).apply(builder).build()) -// -//internal inline fun MemberSpecHolder.Builder<*>.addProperty( -// name: String, -// vararg modifiers: KModifier, -// builder: PropertySpecBuilder, -//) = addProperty(PropertySpec.builder(name, typeNameOf(), *modifiers).apply(builder).build()) -// -//internal inline fun > T.addProperty( -// name: String, -// type: TypeName, -// vararg modifiers: KModifier, -// builder: PropertySpecBuilder, -//) = addProperty(PropertySpec.builder(name, type, *modifiers).apply(builder).build()) -// -// -//// extensions for `TypeSpec.Builder` -// -//internal inline fun TypeSpec.Builder.addCompanionObject(name: String? = null, builder: TypeSpecBuilder) = -// addType(TypeSpec.companionObjectBuilder(name).apply(builder).build()) -// -//internal inline fun TypeSpec.Builder.primaryConstructor(builder: FunSpecBuilder) = -// primaryConstructor(FunSpec.constructorBuilder().apply(builder).build()) -// -//internal inline fun TypeSpec.Builder.addInitializerBlock(builder: CodeBlockBuilder) = -// addInitializerBlock(CodeBlock.builder().apply(builder).build()) -// -// -//// extensions for `FunSpec.Builder` -// -//internal inline fun FunSpec.Builder.addParameter( -// name: String, -// type: TypeName, -// vararg modifiers: KModifier, -// builder: ParameterSpecBuilder, -//) = addParameter(ParameterSpec.builder(name, type, *modifiers).apply(builder).build()) -// -//internal inline fun FunSpec.Builder.addParameter(name: String, vararg modifiers: KModifier) = -// addParameter(name, typeNameOf(), *modifiers) -// -//internal inline fun FunSpec.Builder.returns() = returns(typeNameOf()) -// -//internal inline fun FunSpec.Builder.withControlFlow(controlFlow: String, vararg args: Any, builder: FunSpecBuilder) = -// beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() -// -// -//// extensions for `PropertySpec.Builder` -// -//internal inline fun PropertySpec.Builder.delegate(builder: CodeBlockBuilder) = -// delegate(CodeBlock.builder().apply(builder).build()) -// -//internal inline fun PropertySpec.Builder.getter(builder: FunSpecBuilder) = -// getter(FunSpec.getterBuilder().apply(builder).build()) -// -// -//// extensions for `CodeBlock.Builder` -// -//internal inline fun CodeBlock.Builder.withControlFlow( -// controlFlow: String, -// vararg args: Any?, -// builder: CodeBlockBuilder, -//) = beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() From 4b3dc0266baa032013424623d2ff9465c0a241eb Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Mon, 12 Aug 2024 01:07:59 +0200 Subject: [PATCH 3/5] Revert publishing changes --- buildSrc/src/main/kotlin/Projects.kt | 2 +- .../main/kotlin/kord-publishing.gradle.kts | 23 +++++-------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index 14d33789d2f..be8c41f12ee 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -29,5 +29,5 @@ val Project.isRelease get() = tag != null object Repo { const val releasesUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" - const val snapshotsUrl = "https://repo.kord.dev/snapshots" + const val snapshotsUrl = "https://oss.sonatype.org/content/repositories/snapshots/" } diff --git a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts index 2d14e99d69c..a001f7113cc 100644 --- a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts @@ -60,23 +60,12 @@ publishing { } repositories { - if(isRelease) { - maven { - url = uri(Repo.releasesUrl) + maven { + url = uri(if (isRelease) Repo.releasesUrl else Repo.snapshotsUrl) - credentials { - username = getenv("NEXUS_USER") - password = getenv("NEXUS_PASSWORD") - } - } - } else { - maven { - url = uri(Repo.snapshotsUrl) - - credentials { - username = getenv("REPOSILITE_USER") - password = getenv("REPOSILITE_PASSWORD") - } + credentials { + username = getenv("NEXUS_USER") + password = getenv("NEXUS_PASSWORD") } } } @@ -86,5 +75,5 @@ signing { val secretKey = getenv("SIGNING_KEY")?.let { String(Base64.getDecoder().decode(it)) } val password = getenv("SIGNING_PASSWORD") useInMemoryPgpKeys(secretKey, password) -// sign(publishing.publications) + sign(publishing.publications) } From 663410ea5c397c54bf364705126a0a6278d6cae1 Mon Sep 17 00:00:00 2001 From: lukellmann Date: Sat, 16 Sep 2023 20:32:34 +0200 Subject: [PATCH 4/5] Use codegen.kt instead of KotlinPoetDsl.kt --- .../src/main/kotlin/kord-module.gradle.kts | 1 + .../kord-multiplatform-module.gradle.kts | 1 + gradle/libs.versions.toml | 3 + ksp-processors/build.gradle.kts | 6 + .../src/main/kotlin/KotlinPoetDsl.kt | 122 ------------------ .../generation/bitflags/BitFlagsGeneration.kt | 4 +- .../kotlin/generation/bitflags/Builder.kt | 2 +- .../bitflags/ContainsPlusMinusCopy.kt | 6 +- .../generation/bitflags/FactoryFunctions.kt | 1 - .../kotlin/generation/bitflags/Serializer.kt | 8 +- .../generation/kordenum/KordEnumGeneration.kt | 2 +- .../kotlin/generation/kordenum/Serializer.kt | 6 +- .../shared/EqualsHashCodeToString.kt | 6 +- .../shared/FileSpecForGenerationEntity.kt | 4 +- .../main/kotlin/generation/shared/OptIns.kt | 2 +- .../kotlin/generation/shared/SharedContent.kt | 2 +- 16 files changed, 32 insertions(+), 144 deletions(-) delete mode 100644 ksp-processors/src/main/kotlin/KotlinPoetDsl.kt diff --git a/buildSrc/src/main/kotlin/kord-module.gradle.kts b/buildSrc/src/main/kotlin/kord-module.gradle.kts index 37397688ec0..fa495bd5121 100644 --- a/buildSrc/src/main/kotlin/kord-module.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-module.gradle.kts @@ -10,6 +10,7 @@ plugins { repositories { mavenCentral() + maven("https://oss.sonatype.org/content/repositories/snapshots") } dependencies { diff --git a/buildSrc/src/main/kotlin/kord-multiplatform-module.gradle.kts b/buildSrc/src/main/kotlin/kord-multiplatform-module.gradle.kts index db9f9064e8f..5b6990a9733 100644 --- a/buildSrc/src/main/kotlin/kord-multiplatform-module.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-multiplatform-module.gradle.kts @@ -12,6 +12,7 @@ plugins { repositories { mavenCentral() + maven("https://oss.sonatype.org/content/repositories/snapshots") } dependencies { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1599989c8fa..4bfe2c7a3bb 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,6 +19,7 @@ fastZlib = "2.0.1" # https://github.com/timotejroiko/fast-zlib # code generation ksp = "2.0.21-1.0.25" # https://github.com/google/ksp kotlinpoet = "1.18.1" # https://github.com/square/kotlinpoet +codegen-kt = "main-SNAPSHOT" # https://github.com/kordlib/codegen-kt # tests junit-jupiter = "5.11.2" # https://github.com/junit-team/junit5 @@ -68,6 +69,8 @@ stately-collections = { module = "co.touchlab:stately-concurrent-collections", v ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinpoet" } kotlinpoet-ksp = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlinpoet" } +codegen-kt-kotlinpoet = { module = "dev.kord.codegen:kotlinpoet", version.ref = "codegen-kt" } +codegen-kt-ksp = { module = "dev.kord.codegen:ksp", version.ref = "codegen-kt" } # tests kotlin-test-annotations-common = { module = "org.jetbrains.kotlin:kotlin-test-annotations-common", version.ref = "kotlin" } diff --git a/ksp-processors/build.gradle.kts b/ksp-processors/build.gradle.kts index 5df3edcbbfe..793818edc6e 100644 --- a/ksp-processors/build.gradle.kts +++ b/ksp-processors/build.gradle.kts @@ -8,12 +8,18 @@ kotlin { } } +repositories { + maven("https://oss.sonatype.org/content/repositories/snapshots") +} + dependencies { implementation(projects.kspAnnotations) implementation(libs.ksp.api) implementation(libs.kotlinpoet) implementation(libs.kotlinpoet.ksp) + implementation(libs.codegen.kt.kotlinpoet) + implementation(libs.codegen.kt.ksp) implementation(libs.kotlinx.serialization.json) // use types directly } diff --git a/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt b/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt deleted file mode 100644 index 5176abe6d84..00000000000 --- a/ksp-processors/src/main/kotlin/KotlinPoetDsl.kt +++ /dev/null @@ -1,122 +0,0 @@ -package dev.kord.ksp - -import com.squareup.kotlinpoet.* -import com.squareup.kotlinpoet.MemberName.Companion.member -import kotlin.annotation.AnnotationRetention.SOURCE -import kotlin.annotation.AnnotationTarget.TYPE - -// for scope control, see https://kotlinlang.org/docs/type-safe-builders.html#scope-control-dslmarker -@DslMarker -@Retention(SOURCE) -@Target(TYPE) -internal annotation class KotlinPoetDsl - -internal typealias FileSpecBuilder = (@KotlinPoetDsl FileSpec.Builder).() -> Unit -internal typealias TypeSpecBuilder = (@KotlinPoetDsl TypeSpec.Builder).() -> Unit -internal typealias AnnotationSpecBuilder = (@KotlinPoetDsl AnnotationSpec.Builder).() -> Unit -internal typealias FunSpecBuilder = (@KotlinPoetDsl FunSpec.Builder).() -> Unit -internal typealias PropertySpecBuilder = (@KotlinPoetDsl PropertySpec.Builder).() -> Unit -internal typealias ParameterSpecBuilder = (@KotlinPoetDsl ParameterSpec.Builder).() -> Unit -internal typealias CodeBlockBuilder = (@KotlinPoetDsl CodeBlock.Builder).() -> Unit - - -// miscellaneous - -internal inline fun > E.asMemberName() = E::class.member(name) - -internal inline fun FileSpec(packageName: String, fileName: String, builder: FileSpecBuilder) = - FileSpec.builder(packageName, fileName).apply(builder).build() - - -// extensions for `Annotatable.Builder` - -@DelicateKotlinPoetApi("See 'AnnotationSpec.get'") -internal fun > T.addAnnotation( - annotation: Annotation, - includeDefaultValues: Boolean = false, -) = addAnnotation(AnnotationSpec.get(annotation, includeDefaultValues)) - -internal inline fun > T.addAnnotation(type: ClassName, builder: AnnotationSpecBuilder) = - addAnnotation(AnnotationSpec.builder(type).apply(builder).build()) - -internal inline fun Annotatable.Builder<*>.addAnnotation(builder: AnnotationSpecBuilder) = - addAnnotation(AnnotationSpec.builder(A::class).apply(builder).build()) - - -// extensions for `TypeSpecHolder.Builder` - -internal inline fun > T.addClass(name: String, builder: TypeSpecBuilder) = - addType(TypeSpec.classBuilder(name).apply(builder).build()) - -internal inline fun > T.addClass(className: ClassName, builder: TypeSpecBuilder) = - addType(TypeSpec.classBuilder(className).apply(builder).build()) - -internal inline fun > T.addObject(name: String, builder: TypeSpecBuilder) = - addType(TypeSpec.objectBuilder(name).apply(builder).build()) - - -// extensions for `MemberSpecHolder.Builder` - -internal inline fun > T.addFunction(name: String, builder: FunSpecBuilder) = - addFunction(FunSpec.builder(name).apply(builder).build()) - -internal inline fun MemberSpecHolder.Builder<*>.addProperty( - name: String, - vararg modifiers: KModifier, - builder: PropertySpecBuilder, -) = addProperty(PropertySpec.builder(name, typeNameOf(), *modifiers).apply(builder).build()) - -internal inline fun > T.addProperty( - name: String, - type: TypeName, - vararg modifiers: KModifier, - builder: PropertySpecBuilder, -) = addProperty(PropertySpec.builder(name, type, *modifiers).apply(builder).build()) - - -// extensions for `TypeSpec.Builder` - -internal inline fun TypeSpec.Builder.addCompanionObject(name: String? = null, builder: TypeSpecBuilder) = - addType(TypeSpec.companionObjectBuilder(name).apply(builder).build()) - -internal inline fun TypeSpec.Builder.primaryConstructor(builder: FunSpecBuilder) = - primaryConstructor(FunSpec.constructorBuilder().apply(builder).build()) - -internal inline fun TypeSpec.Builder.addInitializerBlock(builder: CodeBlockBuilder) = - addInitializerBlock(CodeBlock.builder().apply(builder).build()) - - -// extensions for `FunSpec.Builder` - -internal inline fun FunSpec.Builder.addParameter( - name: String, - type: TypeName, - vararg modifiers: KModifier, - builder: ParameterSpecBuilder, -) = addParameter(ParameterSpec.builder(name, type, *modifiers).apply(builder).build()) - -internal inline fun FunSpec.Builder.addParameter(name: String, vararg modifiers: KModifier) = - addParameter(name, typeNameOf(), *modifiers) - -internal inline fun FunSpec.Builder.returns() = returns(typeNameOf()) - -internal inline fun FunSpec.Builder.withControlFlow(controlFlow: String, vararg args: Any, builder: FunSpecBuilder) = - beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() - - -// extensions for `PropertySpec.Builder` - -internal inline fun PropertySpec.Builder.delegate(builder: CodeBlockBuilder) = - delegate(CodeBlock.builder().apply(builder).build()) - -internal inline fun PropertySpec.Builder.getter(builder: FunSpecBuilder) = - getter(FunSpec.getterBuilder().apply(builder).build()) - - -// extensions for `CodeBlock.Builder` - -internal inline fun CodeBlock.Builder.withControlFlow( - controlFlow: String, - vararg args: Any?, - builder: CodeBlockBuilder, -) = beginControlFlow(controlFlow, *args).apply(builder).endControlFlow() diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt b/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt index 72697797fc1..904803081c1 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt @@ -3,10 +3,9 @@ package dev.kord.ksp.generation.bitflags import com.google.devtools.ksp.symbol.KSFile -import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy -import com.squareup.kotlinpoet.SET import com.squareup.kotlinpoet.ksp.addOriginatingKSFile import dev.kord.ksp.* import dev.kord.ksp.generation.GenerationEntity.BitFlags @@ -14,7 +13,6 @@ import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT import dev.kord.ksp.generation.shared.* import kotlinx.serialization.Serializable -import com.squareup.kotlinpoet.INT as INT_CN context(GenerationContext) internal val BitFlags.collectionCN diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt b/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt index ca0ad0acbba..4c7245e804e 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/Builder.kt @@ -5,7 +5,7 @@ package dev.kord.ksp.generation.bitflags import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.* import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt b/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt index f32bf1d10ab..88987d6943f 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt @@ -2,9 +2,11 @@ package dev.kord.ksp.generation.bitflags -import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* -import dev.kord.ksp.addAnnotation +import com.squareup.kotlinpoet.LambdaTypeName +import com.squareup.kotlinpoet.TypeName +import com.squareup.kotlinpoet.TypeSpec +import com.squareup.kotlinpoet.UNIT import dev.kord.ksp.addFunction import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt b/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt index 5658590aeeb..471a10d0746 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt @@ -6,7 +6,6 @@ import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.jvm.jvmName -import dev.kord.ksp.addAnnotation import dev.kord.ksp.addFunction import dev.kord.ksp.addParameter import dev.kord.ksp.generation.GenerationEntity.BitFlags diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt b/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt index 130c08dd9fc..0058f497bac 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/Serializer.kt @@ -7,10 +7,10 @@ import com.squareup.kotlinpoet.KModifier.PRIVATE import com.squareup.kotlinpoet.MemberName import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.addFunction -import dev.kord.ksp.addObject -import dev.kord.ksp.addParameter -import dev.kord.ksp.addProperty +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addObject +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.addParameter import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt b/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt index a13724fbf57..eb2b36ffc8f 100644 --- a/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt +++ b/ksp-processors/src/main/kotlin/generation/kordenum/KordEnumGeneration.kt @@ -4,7 +4,7 @@ import com.google.devtools.ksp.symbol.KSFile import com.squareup.kotlinpoet.KModifier.PUBLIC import com.squareup.kotlinpoet.KModifier.SEALED import com.squareup.kotlinpoet.ksp.addOriginatingKSFile -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.* import dev.kord.ksp.generation.GenerationEntity.KordEnum import dev.kord.ksp.generation.shared.* import kotlinx.serialization.Serializable diff --git a/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt b/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt index 864ed48e039..daa6086d59c 100644 --- a/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt +++ b/ksp-processors/src/main/kotlin/generation/kordenum/Serializer.kt @@ -4,9 +4,9 @@ package dev.kord.ksp.generation.kordenum import com.squareup.kotlinpoet.KModifier.OVERRIDE import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.addFunction -import dev.kord.ksp.addObject -import dev.kord.ksp.addParameter +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addObject +import dev.kord.codegen.kotlinpoet.addParameter import dev.kord.ksp.generation.GenerationEntity.KordEnum import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType import dev.kord.ksp.generation.GenerationEntity.KordEnum.ValueType.INT diff --git a/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt b/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt index b82c7031c88..8e8a791e31d 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/EqualsHashCodeToString.kt @@ -6,10 +6,10 @@ import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.KModifier.FINAL import com.squareup.kotlinpoet.KModifier.OVERRIDE import com.squareup.kotlinpoet.TypeSpec -import dev.kord.ksp.addFunction -import dev.kord.ksp.addParameter +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.returns import dev.kord.ksp.generation.GenerationEntity -import dev.kord.ksp.returns internal fun TypeSpec.Builder.addEqualsAndHashCodeBasedOnClassAndSingleProperty( className: ClassName, diff --git a/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt b/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt index 9021e12cdbc..915c5c89c20 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/FileSpecForGenerationEntity.kt @@ -6,8 +6,8 @@ import com.google.devtools.ksp.symbol.KSFile import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.DelicateKotlinPoetApi import com.squareup.kotlinpoet.FileSpec -import dev.kord.ksp.FileSpec -import dev.kord.ksp.addAnnotation +import dev.kord.codegen.kotlinpoet.FileSpec +import dev.kord.codegen.kotlinpoet.addAnnotation import dev.kord.ksp.generation.GenerationEntity internal class GenerationContext( diff --git a/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt b/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt index 23bdf44c843..581cb4e89ff 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/OptIns.kt @@ -6,7 +6,7 @@ import com.squareup.kotlinpoet.Annotatable import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.joinToCode -import dev.kord.ksp.addAnnotation +import dev.kord.codegen.kotlinpoet.addAnnotation context(GenerationContext) internal fun Annotatable.Builder<*>.addEntryOptIns() { diff --git a/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt b/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt index 89b808ddab3..b9b2ac56758 100644 --- a/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt +++ b/ksp-processors/src/main/kotlin/generation/shared/SharedContent.kt @@ -5,7 +5,7 @@ package dev.kord.ksp.generation.shared import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.* import dev.kord.ksp.generation.GenerationEntity import kotlinx.serialization.descriptors.SerialDescriptor From cca4a6638758fcaef1f318de7d72de1d28b94863 Mon Sep 17 00:00:00 2001 From: lukellmann Date: Sun, 17 Sep 2023 01:10:41 +0200 Subject: [PATCH 5/5] Use codegen.kt instead of KSPUtils.kt --- ksp-processors/src/main/kotlin/KSPUtils.kt | 43 ------------------- .../kotlin/generation/GenerationEntity.kt | 2 +- .../kotlin/generation/GenerationProcessor.kt | 4 +- .../generation/bitflags/BitFlagsGeneration.kt | 14 +++++- .../bitflags/ContainsPlusMinusCopy.kt | 6 ++- .../bitflags/DeprecatedNewCompanion.kt | 7 ++- .../generation/bitflags/FactoryFunctions.kt | 7 +-- 7 files changed, 30 insertions(+), 53 deletions(-) diff --git a/ksp-processors/src/main/kotlin/KSPUtils.kt b/ksp-processors/src/main/kotlin/KSPUtils.kt index bab27d53157..b203c5c21ae 100644 --- a/ksp-processors/src/main/kotlin/KSPUtils.kt +++ b/ksp-processors/src/main/kotlin/KSPUtils.kt @@ -1,52 +1,9 @@ package dev.kord.ksp import com.google.devtools.ksp.KspExperimental -import com.google.devtools.ksp.findActualType import com.google.devtools.ksp.getAnnotationsByType -import com.google.devtools.ksp.isDefault import com.google.devtools.ksp.processing.Resolver import com.google.devtools.ksp.symbol.* -import kotlin.reflect.KProperty1 - -internal inline fun Resolver.getSymbolsWithAnnotation(inDepth: Boolean = false) = - getSymbolsWithAnnotation(A::class.qualifiedName!!, inDepth) - -internal fun Resolver.getNewClasses() = getNewFiles().flatMap { it.declarations.filterIsInstance() } - -internal inline fun KSAnnotation.isOfType() = isOfType(A::class.qualifiedName!!) - -internal fun KSAnnotation.isOfType(qualifiedName: String) = annotationType.resolve() - .declaration.let { if (it is KSTypeAlias) it.findActualType() else it } - .qualifiedName?.asString() == qualifiedName @OptIn(KspExperimental::class) internal inline fun KSAnnotated.getAnnotationsByType() = getAnnotationsByType(A::class) - -internal class AnnotationArguments private constructor( - private val arguments: Map, -) { - private fun getArgument(parameter: KProperty1) = arguments.getValue(parameter.name) - private val KProperty1.value get() = getArgument(this).value - - fun isDefault(parameter: KProperty1) = getArgument(parameter).isDefault() - - // can't return non-nullable values because of https://github.com/google/ksp/issues/885 - operator fun get(parameter: KProperty1) = parameter.value as KSAnnotation? - operator fun get(parameter: KProperty1>) = - @Suppress("UNCHECKED_CAST") (parameter.value as List?) - - companion object { - fun KSAnnotation.arguments() = - AnnotationArguments(arguments.associateBy { it.name!!.asString() }) - } -} - -@Suppress("RecursivePropertyAccessor") -internal val KSReferenceElement.isClassifierReference: Boolean - get() = when (this) { - is KSDynamicReference, is KSCallableReference -> false - is KSClassifierReference -> true - is KSDefNonNullReference -> enclosedType.isClassifierReference - is KSParenthesizedReference -> element.isClassifierReference - else -> error("Unexpected KSReferenceElement: $this") - } diff --git a/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt b/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt index 1d887e879b7..e162737a39f 100644 --- a/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt +++ b/ksp-processors/src/main/kotlin/generation/GenerationEntity.kt @@ -2,7 +2,7 @@ package dev.kord.ksp.generation import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.symbol.KSAnnotation -import dev.kord.ksp.AnnotationArguments.Companion.arguments +import dev.kord.codegen.ksp.annotations.AnnotationArguments.Companion.arguments import dev.kord.ksp.Generate import dev.kord.ksp.Generate.EntityType import dev.kord.ksp.Generate.EntityType.* diff --git a/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt b/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt index 288a07b9996..dcc8cb50af9 100644 --- a/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt +++ b/ksp-processors/src/main/kotlin/generation/GenerationProcessor.kt @@ -4,12 +4,12 @@ import com.google.devtools.ksp.processing.* import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSFile import com.squareup.kotlinpoet.ksp.writeTo +import dev.kord.codegen.ksp.getSymbolsWithAnnotation +import dev.kord.codegen.ksp.isOfType import dev.kord.ksp.Generate import dev.kord.ksp.generation.bitflags.generateFileSpec import dev.kord.ksp.generation.kordenum.generateFileSpec import dev.kord.ksp.getAnnotationsByType -import dev.kord.ksp.getSymbolsWithAnnotation -import dev.kord.ksp.isOfType /** [SymbolProcessorProvider] for [GenerationProcessor]. */ class GenerationProcessorProvider : SymbolProcessorProvider { diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt b/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt index 904803081c1..09df455b7c1 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/BitFlagsGeneration.kt @@ -7,12 +7,23 @@ import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.ksp.addOriginatingKSFile -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addCompanionObject +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addInitializerBlock +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.getter +import dev.kord.codegen.kotlinpoet.primaryConstructor +import dev.kord.codegen.kotlinpoet.returns +import dev.kord.codegen.kotlinpoet.withControlFlow import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT import dev.kord.ksp.generation.shared.* import kotlinx.serialization.Serializable +import com.squareup.kotlinpoet.INT as INT_CN context(GenerationContext) internal val BitFlags.collectionCN @@ -104,6 +115,7 @@ internal fun BitFlags.generateFileSpec(originatingFile: KSFile) = fileSpecForGen addStatement("shift++") } } + BIT_SET -> withControlFlow("for·(shift·in·0..<$valueName.size)") { addStatement("if·($valueName[shift])·add(%T.fromShift(shift))", entityCN) } diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt b/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt index 88987d6943f..04751d390bc 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/ContainsPlusMinusCopy.kt @@ -2,19 +2,21 @@ package dev.kord.ksp.generation.bitflags +import com.squareup.kotlinpoet.DelicateKotlinPoetApi import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.LambdaTypeName import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.UNIT -import dev.kord.ksp.addFunction +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.returns import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.BIT_SET import dev.kord.ksp.generation.GenerationEntity.BitFlags.ValueType.INT import dev.kord.ksp.generation.shared.CONTRACT import dev.kord.ksp.generation.shared.EXACTLY_ONCE import dev.kord.ksp.generation.shared.GenerationContext -import dev.kord.ksp.returns context(BitFlags, GenerationContext) internal fun TypeSpec.Builder.addContains(parameterName: String, parameterType: TypeName) = addFunction("contains") { diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt b/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt index 94c5d4ec3cb..5046735d994 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/DeprecatedNewCompanion.kt @@ -7,7 +7,12 @@ import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.jvm.jvmField -import dev.kord.ksp.* +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addClass +import dev.kord.codegen.kotlinpoet.addCompanionObject +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addProperty +import dev.kord.codegen.kotlinpoet.primaryConstructor import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.shared.GenerationContext import dev.kord.ksp.generation.shared.K_SERIALIZER diff --git a/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt b/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt index 471a10d0746..a21481aa50e 100644 --- a/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt +++ b/ksp-processors/src/main/kotlin/generation/bitflags/FactoryFunctions.kt @@ -6,13 +6,14 @@ import com.squareup.kotlinpoet.* import com.squareup.kotlinpoet.KModifier.* import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.jvm.jvmName -import dev.kord.ksp.addFunction -import dev.kord.ksp.addParameter +import dev.kord.codegen.kotlinpoet.addAnnotation +import dev.kord.codegen.kotlinpoet.addFunction +import dev.kord.codegen.kotlinpoet.addParameter +import dev.kord.codegen.kotlinpoet.withControlFlow import dev.kord.ksp.generation.GenerationEntity.BitFlags import dev.kord.ksp.generation.shared.CONTRACT import dev.kord.ksp.generation.shared.EXACTLY_ONCE import dev.kord.ksp.generation.shared.GenerationContext -import dev.kord.ksp.withControlFlow context(GenerationContext) private val BitFlags.factoryFunctionName