is it possible to create a .kts/kt config file and...
# gradle
a
is it possible to create a .kts/kt config file and use that during some sort of gradle task? my project is a compose multiplatform project i would like to do what settings.gradle.kts does (how it gives the users the way to configure their build), but I cannot find any documentation on this or how to start.
m
h
I decided to not use scripting but instead I use a normal main class/function with a custom JavaExec task because it’s way easier to setup than a custom scripting host. Downside are implicit properties/receivers, if you really need them. Otherwise, just generate custom code and add them to the srcDir.
I also guess, you can’t cache the scripting compiled classes, but you can cache the JavaExec/Kotlin classes because it’s a normal gradle task.
💡 1
a
@mbonnin im lost. what would the script be in this case and how could i import that in the gradle task? I am working on a library and I want to give my users a way to configure the library during build time (generate code). I was thinking of having some sort of kotlin class outside of the source set where people can set the configurations they need (if any)
writing the config in kotlin for the users would be ideal. still trying to figure out how this would work (if it can work)
v
What should this script configure? The build?
m
Does the user configuration need to be turing complete? Or is it just data?
a
What should this script configure?
Ideally this config will be in kotlin. i mentioned in the OP a kts file because that seems what settings.gradle.kts is using (i havent used kts on my own).
@mbonnin what do u mean by turing complete?
m
Execute random algorithm
a
actually, let me tell u specifics
m
Kotlin is good for algorithm, you have statements, loops etc
But if you need data input, other formats like json, toml, etc... are usually better suited
v
Ideally this config will be in kotlin. i mentioned in the OP a kts file because that seems what settings.gradle.kts is using (i havent used kts on my own).
I did not ask how the config should look like. I asked what it should configure.
What will be the effect of executing that script?
a
But if you need data input, other formats like json, toml, etc... are usually better suited
@mbonnin gotcha. the only reason why i think kotlin is better is because of strong types, but reading what you said, im reconsidering
m
There’s been quite some discussions lately about declarative vs imperative configuration and declarative Gradle, amper, etc...
The tldr; is everyone loves Kotlin but it’s longer to compile and harder to write tooling around it. Declarative data languages have some value
I’d love to have a data language that reuses “most” of the Kotlin syntax though
same 3
a
@Vampire I am working on this library which lets people configure their app's theming. right now they can set up their design tokens (theming attributes) via kotlin like this:
Copy code
val Theme = buildComposeTheme { 
colors = DesignTokens(
        primary to Color.Red
    )
}
i would like to be able to generate extensions functions for the users based on their desired theme. ie
ComposeTheme.colors.primary
currently this is done in runtime. trying to figure out ways of achieving the code gen. i think i would need a config on the build step of the project
m
Maybe codegen with KSP?
a
can KSP run annotated code? it only parses right?
m
You can generate code in a KSP processor so it can look at the existing constructs and augment them
This is how Moshi generates JSON parsers for an example
💯 1
a
brilliant. forgot completely about Moshi. need to check
m
If you have a pattern in the user code that you can identify, you can generate code using kotlinpoet and turn runtime behaviour into compile-time behaviour (and fail the build if some constraints are not satisfied)
Not 100% sure it’ll work for your use case though, it depends what you want to codegen
Especially, I’m not sure KSP can look “inside” function bodies, it’s mostly about signatures
h
No, KSP doesn’t look inside the function body
👍 1
a
thanks for the help folks. got a few nice ideas on how to move forward
👍 1
e
there's no shortage of non-turing-complete declarative languages and some of those are typed. https://cel.dev/ for example, although there isn't a Kotlin implementation (you'll have to use a Java implementation or build your own)
v
Or the shiny new Pkl 🙂
☝️ 1