Introduce tooling model for annotation based projects

Introduce Gradle models for plugins 'kotlin-allopen', 'kotlin-noarg' and
'kotlin-sam-with-receiver'. Corresponding model builders are registered
in each one of those plugin main classes.
This commit is contained in:
Lucas Smaira
2018-06-05 17:46:35 +01:00
committed by Sergey Igushkin
parent 5671dbb929
commit 5a423e8dcd
25 changed files with 631 additions and 5 deletions
@@ -22,12 +22,15 @@ import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.internal.AbstractTask
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.allopen.gradle.model.builder.AllOpenModelBuilder
import org.jetbrains.kotlin.gradle.plugin.JetBrainsSubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import javax.inject.Inject
class AllOpenGradleSubplugin : Plugin<Project> {
class AllOpenGradleSubplugin @Inject internal constructor(private val registry: ToolingModelBuilderRegistry) : Plugin<Project> {
companion object {
fun isEnabled(project: Project) = project.plugins.findPlugin(AllOpenGradleSubplugin::class.java) != null
@@ -38,6 +41,7 @@ class AllOpenGradleSubplugin : Plugin<Project> {
override fun apply(project: Project) {
project.extensions.create("allOpen", AllOpenExtension::class.java)
registry.register(AllOpenModelBuilder())
}
}
@@ -0,0 +1,31 @@
/*
* 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.allopen.gradle.model.builder
import org.gradle.api.Project
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension
import org.jetbrains.kotlin.allopen.gradle.model.impl.AllOpenImpl
import org.jetbrains.kotlin.gradle.model.AllOpen
/**
* [ToolingModelBuilder] for [AllOpen] models.
* This model builder is registered for Kotlin All Open sub-plugin.
*/
class AllOpenModelBuilder : ToolingModelBuilder {
override fun canBuild(modelName: String): Boolean {
return modelName == AllOpen::class.java.name
}
override fun buildAll(modelName: String, project: Project): Any? {
if (modelName == AllOpen::class.java.name) {
val extension = project.extensions.getByType(AllOpenExtension::class.java)
return AllOpenImpl(project.name, extension.myAnnotations, extension.myPresets)
}
return null
}
}
@@ -0,0 +1,39 @@
/*
* 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.allopen.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.AllOpen
import java.io.Serializable
/**
* Implementation of the [AllOpen] interface.
*/
data class AllOpenImpl(
private val myName: String,
private val myAnnotations: List<String>,
private val myPresets: List<String>
) : AllOpen, Serializable {
override fun getModelVersion(): Long {
return serialVersionUID
}
override fun getName(): String {
return myName
}
override fun getAnnotations(): List<String> {
return myAnnotations
}
override fun getPresets(): List<String> {
return myPresets
}
companion object {
private const val serialVersionUID = 1L
}
}
@@ -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.allopen.gradle.model.builder
import org.jetbrains.kotlin.gradle.model.AllOpen
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class AllOpenModelBuilderTest {
@Test
fun testCanBuild() {
val modelBuilder = AllOpenModelBuilder()
assertTrue(modelBuilder.canBuild(AllOpen::class.java.name))
assertFalse(modelBuilder.canBuild("wrongModel"))
}
}
@@ -0,0 +1,19 @@
/*
* 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.allopen.gradle.model.impl
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning
import org.junit.Test
class AllOpenImplTest {
@Test
@Throws(Exception::class)
fun equals() {
EqualsVerifier.forClass(AllOpenImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
}
}