kotlin-dsl-samples icon indicating copy to clipboard operation
kotlin-dsl-samples copied to clipboard

Sample plugin showing custom DSL

Open ryantheleach opened this issue 5 years ago • 4 comments

It would be great to see a plugin that provides a custom DSL, that matches kotlin-dsl styles for configuring tasks using custom functions.

If it's already in the samples, it's not clear from the sample readme where it is.

ryantheleach avatar Aug 29 '18 04:08 ryantheleach

Sry but I don't get your question 🙃 You can "get" a Task like tasks.withType<TaskType> { // set it up here }. Does that answer you question?

StefMa avatar Aug 29 '18 07:08 StefMa

@StefMa e.g. https://github.com/gradle/kotlin-dsl/blob/master/build.gradle.kts#L110-L140

Shows the build.gradle of this project, using the idea plugin + configuration.

It has several 'type safe kotlin builders / dsl's'

But attempting to make the same in a project extension hasn't worked well for me. Seeing if there are any utilities, or if it's all manual, or a basic example of how to do that, would be excellent in order to make the plugin I plan on publishing feel like a native kotlin-dsl plugin.

ryantheleach avatar Aug 29 '18 11:08 ryantheleach

Ok, understood. Maybe you can read this article which explains the basics about the Kotlin-DSL (from user perspective).

But you are talking about of having your own plugin (or plugin extension). You don't have to do anything special. The "nice dsl" will be created by the Gradle/Kotlin-DSL themself. You only have to create you extension like you have done it in the "old Groovy" days. As long as your "consumer" apply you plugin over the plugins {} block the DSL will be created for you.

But the way: This is not a special kotlin-dsl question. Such things should be better posted in the gradle forum.

StefMa avatar Aug 29 '18 12:08 StefMa

You only have to create you extension like you have done it in the "old Groovy" days.

I've been avoiding gradle due to groovy, this is the first time I've attempted to make my own plugins (and write Kotlin), most of the examples have been super helpful when combined with the gradle docs. Between my limited Scala knowledge, Java 8 experience, and C#, I've been able to mostly make heads and tails of Kotlin by referring to the documentation.

You don't have to do anything special. The "nice dsl" will be created by the Gradle/Kotlin-DSL themself.

From what? The few attempts I've made havn't had that nice syntax apply, which is why I've asked that it be a sample, so others can benefit. Even if the sample shows that you don't need to write the builders and glue code yourself, because currently that's the idea that I had, if you write a plugin in Kotlin Script, that you had to do that yourself (as opposed to writing it in groovy, where it would be generated).

(I'll push to my repo and show you when I have access to my changes next (I'm not on that machine currently))

ryantheleach avatar Aug 29 '18 15:08 ryantheleach