Introduce tooling model for Kotlin gradle projects

A KotlinProject model for Gradle projects including Kotlin (JVM, JS or
common) plugins is introduced. Its model builder KotlinModelBuilder
produces an implementation of these models and can be queried through
the Gradle Tooling API when needed. Model builder is registered in the
corresponding Kotlin Gradle plugins.

Model definition should be published as a separate artifact in public
repositories so it can be consumed when needed.
This commit is contained in:
Lucas Smaira
2018-06-04 16:33:02 +01:00
committed by Sergey Igushkin
parent 4b03db771a
commit 9def6f020f
24 changed files with 900 additions and 16 deletions
@@ -32,6 +32,7 @@ pill {
dependencies {
compile project(':kotlin-gradle-plugin-api')
compile project(':kotlin-gradle-plugin-model')
compileOnly project(':compiler')
compileOnly project(':compiler:incremental-compilation-impl')
compileOnly project(':compiler:daemon-common')
@@ -77,6 +78,7 @@ dependencies {
testCompile project(':kotlin-compiler-runner')
testCompile project(':kotlin-test::kotlin-test-junit')
testCompile "junit:junit:4.12"
testCompile "nl.jqno.equalsverifier:equalsverifier:2.1.5"
testCompileOnly project(':kotlin-reflect-api')
testCompileOnly project(':kotlin-annotation-processing')
testCompileOnly project(':kotlin-annotation-processing-gradle')
@@ -0,0 +1,112 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.builder
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.jetbrains.kotlin.gradle.model.CompilerArguments
import org.jetbrains.kotlin.gradle.model.ExperimentalFeatures
import org.jetbrains.kotlin.gradle.model.KotlinProject
import org.jetbrains.kotlin.gradle.model.SourceSet
import org.jetbrains.kotlin.gradle.model.impl.CompilerArgumentsImpl
import org.jetbrains.kotlin.gradle.model.impl.ExperimentalFeaturesImpl
import org.jetbrains.kotlin.gradle.model.impl.KotlinProjectImpl
import org.jetbrains.kotlin.gradle.model.impl.SourceSetImpl
import org.jetbrains.kotlin.gradle.plugin.KOTLIN_DSL_NAME
import org.jetbrains.kotlin.gradle.plugin.KOTLIN_JS_DSL_NAME
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.getConvention
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
/**
* [ToolingModelBuilder] for [KotlinProject] models.
* This model builder is registered for base Kotlin JVM, Kotlin JS and Kotlin Common plugins.
*/
class KotlinModelBuilder(private val kotlinPluginVersion: String) : ToolingModelBuilder {
override fun canBuild(modelName: String): Boolean {
return modelName == KotlinProject::class.java.name
}
override fun buildAll(modelName: String, project: Project): Any? {
if (modelName == KotlinProject::class.java.name) {
val kotlinCompileTasks = project.tasks.withType(AbstractKotlinCompile::class.java)
val projectType = getProjectType(project)
return KotlinProjectImpl(
project.name,
kotlinPluginVersion,
projectType,
kotlinCompileTasks.mapNotNull { it.createSourceSet(project, projectType) },
getExpectedByDependencies(project),
kotlinCompileTasks.first()!!.createExperimentalFeatures()
)
}
return null
}
companion object {
private fun getProjectType(project: Project): KotlinProject.ProjectType {
return if (project.plugins.hasPlugin("kotlin") || project.plugins.hasPlugin("kotlin-platform-jvm")) {
KotlinProject.ProjectType.PLATFORM_JVM
} else if (project.plugins.hasPlugin("kotlin2js") || project.plugins.hasPlugin("kotlin-platform-js")) {
KotlinProject.ProjectType.PLATFORM_JS
} else {
KotlinProject.ProjectType.PLATFORM_COMMON
}
}
private fun getExpectedByDependencies(project: Project): Collection<String> {
return listOf("expectedBy", "implement")
.flatMap { project.configurations.findByName(it)?.dependencies ?: emptySet<Dependency>() }
.filterIsInstance<ProjectDependency>()
.mapNotNull { it.dependencyProject }
.map { it.pathOrName() }
}
private fun Project.pathOrName() = if (path == ":") name else path
private fun AbstractKotlinCompile<*>.createSourceSet(project: Project, projectType: KotlinProject.ProjectType): SourceSet? {
val javaSourceSet =
project.convention.findPlugin(JavaPluginConvention::class.java)?.sourceSets?.find { it.name == sourceSetName }
val kotlinSourceSet =
javaSourceSet?.getConvention(if (projectType == KotlinProject.ProjectType.PLATFORM_JS) KOTLIN_JS_DSL_NAME else KOTLIN_DSL_NAME) as? KotlinSourceSet
return if (kotlinSourceSet != null) {
SourceSetImpl(
sourceSetName,
if (sourceSetName.contains("test", true)) SourceSet.SourceSetType.TEST else SourceSet.SourceSetType.PRODUCTION,
findFriendSourceSets(),
kotlinSourceSet.kotlin.srcDirs,
javaSourceSet.resources.srcDirs,
destinationDir,
javaSourceSet.output.resourcesDir,
createCompilerArguments()
)
} else null
}
private fun AbstractKotlinCompile<*>.findFriendSourceSets(): Collection<String> {
val friendSourceSets = ArrayList<String>()
friendTask?.sourceSetName?.let { friendSourceSets.add(it) }
return friendSourceSets
}
private fun AbstractKotlinCompile<*>.createCompilerArguments(): CompilerArguments {
return CompilerArgumentsImpl(
serializedCompilerArguments,
defaultSerializedCompilerArguments,
compileClasspath.toList()
)
}
private fun AbstractKotlinCompile<*>.createExperimentalFeatures(): ExperimentalFeatures {
return ExperimentalFeaturesImpl(coroutinesStr)
}
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.CompilerArguments
import java.io.File
import java.io.Serializable
/**
* Implementation of the [CompilerArguments] interface.
*/
data class CompilerArgumentsImpl(
private val myCurrentArguments: List<String>,
private val myDefaultArguments: List<String>,
private val myCompilerClasspath: List<File>
) : CompilerArguments, Serializable {
override fun getCurrentArguments(): List<String> {
return myCurrentArguments
}
override fun getDefaultArguments(): List<String> {
return myDefaultArguments
}
override fun getCompileClasspath(): List<File> {
return myCompilerClasspath
}
companion object {
private const val serialVersionUID = 1L
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.ExperimentalFeatures
import java.io.Serializable
/**
* Implementation of the [ExperimentalFeatures] interface.
*/
data class ExperimentalFeaturesImpl(
private val myCoroutines: String?
) : ExperimentalFeatures, Serializable {
override fun getCoroutines(): String? {
return myCoroutines
}
companion object {
private const val serialVersionUID = 1L
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.ExperimentalFeatures
import org.jetbrains.kotlin.gradle.model.KotlinProject
import org.jetbrains.kotlin.gradle.model.SourceSet
import java.io.Serializable
/**
* Implementation of the [KotlinProject] interface.
*/
data class KotlinProjectImpl(
private val myName: String,
private val myKotlinVersion: String,
private val myProjectType: KotlinProject.ProjectType,
private val mySourceSets: Collection<SourceSet>,
private val myExpectedByDependencies: Collection<String>,
private val myExperimentalFeatures: ExperimentalFeatures
) : KotlinProject, Serializable {
override fun getModelVersion(): Long {
return serialVersionUID
}
override fun getName(): String {
return myName
}
override fun getKotlinVersion(): String {
return myKotlinVersion
}
override fun getProjectType(): KotlinProject.ProjectType {
return myProjectType
}
override fun getSourceSets(): Collection<SourceSet> {
return mySourceSets
}
override fun getExpectedByDependencies(): Collection<String> {
return myExpectedByDependencies
}
override fun getExperimentalFeatures(): ExperimentalFeatures {
return myExperimentalFeatures
}
companion object {
private const val serialVersionUID = 1L
}
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.CompilerArguments
import org.jetbrains.kotlin.gradle.model.SourceSet
import java.io.File
import java.io.Serializable
/**
* Implementation of the [SourceSet] interface.
*/
data class SourceSetImpl(
private val myName: String,
private val myType: SourceSet.SourceSetType,
private val myFriendSourceSets: Collection<String>,
private val mySourceDirectories: Collection<File>,
private val myResourcesDirectories: Collection<File>,
private val myClassesOutputDirectory: File,
private val myResourcesOutputDirectory: File,
private val myCompilerArguments: CompilerArguments
) : SourceSet, Serializable {
override fun getName(): String {
return myName
}
override fun getType(): SourceSet.SourceSetType {
return myType
}
override fun getFriendSourceSets(): Collection<String> {
return myFriendSourceSets
}
override fun getSourceDirectories(): Collection<File> {
return mySourceDirectories
}
override fun getResourcesDirectories(): Collection<File> {
return myResourcesDirectories
}
override fun getClassesOutputDirectory(): File {
return myClassesOutputDirectory
}
override fun getResourcesOutputDirectory(): File {
return myResourcesOutputDirectory
}
override fun getCompilerArguments(): CompilerArguments {
return myCompilerArguments
}
companion object {
private const val serialVersionUID = 1L
}
}
@@ -23,12 +23,14 @@ import org.gradle.api.tasks.SourceSetOutput
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.jvm.tasks.Jar
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedClassesDir
import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin
import org.jetbrains.kotlin.gradle.internal.KaptVariantData
import org.jetbrains.kotlin.gradle.internal.checkAndroidAnnotationProcessorDependencyUsage
import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.utils.*
@@ -333,7 +335,8 @@ internal class KotlinCommonSourceSetProcessor(
internal abstract class AbstractKotlinPlugin(
val tasksProvider: KotlinTasksProvider,
val kotlinSourceSetProvider: KotlinSourceSetProvider,
protected val kotlinPluginVersion: String
protected val kotlinPluginVersion: String,
val registry: ToolingModelBuilderRegistry
) : Plugin<Project> {
internal abstract fun buildSourceSetProcessor(project: Project, javaBasePlugin: JavaBasePlugin, sourceSet: SourceSet, kotlinPluginVersion: String): KotlinSourceSetProcessor<*>
@@ -346,6 +349,7 @@ internal abstract class AbstractKotlinPlugin(
configureSourceSetDefaults(project, javaBasePlugin, javaPluginConvention)
configureDefaultVersionsResolutionStrategy(project)
configureClassInspectionForIC(project)
registry.register(KotlinModelBuilder(kotlinPluginVersion))
}
open protected fun configureSourceSetDefaults(
@@ -402,8 +406,9 @@ internal abstract class AbstractKotlinPlugin(
internal open class KotlinPlugin(
tasksProvider: KotlinTasksProvider,
kotlinSourceSetProvider: KotlinSourceSetProvider,
kotlinPluginVersion: String
) : AbstractKotlinPlugin(tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion) {
kotlinPluginVersion: String,
registry: ToolingModelBuilderRegistry
) : AbstractKotlinPlugin(tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion, registry) {
override fun buildSourceSetProcessor(project: Project, javaBasePlugin: JavaBasePlugin, sourceSet: SourceSet, kotlinPluginVersion: String) =
Kotlin2JvmSourceSetProcessor(project, javaBasePlugin, sourceSet, tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion)
@@ -416,8 +421,9 @@ internal open class KotlinPlugin(
internal open class KotlinCommonPlugin(
tasksProvider: KotlinTasksProvider,
kotlinSourceSetProvider: KotlinSourceSetProvider,
kotlinPluginVersion: String
) : AbstractKotlinPlugin(tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion) {
kotlinPluginVersion: String,
registry: ToolingModelBuilderRegistry
) : AbstractKotlinPlugin(tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion, registry) {
override fun buildSourceSetProcessor(project: Project, javaBasePlugin: JavaBasePlugin, sourceSet: SourceSet, kotlinPluginVersion: String) =
KotlinCommonSourceSetProcessor(project, javaBasePlugin, sourceSet, tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion)
}
@@ -425,8 +431,9 @@ internal open class KotlinCommonPlugin(
internal open class Kotlin2JsPlugin(
tasksProvider: KotlinTasksProvider,
kotlinSourceSetProvider: KotlinSourceSetProvider,
kotlinPluginVersion: String
) : AbstractKotlinPlugin(tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion) {
kotlinPluginVersion: String,
registry: ToolingModelBuilderRegistry
) : AbstractKotlinPlugin(tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion, registry) {
override fun buildSourceSetProcessor(project: Project, javaBasePlugin: JavaBasePlugin, sourceSet: SourceSet, kotlinPluginVersion: String) =
Kotlin2JsSourceSetProcessor(project, javaBasePlugin, sourceSet, tasksProvider, kotlinSourceSetProvider, kotlinPluginVersion)
}
@@ -21,6 +21,7 @@ import org.gradle.api.Project
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.createKotlinExtension
@@ -60,27 +61,27 @@ abstract class KotlinBasePluginWrapper(protected val fileResolver: FileResolver)
open val projectExtensionClass: KClass<out KotlinProjectExtension> get() = KotlinProjectExtension::class
}
open class KotlinPluginWrapper @Inject constructor(fileResolver: FileResolver): KotlinBasePluginWrapper(fileResolver) {
open class KotlinPluginWrapper @Inject internal constructor(fileResolver: FileResolver, protected val registry: ToolingModelBuilderRegistry): KotlinBasePluginWrapper(fileResolver) {
override fun getPlugin(kotlinGradleBuildServices: KotlinGradleBuildServices) =
KotlinPlugin(KotlinTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion)
KotlinPlugin(KotlinTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion, registry)
override val projectExtensionClass: KClass<out KotlinJvmProjectExtension>
get() = KotlinJvmProjectExtension::class
}
open class KotlinCommonPluginWrapper @Inject constructor(fileResolver: FileResolver): KotlinBasePluginWrapper(fileResolver) {
open class KotlinCommonPluginWrapper @Inject internal constructor(fileResolver: FileResolver, protected val registry: ToolingModelBuilderRegistry): KotlinBasePluginWrapper(fileResolver) {
override fun getPlugin(kotlinGradleBuildServices: KotlinGradleBuildServices) =
KotlinCommonPlugin(KotlinCommonTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion)
KotlinCommonPlugin(KotlinCommonTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion, registry)
}
open class KotlinAndroidPluginWrapper @Inject constructor(fileResolver: FileResolver): KotlinBasePluginWrapper(fileResolver) {
open class KotlinAndroidPluginWrapper @Inject internal constructor(fileResolver: FileResolver, protected val registry: ToolingModelBuilderRegistry): KotlinBasePluginWrapper(fileResolver) {
override fun getPlugin(kotlinGradleBuildServices: KotlinGradleBuildServices) =
KotlinAndroidPlugin(AndroidTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion)
}
open class Kotlin2JsPluginWrapper @Inject constructor(fileResolver: FileResolver): KotlinBasePluginWrapper(fileResolver) {
open class Kotlin2JsPluginWrapper @Inject internal constructor(fileResolver: FileResolver, protected val registry: ToolingModelBuilderRegistry): KotlinBasePluginWrapper(fileResolver) {
override fun getPlugin(kotlinGradleBuildServices: KotlinGradleBuildServices) =
Kotlin2JsPlugin(Kotlin2JsTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion)
Kotlin2JsPlugin(Kotlin2JsTasksProvider(), KotlinSourceSetProviderImpl(fileResolver), kotlinPluginVersion, registry)
}
fun Project.getKotlinPluginVersion(): String? =
@@ -148,7 +148,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
protected val additionalClasspath = arrayListOf<File>()
@get:Internal // classpath already participates in the checks
protected val compileClasspath: Iterable<File>
val compileClasspath: Iterable<File>
get() = (classpath + additionalClasspath)
.filterTo(LinkedHashSet(), File::exists)
@@ -201,7 +201,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
@Suppress("UNCHECKED_CAST")
@get:Internal
protected val friendTask: AbstractKotlinCompile<T>?
val friendTask: AbstractKotlinCompile<T>?
get() = friendTaskName?.let { project.tasks.findByName(it) } as? AbstractKotlinCompile<T>
/** Classes directories that are not produced by this task but should be consumed by
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.builder
import org.jetbrains.kotlin.gradle.model.KotlinProject
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class KotlinModelBuilderTest {
@Test
fun testCanBuild() {
val modelBuilder = KotlinModelBuilder("version")
assertTrue(modelBuilder.canBuild(KotlinProject::class.java.name))
assertFalse(modelBuilder.canBuild("wrongModel"))
}
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning
import org.junit.Test
class CompilerArgumentsImplTest {
@Test
@Throws(Exception::class)
fun equals() {
EqualsVerifier.forClass(CompilerArgumentsImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
}
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning
import org.junit.Test
class ExperimentalFeaturesImplTest {
@Test
@Throws(Exception::class)
fun equals() {
EqualsVerifier.forClass(ExperimentalFeaturesImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
}
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning
import org.junit.Test
class KotlinProjectImplTest {
@Test
@Throws(Exception::class)
fun equals() {
EqualsVerifier.forClass(KotlinProjectImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
}
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.model.impl
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning
import org.junit.Test
class SourceSetImplTest {
@Test
@Throws(Exception::class)
fun equals() {
EqualsVerifier.forClass(SourceSetImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
}
}