kotlin-frontend-plugin icon indicating copy to clipboard operation
kotlin-frontend-plugin copied to clipboard

Use gradle kotlin script in examples

Open LouisCAD opened this issue 7 years ago • 10 comments
trafficstars

This would be great for the Definitely Kotlin people, or those who wish to use more static build scripts.

LouisCAD avatar Sep 26 '18 23:09 LouisCAD

This is the one thing blocking us from using the front end plugin instead of manually running command line command ourselves. Can't seem to get this to work with the Gradle Kotlin DSL.

AlteaDown avatar Feb 20 '19 14:02 AlteaDown

Can't seem to get this to work with the Gradle Kotlin DSL.

What is your problem exactly? Maybe I can help... (I use it successfully with Kotlin DSL.)

NorbertSandor avatar Feb 20 '19 14:02 NorbertSandor

I can't seem to get the syntax right, especially around the dsl within the kotlinFrontend {} lambda. Specifically, I'm trying to make the webpackBundle {}, resolve or work in anyways, but I can't seem to get it to work. So I have tried digging through the source to figure out what I should be putting, but end up not getting very far. An example of a kotlinFrontend {} lambda in Gradle Kotlin DSL would be immensely useful.

AlteaDown avatar Feb 20 '19 15:02 AlteaDown

An example of a kotlinFrontend {} lambda in Gradle Kotlin DSL would be immensely useful.

An extract of my build.gradle.kts, I hope it helps:

val kotlinFrontend: KotlinFrontendExtension by project
kotlinFrontend.apply {
    downloadNodeJsVersion = "..."

    bundle("webpack") {
        this as WebPackExtension
        bundleName = "main"
        sourceMapEnabled = true
        ...
    }
}

NorbertSandor avatar Feb 20 '19 16:02 NorbertSandor

Thank you, that is really helpful, and identifies what I was doing wrong.

AlteaDown avatar Feb 20 '19 16:02 AlteaDown

extensions.findByType<KotlinFrontendExtension>()?.apply{
...
}

This one is equivalent to groovy closure. One can write an extension to make it shorter like

inline fun <reified T: Any> Project.withExtension(block : T.()->Unit){
  extensions.findByType<T>()?.run(block)
}

I wander, why gradle does not have it.

Still, translating inner parts from Groovy is not always obvious. For example I am currently searching for a way to represent npm block.

altavir avatar Mar 01 '19 09:03 altavir

I am currently searching for a way to represent npm block.

I use simply:

val npm: NpmExtension by project.extensions
npm.apply { ... }

NorbertSandor avatar Mar 01 '19 11:03 NorbertSandor

Thanks. I've been searching in the wrong place, I though that it should be a part of KotlinFrontendExtension.

altavir avatar Mar 01 '19 11:03 altavir

Reading the above, I managed to simplify my code to the<KotlinFrontendExtension>().apply { … }. It is nice to see this as WebPackExtension confirmed as a pattern. I agree that a complete example might be instructive to have as part of this repository.

Do you folks import all the relevant classes, or is there some magic to avoid that?

gagern avatar Mar 21 '19 22:03 gagern

Among those already using this gradle plugin with Kotlin DSL successfully, does anyone want to edit the README.md to add such examples?

LouisCAD avatar Mar 22 '19 13:03 LouisCAD