Skip to content

Commit

Permalink
feat: generate reflection config task
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvaar committed Jun 3, 2022
1 parent 23789a6 commit 7c58373
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ subprojects {
kotlinOptions {
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
}
finalizedBy("genReflectConfig")
}

protobuf {
Expand Down Expand Up @@ -100,6 +101,11 @@ subprojects {
}
}

tasks.register("genReflectConfig", GenerateReflectionConfig::class) {
configPath = "reflection-config.json"
packageNames = listOf("cosmos", "tendermint", "cosmos_proto", "okp4-cosmos-proto.okp4")
}

configure<PublishingExtension> {
repositories {
maven {
Expand Down
33 changes: 33 additions & 0 deletions kotlin/buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id("org.jetbrains.kotlin.jvm") version "1.5.31"
}

repositories {
mavenCentral()
}

dependencies {
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

val grpcVersion = "1.45.0"
api("io.grpc:grpc-stub:$grpcVersion")
api("io.grpc:grpc-protobuf:$grpcVersion")

val protobufVersion = "3.19.4"
api("com.google.protobuf:protobuf-java-util:$protobufVersion")
api("com.google.protobuf:protobuf-kotlin:$protobufVersion")

val grpcKotlinVersion = "1.2.1"
api("io.grpc:grpc-kotlin-stub:$grpcKotlinVersion")

api("com.google.code.gson:gson:2.9.0")
api(gradleApi())
api(kotlin("reflect"))
api("org.reflections:reflections:0.10.2")
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
1 change: 1 addition & 0 deletions kotlin/buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "reflection-task"
69 changes: 69 additions & 0 deletions kotlin/buildSrc/src/main/kotlin/ReflectionTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import com.google.gson.GsonBuilder
import com.google.protobuf.Descriptors
import com.google.protobuf.GeneratedMessageV3
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.reflections.Reflections
import java.io.File

data class ReflectionMethod(
val name: String,
val parameterTypes: List<String>
)

data class ReflectionConfig(
val name: String,
val methods: List<ReflectionMethod>,
)

open class GenerateReflectionConfig : DefaultTask() {
@Input
lateinit var configPath: String

@Input
lateinit var packageNames: List<String>

@TaskAction
fun generateReflectionConfigJSON() =
File(configPath).writeText(
GsonBuilder().setPrettyPrinting().create()
.toJson(
packageNames.asSequence().map { Reflections(it) }
.map { reflections ->
reflections.getSubTypesOf(GeneratedMessageV3::class.java)
.map { it.name }
}
.flatten()
.map { className ->
ReflectionConfig(
name = className,
methods = listOf(
ReflectionMethod(
name = "getDescriptor",
parameterTypes = emptyList()
)
)
)
}
.toMutableList()
.apply {
this.add(
Descriptors.Descriptor::class.java.let { c ->
ReflectionConfig(
name = c.name,
methods = c.methods .map {
ReflectionMethod(
name = it.name,
parameterTypes = it.parameterTypes.map { param ->
param.name
}
)
}
)
}
)
}
)
)
}

0 comments on commit 7c58373

Please sign in to comment.