From be26246a3eb7d65fe3981290a0d8659f4b7b93bd Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Wed, 5 Oct 2016 21:56:50 +0300 Subject: [PATCH] Noarg: Add a kotlin-noarg Gradle plugin --- libraries/pom.xml | 1 + .../kotlin/gradle/SimpleKotlinGradleIT.kt | 13 ++ .../testProject/noArgJpa/build.gradle | 24 +++ .../src/javax/persistence/Entity.java | 47 ++++++ .../testProject/noArgJpa/src/test.kt | 4 + libraries/tools/kotlin-noarg/pom.xml | 148 ++++++++++++++++++ .../kotlin/noarg/gradle/KotlinJpaSubplugin.kt | 28 ++++ .../kotlin/noarg/gradle/NoArgExtension.kt | 33 ++++ .../kotlin/noarg/gradle/NoArgSubplugin.kt | 86 ++++++++++ .../gradle-plugins/kotlin-jpa.properties | 1 + .../gradle-plugins/kotlin-noarg.properties | 1 + ...kotlin.gradle.plugin.KotlinGradleSubplugin | 1 + 12 files changed, 387 insertions(+) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/build.gradle create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/javax/persistence/Entity.java create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/test.kt create mode 100755 libraries/tools/kotlin-noarg/pom.xml create mode 100644 libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/KotlinJpaSubplugin.kt create mode 100644 libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgExtension.kt create mode 100644 libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgSubplugin.kt create mode 100644 libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-jpa.properties create mode 100644 libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-noarg.properties create mode 100644 libraries/tools/kotlin-noarg/src/main/resources/META-INF/services/org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin diff --git a/libraries/pom.xml b/libraries/pom.xml index c6d40d23937..0a6b58e90b6 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -102,6 +102,7 @@ tools/kotlin-annotation-processing tools/kotlin-allopen tools/kotlin-maven-allopen + tools/kotlin-noarg tools/kotlin-gradle-plugin tools/kotlin-gradle-plugin-api diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SimpleKotlinGradleIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SimpleKotlinGradleIT.kt index 87ae90d1567..c4ad45dd8e0 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SimpleKotlinGradleIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/SimpleKotlinGradleIT.kt @@ -144,4 +144,17 @@ class SimpleKotlinGradleIT : BaseGradleIT() { "public final method()V") } } + + @Test + fun testKotlinJpaPlugin() { + Project("noArgJpa", GRADLE_VERSION).build("build") { + assertSuccessful() + + val classesDir = File(project.projectDir, "build/classes/main") + val testClass = File(classesDir, "test/Test.class") + assertTrue(testClass.exists()) + + checkBytecodeContains(testClass, "public ()V") + } + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/build.gradle b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/build.gradle new file mode 100644 index 00000000000..b21e86726f2 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/build.gradle @@ -0,0 +1,24 @@ +buildscript { + repositories { + mavenLocal() + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" + } +} + +apply plugin: "kotlin" +apply plugin: "kotlin-jpa" + +repositories { + mavenLocal() +} + +sourceSets { + main.kotlin.srcDir 'src' +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/javax/persistence/Entity.java b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/javax/persistence/Entity.java new file mode 100644 index 00000000000..b449cda2444 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/javax/persistence/Entity.java @@ -0,0 +1,47 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * 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. + */ + +/** + * This file was copied from the javax.persistence : + * https://github.com/eclipse/javax.persistence/blob/master/src/javax/persistence/Entity.java + */ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies that the class is an entity. This annotation is applied to the + * entity class. + * + * @since Java Persistence 1.0 + */ +@Documented +@Target(TYPE) +@Retention(RUNTIME) +public @interface Entity { + + /** + * (Optional) The entity name. Defaults to the unqualified + * name of the entity class. This name is used to refer to the + * entity in queries. The name must not be a reserved literal + * in the Java Persistence query language. + */ + String name() default ""; +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/test.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/test.kt new file mode 100644 index 00000000000..30fe8f08957 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/noArgJpa/src/test.kt @@ -0,0 +1,4 @@ +package test + +@javax.persistence.Entity +class Test(a: String, b: Int) \ No newline at end of file diff --git a/libraries/tools/kotlin-noarg/pom.xml b/libraries/tools/kotlin-noarg/pom.xml new file mode 100755 index 00000000000..8de2402d129 --- /dev/null +++ b/libraries/tools/kotlin-noarg/pom.xml @@ -0,0 +1,148 @@ + + + + 4.0.0 + + 3.0.4 + ${basedir}/../../../plugins/noarg/noarg-cli/src + ${basedir}/src/main/kotlin + ${basedir}/src/main/resources + ${basedir}/target/src/main/kotlin + ${basedir}/target/resource + + + + org.jetbrains.kotlin + kotlin-project + 1.1-SNAPSHOT + ../../pom.xml + + + kotlin-noarg + jar + + All-open plugin for Gradle + + + + jetbrains-utils + http://repository.jetbrains.com/utils + + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${project.version} + + + org.jetbrains.kotlin + kotlin-compiler-embeddable + ${project.version} + provided + + + org.jetbrains.kotlin + kotlin-gradle-plugin-api + ${project.version} + provided + + + org.jetbrains.kotlin + gradle-api + 1.6 + provided + + + + + ${noarg.target-src} + + + ${noarg.target-resources} + + + + + + maven-resources-plugin + 3.0.0 + + + copy-sources + validate + + copy-resources + + + ${noarg.target-src} + + ${noarg.src} + ${noarg.gradle.plugin.src} + + + + + copy-resources + validate + + copy-resources + + + ${noarg.target-resources}/META-INF + + ${noarg.src}/META-INF + ${noarg.gradle.plugin.resources}/META-INF + + + + + + + com.google.code.maven-replacer-plugin + replacer + 1.5.3 + + + generate-sources + + replace + + + + + + ${noarg.target-src}/** + + + + com\.intellij + org.jetbrains.kotlin.com.intellij + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${project.version} + + + + compile + compile + compile + + + ${noarg.target-src} + + + + + + + + diff --git a/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/KotlinJpaSubplugin.kt b/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/KotlinJpaSubplugin.kt new file mode 100644 index 00000000000..ed1c143659e --- /dev/null +++ b/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/KotlinJpaSubplugin.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * 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.jetbrains.kotlin.noarg.gradle + +import org.gradle.api.Plugin +import org.gradle.api.Project + +class KotlinJpaSubplugin : Plugin { + override fun apply(project: Project) { + project.plugins.apply(NoArgGradleSubplugin::class.java) + val noArgExtension = NoArgGradleSubplugin.getNoArgExtension(project) + noArgExtension.annotation("javax.persistence.Entity") + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgExtension.kt b/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgExtension.kt new file mode 100644 index 00000000000..188aacc0d0a --- /dev/null +++ b/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgExtension.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * 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.jetbrains.kotlin.noarg.gradle + +open class NoArgExtension { + internal val myAnnotations = mutableListOf() + + open fun annotation(fqName: String) { + myAnnotations.add(fqName) + } + + open fun annotations(fqNames: List) { + myAnnotations.addAll(fqNames) + } + + open fun annotations(vararg fqNames: String) { + myAnnotations.addAll(fqNames) + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgSubplugin.kt b/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgSubplugin.kt new file mode 100644 index 00000000000..72774c8125b --- /dev/null +++ b/libraries/tools/kotlin-noarg/src/main/kotlin/org/jetbrains/kotlin/noarg/gradle/NoArgSubplugin.kt @@ -0,0 +1,86 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * 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.jetbrains.kotlin.noarg.gradle + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.internal.AbstractTask +import org.gradle.api.internal.ConventionTask +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.compile.AbstractCompile +import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin +import org.jetbrains.kotlin.gradle.plugin.SubpluginOption + +class NoArgGradleSubplugin : Plugin { + companion object { + fun isEnabled(project: Project) = project.plugins.findPlugin(NoArgGradleSubplugin::class.java) != null + + fun getNoArgExtension(project: Project): NoArgExtension { + return project.extensions.getByType(NoArgExtension::class.java) + } + } + + override fun apply(project: Project) { + val noArgExtension = project.extensions.create("noArg", NoArgExtension::class.java) + + project.afterEvaluate { + val fqNamesAsString = noArgExtension.myAnnotations.joinToString(",") + project.extensions.extraProperties.set("kotlinNoArgAnnotations", fqNamesAsString) + + open class TaskForAllOpen : AbstractTask() + project.tasks.add(project.tasks.create("noArgDataStorageTask", TaskForAllOpen::class.java).apply { + isEnabled = false + description = "Supported no-arg annotations: " + fqNamesAsString + }) + } + } +} + +class NoArgKotlinGradleSubplugin : KotlinGradleSubplugin { + companion object { + val NOARG_GROUP_NAME = "org.jetbrains.kotlin" + val NOARG_ARTIFACT_NAME = "kotlin-noarg" + + private val ANNOTATIONS_ARG_NAME = "annotation" + } + + override fun isApplicable(project: Project, task: AbstractCompile) = AllOpenGradleSubplugin.isEnabled(project) + + override fun apply( + project: Project, + kotlinCompile: AbstractCompile, + javaCompile: AbstractCompile, + variantData: Any?, + javaSourceSet: SourceSet? + ): List { + if (!NoArgGradleSubplugin.isEnabled(project)) return emptyList() + + val allOpenExtension = project.extensions.findByType(AllOpenExtension::class.java) ?: return emptyList() + + val options = mutableListOf() + + for (anno in allOpenExtension.myAnnotations) { + options += SubpluginOption(ANNOTATIONS_ARG_NAME, anno) + } + + return options + } + + override fun getArtifactName() = "kotlin-noarg" + override fun getGroupName() = "org.jetbrains.kotlin" + override fun getPluginName() = "org.jetbrains.kotlin.noarg" +} \ No newline at end of file diff --git a/libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-jpa.properties b/libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-jpa.properties new file mode 100644 index 00000000000..197f43c9cf8 --- /dev/null +++ b/libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-jpa.properties @@ -0,0 +1 @@ +implementation-class=org.jetbrains.kotlin.noarg.gradle.KotlinJpaSubplugin \ No newline at end of file diff --git a/libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-noarg.properties b/libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-noarg.properties new file mode 100644 index 00000000000..4da171e05df --- /dev/null +++ b/libraries/tools/kotlin-noarg/src/main/resources/META-INF/gradle-plugins/kotlin-noarg.properties @@ -0,0 +1 @@ +implementation-class=org.jetbrains.kotlin.noarg.gradle.NoArgGradleSubplugin \ No newline at end of file diff --git a/libraries/tools/kotlin-noarg/src/main/resources/META-INF/services/org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin b/libraries/tools/kotlin-noarg/src/main/resources/META-INF/services/org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin new file mode 100644 index 00000000000..83e1aa6f349 --- /dev/null +++ b/libraries/tools/kotlin-noarg/src/main/resources/META-INF/services/org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin @@ -0,0 +1 @@ +org.jetbrains.kotlin.noarg.gradle.NoArgKotlinGradleSubplugin \ No newline at end of file