How to parse `.gradle` and `.gradle.kts` files into Kotlin data classes?
m
How to parse
.gradle
and
.gradle.kts
files into Kotlin data classes?
I want to extract
plugins
and
dependencies
from Gradle build files and map them into Kotlin data classes. Example Groovy DSL (
build.gradle
):
Copy code
plugins {
    id 'com.android.application' version '8.1.0'
}

dependencies {
    implementation "androidx.core:core-ktx:1.12.0"
}
Example Kotlin DSL (
build.gradle.kts
):
Copy code
plugins {
    id("com.android.application") version "8.1.0"
}

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
}
Target data classes:
Copy code
data class PluginEntry(val id: String, val version: String?)
data class DependencyEntry(val configuration: String, val notation: String)

data class GradleFile(
    val plugins: List<PluginEntry>,
    val dependencies: List<DependencyEntry>
)
👉 How can I parse both
.gradle
and
.gradle.kts
files into such Kotlin data classes? • Is there a library for this? • Or do I need different approaches for Groovy DSL and Kotlin DSL?
đź‘€ 1
v
If you know the exact syntax that might ever be used in the files you can simply use regex. If you want a full picture of the plugins and dependencies any parsing cannot work as you have fully turing-complete languages at hand, so the dependencies can for example be registered in different ways or can also be added by plugins and plugins can also apply other plugins, ... If you want a full picture the build needs to actually be configured and then the information extracted. For example by using the Gradle Tooling API, which is also what IDEs and similar use to extract information from a Gradle build.
m
ok understand thanks
👌 1
e
Gradle is able to parse some
plugins
blocks directly, https://github.com/gradle/gradle/blob/master/platforms/core-configuration/kotlin-dsl/src/main/kotlin/org/gradle/kotlin/dsl/execution/PluginsBlockInterpretation.kt but there is no guarantee that it will succeed and Gradle will fall back to executing the code to determine which plugins have been requested
there's no such thing for dependencies, those happen as part of the usual configuration process, and the possible syntax for those is far more varied
m
Thanks for sharing many syntax I see it currently I use it regex
v
Even if that parsing of the plugins block succeeds, it is only for determining the script classpath, isn't it? You still don't know which plugins get actually applied and which are applied by them if that is what you need to know. Heavily depends on the actual use-case.
p
I'm curious why it's needed, some context would help. Asking to possibly identify an XY problem where your real problem is different, and you're trying this path, while there may be other easier solutions 👍
m
thanks
@Piotr Krzemiński currently i can not share it why need it but i something interesting build it i will share feed done project
👍 1
https://github.com/Coding-Meet/DevAnalyzer @Piotr Krzemiński @Vampire this is usecase for .gradle
đź‘€ 1
in this project i use regex it very complicated get extract data library. that why ask here any way to prefect extract data library