Eric
02/21/2022, 2:09 PMinline fun <reified T : Any> PluginManager.apply() = apply(T::class.java)
inline fun <reified T : Plugin<*>> PluginContainer.getPlugin(): T = getPlugin(T::class.java)
inline fun <reified T : Task> TaskContainer.withType(): TaskCollection<T> = withType(T::class.java)
inline fun <reified T : Any> ExtensionContainer.findByType(): T? = findByType(T::class.java)
inline fun <reified T : Any> ExtensionContainer.configure(action: Action<T>) = configure(T::class.java, action)
Searching is difficult because because I just get hits on “how to write plugins” etc.
Then I can write more “Kotlin-esque” code like this:
plugin.pluginManager.apply<DetektPlugin>()
plugin.extensions.configure<DetektExtension> { detekt ->
// ...
}
val detekt = target.tasks.withType<Detekt>().first()
Sam
02/21/2022, 2:11 PMkotlin-dsl
plugin to your plugin project?Sam
02/21/2022, 2:12 PMSam
02/21/2022, 2:14 PMbuild.gradle.kts
scriptsSam
02/21/2022, 2:16 PMSam
02/21/2022, 2:17 PMgradleKotlinDsl()
dependency for you, which you could also do by hand if you preferEric
02/21/2022, 2:18 PMdependencies {
// others...
implementation("org.gradle.kotlin:gradle-kotlin-dsl-plugins:2.2.0")
}
But I don’t see ext functions like those in the imported jar.Vampire
02/21/2022, 2:20 PMSam
02/21/2022, 2:20 PMdependencies {
implementation(gradleKotlinDsl())
}
or
plugins {
`kotlin-dsl`
}
Sam
02/21/2022, 2:21 PMVampire
02/21/2022, 2:23 PMkotlin-dsl
plugin does more than just adding that dependency.
kotlin-dsl
applies java-gradle-plugin
, kotlin-dsl.base
, and kotlin-dsl.precompiled-script-plugins
plugins
kotlin-dsl.base
plugin applies embedded-kotlin
, adds gradleKotlinDsl()
to the dependencies of compileOnly
and testImplementation
configurations, and configures the Kotlin DSL compiler plugins for example for proper SAM conversion for Action
and similar.
So if you are just after having the Gradle Kotlin DSL classes in your dependencies, you can use gradleKotlinDsl()
🙂Eric
02/21/2022, 2:27 PMEric
02/21/2022, 2:32 PMSam
02/21/2022, 2:34 PMEric
02/21/2022, 2:38 PMinline fun <reified T : Any> KClass<T>.simpleClassName() = java.simpleName.replaceFirstChar { it.lowercase() }
It can’t resolve replaceFirstChar
now.Sam
02/21/2022, 2:39 PMkotlin-dsl
plugin will make sure you're using the version of Kotlin that corresponds to the version embedded in your Gradle version. It's possible that's an older version than what you were using before.Sam
02/21/2022, 2:40 PMreplaceFirstChar
was added in 1.5Eric
02/21/2022, 2:41 PMeskatos
02/22/2022, 7:23 AM