[Assign plugin] Add Gradle plugin and IDE dependencies for Assignment compiler plugin

This commit is contained in:
Anže Sodja
2022-09-01 15:58:08 +02:00
committed by Dmitriy Novozhilov
parent 09d6dfc8bf
commit 17cd0d7dbc
22 changed files with 361 additions and 1 deletions
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.assignment.plugin.gradle
open class AssignmentExtension {
internal val myAnnotations = mutableListOf<String>()
open fun annotation(fqName: String) {
myAnnotations.add(fqName)
}
open fun annotations(fqNames: List<String>) {
myAnnotations.addAll(fqNames)
}
open fun annotations(vararg fqNames: String) {
myAnnotations.addAll(fqNames)
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.assignment.plugin.gradle
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.assignment.plugin.gradle.model.builder.AssignmentModelBuilder
import org.jetbrains.kotlin.gradle.plugin.*
import javax.inject.Inject
class AssignmentSubplugin
@Inject internal constructor(
private val registry: ToolingModelBuilderRegistry
) : KotlinCompilerPluginSupportPlugin {
companion object {
const val COMPILER_PLUGIN_ARTIFACT_NAME = "kotlin-assignment"
const val COMPILER_PLUGIN_ID = "org.jetbrains.kotlin.assignment"
private const val ANNOTATION_ARG_NAME = "annotation"
}
override fun apply(target: Project) {
target.extensions.create("assignment", AssignmentExtension::class.java)
registry.register(AssignmentModelBuilder())
}
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true
override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider<List<SubpluginOption>> {
val project = kotlinCompilation.target.project
val extension = project.extensions.findByType(AssignmentExtension::class.java)
?: return project.provider { emptyList() }
return project.provider {
val options = mutableListOf<SubpluginOption>()
for (anno in extension.myAnnotations) {
options += SubpluginOption(ANNOTATION_ARG_NAME, anno)
}
options
}
}
override fun getCompilerPluginId(): String = COMPILER_PLUGIN_ID
override fun getPluginArtifact(): SubpluginArtifact = JetBrainsSubpluginArtifact(artifactId = COMPILER_PLUGIN_ARTIFACT_NAME)
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.assignment.plugin.gradle.model.builder
import org.gradle.api.Project
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.jetbrains.kotlin.assignment.plugin.gradle.AssignmentExtension
import org.jetbrains.kotlin.assignment.plugin.gradle.model.impl.AssignmentImpl
import org.jetbrains.kotlin.gradle.model.Assignment
/**
* [ToolingModelBuilder] for [ValueContainerAssignment] models.
* This model builder is registered for Kotlin Value Container Assignment sub-plugin.
*/
class AssignmentModelBuilder : ToolingModelBuilder {
override fun canBuild(modelName: String): Boolean {
return modelName.equals(Assignment::class.java.name)
}
override fun buildAll(modelName: String, project: Project): Any {
require(canBuild(modelName)) { "buildAll(\"$modelName\") has been called while canBeBuild is false" }
val extension = project.extensions.getByType(AssignmentExtension::class.java)
return AssignmentImpl(project.name, extension.myAnnotations)
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.assignment.plugin.gradle.model.impl
import org.jetbrains.kotlin.gradle.model.Assignment
import java.io.Serializable
/**
* Implementation of the [ValueContainerAssignment] interface.
*/
data class AssignmentImpl(
override val name: String,
override val annotations: List<String>
) : Assignment, Serializable {
override val modelVersion = serialVersionUID
companion object {
private const val serialVersionUID = 1L
}
}
@@ -0,0 +1 @@
implementation-class=org.jetbrains.kotlin.assignment.plugin.gradle.AssignmentSubplugin
@@ -0,0 +1 @@
org.jetbrains.kotlin.assignment.plugin.gradle.AssignmentSubplugin
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.assignment.plugin.gradle.model.builder
import org.jetbrains.kotlin.gradle.model.Assignment
import org.junit.Assert
import org.junit.Test
class AssignmentModelBuilderTest {
@Test
fun testCanBuild() {
val modelBuilder = AssignmentModelBuilder()
Assert.assertTrue(modelBuilder.canBuild(Assignment::class.java.name))
Assert.assertFalse(modelBuilder.canBuild("wrongModel"))
}
}