Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Let the kotlin-dsl plugin configure precompiled script plugins support #1135

Merged
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ allprojects {
version = "1.0-SNAPSHOT"
}

val publishedPluginsVersion by extra { "1.0-rc-8" }
val futurePluginsVersion = "1.0-rc-9"
val publishedPluginsVersion by extra { "1.0-rc-9" }
val futurePluginsVersion = "1.0-rc-10"
project(":plugins") {
group = "org.gradle.kotlin"
version = futurePluginsVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ val PluginDependenciesSpec.`kotlin-dsl`: PluginDependencySpec
get() = id("org.gradle.kotlin.kotlin-dsl") version "$kotlinDslPluginsVersion"


/**
* The `kotlin-dsl.base` plugin.
*
* Equivalent to `id("org.gradle.kotlin.kotlin-dsl.base") version "$kotlinDslPluginsVersion"`
*
* You can also use `` `kotlin-dsl-base` version "$kotlinDslPluginsVersion" `` if you want to use a different version.
*
* @see org.gradle.kotlin.dsl.plugins.base.KotlinDslBasePlugin
*/
val PluginDependenciesSpec.`kotlin-dsl-base`: PluginDependencySpec
get() = id("org.gradle.kotlin.kotlin-dsl.base") version "$kotlinDslPluginsVersion"


/**
* The `kotlin-dsl.precompiled-script-plugins` plugin.
*
Expand Down
2 changes: 0 additions & 2 deletions samples/precompiled-script-plugin/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
plugins {
`java-gradle-plugin`
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
`maven-publish`
}

Expand Down
2 changes: 1 addition & 1 deletion samples/project-with-buildSrc/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project-with-buildSrc
=====================

A build with logic defined in [`buildSrc`](./buildSrc) including a [custom task](./buildSrc/src/main/kotlin/HelloTask.kt#L5) as well as [an extension property and an extension function](./buildSrc/src/main/kotlin/extensions.kt).
A build with logic defined in [`buildSrc`](./buildSrc) including a [custom task](./buildSrc/src/main/kotlin/HelloTask.kt#L5), a [custom precompiled script plugin](./buildSrc/src/main/kotlin/my-plugin.gradle.kts) as well as [an extension property and an extension function](./buildSrc/src/main/kotlin/extensions.kt).

List the available tasks with:

Expand Down
8 changes: 8 additions & 0 deletions samples/project-with-buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
plugins {
// Apply the `my-plugin` defined in buildSrc/src/main/kotlin/my-plugin.gradle.kts
id("my-plugin")
}

my {
flag.set(true)
}

withHelloTask()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.gradle.api.model.ObjectFactory

import org.gradle.kotlin.dsl.*


open class MyProjectExtension(objects: ObjectFactory) {

val flag = objects.property<Boolean>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

val my = extensions.create<MyProjectExtension>("my", project.objects)

tasks.register("myReport") {
doLast {
println("my.flag = ${my.flag.get()}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ class GradleApiExtensionsIntegrationTest : AbstractPluginIntegrationTest() {
withBuildScriptIn("buildSrc", """
plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
`java-gradle-plugin`
}

$repositoriesBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,6 @@ class GradleKotlinDslIntegrationTest : AbstractPluginIntegrationTest() {

plugins {
`kotlin-dsl`
`java-gradle-plugin`
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class PrecompiledScriptPluginIntegrationTest : AbstractPluginIntegrationTest() {
withBuildScript("""
plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
`java-gradle-plugin`
id("org.gradle.kotlin.ktlint-convention")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {

plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
`java-gradle-plugin`
}

$repositoriesBlock
Expand Down Expand Up @@ -581,7 +579,6 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
withFile("buildSrc/build.gradle.kts", """
plugins {
`kotlin-dsl`
`java-gradle-plugin`
}

gradlePlugin {
Expand Down Expand Up @@ -634,7 +631,6 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {

withBuildScriptIn("buildSrc", """
plugins {
`java-gradle-plugin`
`kotlin-dsl`
}

Expand Down Expand Up @@ -728,7 +724,6 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {

withBuildScriptIn("buildSrc", """
plugins {
`java-gradle-plugin`
`kotlin-dsl`
}

Expand Down
6 changes: 6 additions & 0 deletions subprojects/plugins/plugins.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ bundledGradlePlugin(
pluginId = "org.gradle.kotlin.kotlin-dsl",
pluginClass = "org.gradle.kotlin.dsl.plugins.dsl.KotlinDslPlugin")

bundledGradlePlugin(
name = "kotlinDslBase",
shortDescription = "Gradle Kotlin DSL Base Plugin",
pluginId = "org.gradle.kotlin.kotlin-dsl.base",
pluginClass = "org.gradle.kotlin.dsl.plugins.base.KotlinDslBasePlugin")

bundledGradlePlugin(
name = "kotlinDslCompilerSettings",
shortDescription = "Gradle Kotlin DSL Compiler Settings",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradle.kotlin.dsl.plugins.base

import org.gradle.api.Plugin
import org.gradle.api.Project

import org.gradle.kotlin.dsl.plugins.dsl.KotlinDslCompilerPlugins
import org.gradle.kotlin.dsl.plugins.dsl.KotlinDslPluginOptions
import org.gradle.kotlin.dsl.plugins.embedded.EmbeddedKotlinPlugin
import org.gradle.kotlin.dsl.plugins.embedded.kotlinArtifactConfigurationNames

import org.gradle.kotlin.dsl.*


/**
* The `kotlin-dsl-base` plugin.
*
* - Applies the `embedded-kotlin` plugin
* - Adds the `gradleKotlinDsl()` dependency to the `compileOnly` and `testImplementation` configurations
* - Configures the Kotlin DSL compiler plugins
*
* You can use the `kotlinDslPluginOptions` extension of type [KotlinDslPluginOptions] to configure the plugin.
*
* @see KotlinDslPluginOptions
* @see org.gradle.kotlin.dsl.plugins.embedded.EmbeddedKotlinPlugin
*/
class KotlinDslBasePlugin : Plugin<Project> {

override fun apply(project: Project): Unit = project.run {

apply<EmbeddedKotlinPlugin>()
createOptionsExtension()
apply<KotlinDslCompilerPlugins>()
addGradleKotlinDslDependencyTo(kotlinArtifactConfigurationNames)
}

private
fun Project.createOptionsExtension() =
extensions.add("kotlinDslPluginOptions", KotlinDslPluginOptions(objects))

private
fun Project.addGradleKotlinDslDependencyTo(configurations: List<String>) =
configurations.forEach {
dependencies.add(it, gradleKotlinDsl())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,31 @@ package org.gradle.kotlin.dsl.plugins.dsl
import org.gradle.api.Plugin
import org.gradle.api.Project

import org.gradle.kotlin.dsl.plugins.embedded.EmbeddedKotlinPlugin
import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin

import org.gradle.kotlin.dsl.plugins.base.KotlinDslBasePlugin
import org.gradle.kotlin.dsl.plugins.precompiled.PrecompiledScriptPlugins

import org.gradle.kotlin.dsl.*


/**
* The `kotlin-dsl` plugin.
*
* - Applies the `embedded-kotlin` plugin
* - Adds the `gradleKotlinDsl()` dependency to the `compileOnly` and `testImplementation` configurations
* - Configures the Kotlin DSL compiler plugins
*
* You can use the `kotlinDslPluginOptions` extension of type [KotlinDslPluginOptions] to configure the plugin.
* - Applies the `java-gradle-plugin` plugin
* - Applies the `kotlin-dsl.base` plugin
* - Applies the `kotlin-dsl.precompiled-script-plugins` plugin
*
* @see KotlinDslPluginOptions
* @see org.gradle.kotlin.dsl.plugins.embedded.EmbeddedKotlinPlugin
* @see JavaGradlePluginPlugin
* @see KotlinDslBasePlugin
* @see PrecompiledScriptPlugins
*/
class KotlinDslPlugin : Plugin<Project> {

override fun apply(project: Project) {
project.run {

applyEmbeddedKotlinPlugin()
createOptionsExtension()
applyKotlinDslCompilerPlugins()
addGradleKotlinDslDependencyTo("compileOnly", "testImplementation")
}
}

private
fun Project.applyEmbeddedKotlinPlugin() {
plugins.apply(EmbeddedKotlinPlugin::class.java)
}

private
fun Project.createOptionsExtension() {
extensions.add("kotlinDslPluginOptions", KotlinDslPluginOptions(objects))
}

private
fun Project.applyKotlinDslCompilerPlugins() {
plugins.apply(KotlinDslCompilerPlugins::class.java)
}
override fun apply(project: Project): Unit = project.run {

private
fun Project.addGradleKotlinDslDependencyTo(vararg configurations: String) {
configurations.forEach {
dependencies.add(it, gradleKotlinDsl())
}
apply<JavaGradlePluginPlugin>()
apply<KotlinDslBasePlugin>()
apply<PrecompiledScriptPlugins>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.logging.Logger

import org.gradle.api.plugins.JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME
import org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME

import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion

Expand Down Expand Up @@ -55,7 +58,7 @@ open class EmbeddedKotlinPlugin @Inject internal constructor(
embeddedKotlinConfiguration.name,
"stdlib-jdk8", "reflect")

listOf("compileOnly", "testImplementation").forEach {
kotlinArtifactConfigurationNames.forEach {
configurations.getByName(it).extendsFrom(embeddedKotlinConfiguration)
}

Expand All @@ -80,3 +83,8 @@ fun Logger.warnOnDifferentKotlinVersion(kotlinVersion: String?) {
)
}
}


internal
val kotlinArtifactConfigurationNames =
listOf(COMPILE_ONLY_CONFIGURATION_NAME, TEST_IMPLEMENTATION_CONFIGURATION_NAME)
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class KotlinDslPluginTest : AbstractPluginTest() {
withBuildScript("""

plugins {
`java-gradle-plugin`
`kotlin-dsl`
}

Expand Down Expand Up @@ -102,7 +101,6 @@ class KotlinDslPluginTest : AbstractPluginTest() {
withBuildScript("""

plugins {
`java-gradle-plugin`
`kotlin-dsl`
}

Expand Down