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
@@ -23,18 +23,22 @@ import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.AbstractCompile
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
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 org.jetbrains.kotlin.noarg.gradle.model.builder.SamWithReceiverModelBuilder
import javax.inject.Inject
class SamWithReceiverGradleSubplugin : Plugin<Project> {
class SamWithReceiverGradleSubplugin @Inject internal constructor(private val registry: ToolingModelBuilderRegistry) : Plugin<Project> {
companion object {
fun isEnabled(project: Project) = project.plugins.findPlugin(SamWithReceiverGradleSubplugin::class.java) != null
}
override fun apply(project: Project) {
project.extensions.create("samWithReceiver", SamWithReceiverExtension::class.java)
registry.register(SamWithReceiverModelBuilder())
}
}
@@ -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.noarg.gradle.model.builder
import org.gradle.api.Project
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.jetbrains.kotlin.gradle.model.SamWithReceiver
import org.jetbrains.kotlin.noarg.gradle.model.impl.SamWithReceiverImpl
import org.jetbrains.kotlin.samWithReceiver.gradle.SamWithReceiverExtension
/**
* [ToolingModelBuilder] for [SamWithReceiver] models.
* This model builder is registered for Kotlin Sam With Receiver sub-plugin.
*/
class SamWithReceiverModelBuilder : ToolingModelBuilder {
override fun canBuild(modelName: String): Boolean {
return modelName == SamWithReceiver::class.java.name
}
override fun buildAll(modelName: String, project: Project): Any? {
if (modelName == SamWithReceiver::class.java.name) {
val extension = project.extensions.getByType(SamWithReceiverExtension::class.java)
return SamWithReceiverImpl(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.noarg.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.SamWithReceiver
import java.io.Serializable
/**
* Implementation of the [SamWithReceiver] interface.
*/
data class SamWithReceiverImpl(
private val myName: String,
private val myAnnotations: List<String>,
private val myPresets: List<String>
) : SamWithReceiver, 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.noarg.gradle.model.builder
import org.jetbrains.kotlin.gradle.model.SamWithReceiver
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class SamWithReceiverModelBuilderTest {
@Test
fun testCanBuild() {
val modelBuilder = SamWithReceiverModelBuilder()
assertTrue(modelBuilder.canBuild(SamWithReceiver::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.noarg.gradle.model.impl
import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning
import org.junit.Test
class SamWithReceiverImplTest {
@Test
@Throws(Exception::class)
fun equals() {
EqualsVerifier.forClass(SamWithReceiverImpl::class.java).suppress(Warning.NULL_FIELDS).verify()
}
}