Javi
06/10/2022, 2:20 PMtarget: Project
everywhere, but I am not able to find itVampire
06/10/2022, 2:21 PMtarget.apply {
...
}
?Chris Lee
06/10/2022, 2:22 PMabstract class MyPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run {
}
}
Vampire
06/10/2022, 2:23 PMrun
over apply
here?Chris Lee
06/10/2022, 2:25 PMJavi
06/10/2022, 2:31 PMVampire
06/10/2022, 2:33 PMabstract class BasePlugin(private val action: Project.() -> Unit) : Plugin<Project> {
override fun apply(project: Project): Unit = project.action()
}
?Javi
06/10/2022, 2:34 PMJavi
06/10/2022, 2:34 PMVampire
06/10/2022, 2:34 PMJavi
06/10/2022, 2:35 PMVampire
06/10/2022, 2:36 PMChris Lee
06/10/2022, 2:37 PMclass KotlinDslPlugin : Plugin<Project> {
override fun apply(project: Project): Unit = project.run {
Chris Lee
06/10/2022, 2:42 PMChris Lee
06/10/2022, 2:45 PMpublic abstract class FooPlugin(private val action : (Project).() -> Unit) : Plugin<Project> {
override fun apply(project: Project): Unit = project.action()
}
public abstract class BarPlugin : FooPlugin( {
tasks.named("build") {
dependsOn(tasks.named("blah"))
}
})
Javi
06/10/2022, 2:52 PMPlugin<Project>, Project by project
so you can get the context is Project
directlyChris Lee
06/10/2022, 2:53 PMJavi
06/10/2022, 2:59 PMtarget.apply
and so on, because with some formatters, you get a ugly format and/or a worse indentation. If the context of the class is already the project, it shouldn't be necessaryChris Lee
06/10/2022, 3:02 PMtarget.run
(presumably `target.apply`would format the same), using IntelliJ and Spotless/ktlint. It in fact reduces the indentation when used in the method declaration.Chris Lee
06/10/2022, 3:05 PMpublic abstract class Plugin1 : Plugin<Project> {
override fun apply(target: Project) {
target.run {
tasks {
named("build") {
dependsOn("something")
}
}
}
// can't use apply as it's already defined on the Project object
target.apply {
tasks {
named("build") {
dependsOn("something")
}
}
}
}
}
public abstract class Plugin2 : Plugin<Project> {
override fun apply(project: Project): Unit = project.run {
tasks {
named("build") {
dependsOn("something")
}
}
}
}
grossws
06/10/2022, 4:24 PMoverride fun apply(project: Project): Unit = project.apply { .. }
wouldn't work since kotlin's T.apply(T.() -> Unit)
returns T
and it would bind to Project#apply(Action)
that applies gradle plugin.ephemient
06/11/2022, 3:41 AMwith(target) { ... }
over
target.run { ... }
but they're equivalentephemient
06/11/2022, 3:43 AMUnit
return type, the type of the expression doesn't matterephemient
06/11/2022, 3:45 AMoverride fun apply(target: T) {
target._apply()
}
private fun T._apply() {
...
}
although sadly you can't avoid having some bridge method