SamWithReceiver: Add general-purpose plugin and Gradle/Maven integrations

This commit is contained in:
Yan Zhulanow
2017-06-08 15:38:10 +03:00
committed by Yan Zhulanow
parent db3172a750
commit d07fd52c43
38 changed files with 886 additions and 21 deletions
@@ -3,6 +3,7 @@ include ':kotlin-gradle-plugin-api',
':kotlin-gradle-plugin-integration-tests',
':kotlin-allopen',
':kotlin-noarg',
':kotlin-sam-with-receiver',
':kotlin-gradle-subplugin-example'
project(':kotlin-gradle-plugin-api').projectDir = file("../kotlin-gradle-plugin-api")
@@ -10,4 +11,5 @@ project(':kotlin-gradle-plugin').projectDir = file("../kotlin-gradle-plugin")
project(':kotlin-gradle-plugin-integration-tests').projectDir = file("../kotlin-gradle-plugin-integration-tests")
project(':kotlin-allopen').projectDir = file("../kotlin-allopen")
project(':kotlin-noarg').projectDir = file("../kotlin-noarg")
project(':kotlin-sam-with-receiver').projectDir = file("../kotlin-sam-with-receiver")
project(':kotlin-gradle-subplugin-example').projectDir = file("../../examples/kotlin-gradle-subplugin-example")
@@ -163,4 +163,11 @@ class SimpleKotlinGradleIT : BaseGradleIT() {
checkClass("Test2")
}
}
@Test
fun testSamWithReceiverSimple() {
Project("samWithReceiverSimple", GRADLE_VERSION).build("build") {
assertSuccessful()
}
}
}
@@ -0,0 +1,28 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-sam-with-receiver:$kotlin_version"
}
}
apply plugin: "kotlin"
apply plugin: "kotlin-sam-with-receiver"
repositories {
mavenLocal()
}
sourceSets {
main.kotlin.srcDir 'src'
}
samWithReceiver {
annotation("lib.SamWithReceiver")
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
@@ -0,0 +1,6 @@
package lib;
@SamWithReceiver
public interface Job {
public void execute(String context, int other);
}
@@ -0,0 +1,5 @@
package lib;
public interface JobWithoutReceiver {
public void execute(String context, int other);
}
@@ -0,0 +1,6 @@
package lib;
public class Manager {
public void doJob(Job job) {}
public void doJobWithoutReceiver(JobWithoutReceiver job) {}
}
@@ -0,0 +1,3 @@
package lib;
public @interface SamWithReceiver {}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2017 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 test
import lib.*
fun test() {
val manager = Manager()
manager.doJob { other -> println(this) }
manager.doJobWithoutReceiver { context, other -> println(context) }
}
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>test-sam-with-receiver-simple</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
</plugin>
</plugins>
</pluginManagement>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>sam-with-receiver</plugin>
</compilerPlugins>
<pluginOptions>
<option>sam-with-receiver:annotation=lib.SamWithReceiver</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-sam-with-receiver</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,6 @@
package lib;
@SamWithReceiver
public interface Job {
public void execute(String context, int other);
}
@@ -0,0 +1,5 @@
package lib;
public interface JobWithoutReceiver {
public void execute(String context, int other);
}
@@ -0,0 +1,6 @@
package lib;
public class Manager {
public void doJob(Job job) {}
public void doJobWithoutReceiver(JobWithoutReceiver job) {}
}
@@ -0,0 +1,3 @@
package lib;
public @interface SamWithReceiver {}
@@ -0,0 +1,9 @@
package test
import lib.*
fun test() {
val manager = Manager()
manager.doJob { other -> println(this) }
manager.doJobWithoutReceiver { context, other -> println(context) }
}
@@ -0,0 +1,6 @@
import java.io.*;
File file = new File(basedir, "target/test-sam-with-receiver-simple-1.0-SNAPSHOT.jar");
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("Could not find generated JAR: " + file);
}
+131
View File
@@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.version>3.0.5</maven.version>
<sam.src>${basedir}/../../../plugins/sam-with-receiver/sam-with-receiver-cli/src</sam.src>
<sam.maven.plugin.src>${basedir}/src/main/kotlin</sam.maven.plugin.src>
<sam.maven.plugin.resources>${basedir}/src/main/resources</sam.maven.plugin.resources>
<sam.target-src>${basedir}/target/src/main/kotlin</sam.target-src>
<sam.target-resources>${basedir}/target/resource</sam.target-resources>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-maven-sam-with-receiver</artifactId>
<packaging>jar</packaging>
<description>"SAM with Receiver" plugin for Maven</description>
<repositories>
<repository>
<id>jetbrains-utils</id>
<url>http://repository.jetbrains.com/utils</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${sam.target-src}</sourceDirectory>
<resources>
<resource>
<directory>${sam.target-resources}</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>copy-sources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${sam.target-src}</outputDirectory>
<resources>
<resource><directory>${sam.src}</directory></resource>
<resource><directory>${sam.maven.plugin.src}</directory></resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${sam.target-resources}/META-INF</outputDirectory>
<resources>
<resource><directory>${sam.src}/META-INF</directory></resource>
<resource><directory>${sam.maven.plugin.resources}/META-INF</directory></resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${sam.target-src}</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<id>process-classes</id>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
<execution>
<id>process-test-classes</id>
<goals>
<goal>generate-test-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2017 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.test
import org.apache.maven.plugin.*
import org.apache.maven.project.*
import org.codehaus.plexus.component.annotations.*
import org.codehaus.plexus.logging.*
import org.jetbrains.kotlin.maven.*
val SAM_WITH_RECEIVER_COMPILER_PLUGIN_ID = "org.jetbrains.kotlin.samWithReceiver"
@Component(role = KotlinMavenPluginExtension::class, hint = "sam-with-receiver")
class KotlinSamWithReceiverMavenPluginExtension : KotlinMavenPluginExtension {
@Requirement
lateinit var logger: Logger
override fun getCompilerPluginId() = SAM_WITH_RECEIVER_COMPILER_PLUGIN_ID
override fun isApplicable(project: MavenProject, execution: MojoExecution) = true
override fun getPluginOptions(project: MavenProject, execution: MojoExecution): List<PluginOption> {
logger.debug("Loaded Maven plugin " + javaClass.name)
return emptyList()
}
}
@@ -0,0 +1,42 @@
apply plugin: 'kotlin'
configureJvmProject(project)
configurePublishing(project)
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
options.fork = false
}
repositories {
mavenLocal()
jcenter()
maven { url 'http://repository.jetbrains.com/utils' }
}
dependencies {
compile project(':kotlin-gradle-plugin-api')
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version"
compileOnly 'org.jetbrains.kotlin:gradle-api:1.6'
}
def originalSrc = "$kotlin_root/plugins/sam-with-receiver/sam-with-receiver-cli/src"
def targetSrc = file("$buildDir/sam-with-receiver-target-src")
task preprocessSources(type: Copy) {
from originalSrc
into targetSrc
filter { it.replaceAll('(?<!\\.)com\\.intellij', 'org.jetbrains.kotlin.com.intellij') }
}
sourceSets.main.java.srcDirs += targetSrc
compileKotlin.dependsOn preprocessSources
jar {
from(targetSrc) { include("META-INF/**") }
}
+148
View File
@@ -0,0 +1,148 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.version>3.0.4</maven.version>
<sam.src>${basedir}/../../../plugins/sam-with-receiver/sam-with-receiver-cli/src</sam.src>
<sam.gradle.plugin.src>${basedir}/src/main/kotlin</sam.gradle.plugin.src>
<sam.gradle.plugin.resources>${basedir}/src/main/resources</sam.gradle.plugin.resources>
<sam.target-src>${basedir}/target/src/main/kotlin</sam.target-src>
<sam.target-resources>${basedir}/target/resource</sam.target-resources>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-sam-with-receiver</artifactId>
<packaging>jar</packaging>
<description>All-open plugin for Gradle</description>
<repositories>
<repository>
<id>jetbrains-utils</id>
<url>http://repository.jetbrains.com/utils</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-gradle-plugin-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>gradle-api</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${sam.target-src}</sourceDirectory>
<resources>
<resource>
<directory>${sam.target-resources}</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>copy-sources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${sam.target-src}</outputDirectory>
<resources>
<resource><directory>${sam.src}</directory></resource>
<resource><directory>${sam.gradle.plugin.src}</directory></resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${sam.target-resources}/META-INF</outputDirectory>
<resources>
<resource><directory>${sam.src}/META-INF</directory></resource>
<resource><directory>${sam.gradle.plugin.resources}/META-INF</directory></resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${sam.target-src}/**</include>
</includes>
<replacements>
<replacement>
<token>(?&lt;!\.)com\.intellij</token>
<value>org.jetbrains.kotlin.com.intellij</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${sam.target-src}</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2017 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.samWithReceiver.gradle
open class SamWithReceiverExtension {
internal val myAnnotations = mutableListOf<String>()
internal val myPresets = 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,105 @@
/*
* Copyright 2010-2017 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.samWithReceiver.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.AbstractTask
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.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
class SamWithReceiverGradleSubplugin : Plugin<Project> {
companion object {
fun isEnabled(project: Project) = project.plugins.findPlugin(SamWithReceiverGradleSubplugin::class.java) != null
fun getSamWithReceiverExtension(project: Project): SamWithReceiverExtension {
return project.extensions.getByType(SamWithReceiverExtension::class.java)
}
}
fun Project.getBuildscriptArtifacts(): Set<ResolvedArtifact> =
buildscript.configurations.findByName("classpath")?.resolvedConfiguration?.resolvedArtifacts ?: emptySet()
override fun apply(project: Project) {
val samWithReceiverExtension = project.extensions.create("samWithReceiver", SamWithReceiverExtension::class.java)
project.afterEvaluate {
val fqNamesAsString = samWithReceiverExtension.myAnnotations.joinToString(",")
val presetsAsString = samWithReceiverExtension.myPresets.joinToString(",")
project.extensions.extraProperties.set("kotlinSamWithReceiverAnnotations", fqNamesAsString)
val allBuildscriptArtifacts = project.getBuildscriptArtifacts() + project.rootProject.getBuildscriptArtifacts()
val samWithReceiverCompilerPluginFile = allBuildscriptArtifacts.filter {
val id = it.moduleVersion.id
id.group == SamWithReceiverKotlinGradleSubplugin.SAM_WITH_RECEIVER_GROUP_NAME
&& id.name == SamWithReceiverKotlinGradleSubplugin.SAM_WITH_RECEIVER_ARTIFACT_NAME
}.firstOrNull()?.file?.absolutePath ?: ""
open class TaskForSamWithReceiver : AbstractTask()
project.tasks.add(project.tasks.create("samWithReceiverDataStorageTask", TaskForSamWithReceiver::class.java).apply {
isEnabled = false
description = "Supported annotations: " + fqNamesAsString +
"; Presets: $presetsAsString" +
"; Compiler plugin classpath: $samWithReceiverCompilerPluginFile"
})
}
}
}
class SamWithReceiverKotlinGradleSubplugin : KotlinGradleSubplugin<AbstractCompile> {
companion object {
val SAM_WITH_RECEIVER_GROUP_NAME = "org.jetbrains.kotlin"
val SAM_WITH_RECEIVER_ARTIFACT_NAME = "kotlin-sam-with-receiver"
private val ANNOTATION_ARG_NAME = "annotation"
private val PRESET_ARG_NAME = "preset"
}
override fun isApplicable(project: Project, task: AbstractCompile) = SamWithReceiverGradleSubplugin.isEnabled(project)
override fun apply(
project: Project,
kotlinCompile: AbstractCompile,
javaCompile: AbstractCompile,
variantData: Any?,
javaSourceSet: SourceSet?
): List<SubpluginOption> {
if (!SamWithReceiverGradleSubplugin.isEnabled(project)) return emptyList()
val samWithReceiverExtension = project.extensions.findByType(SamWithReceiverExtension::class.java) ?: return emptyList()
val options = mutableListOf<SubpluginOption>()
for (anno in samWithReceiverExtension.myAnnotations) {
options += SubpluginOption(ANNOTATION_ARG_NAME, anno)
}
for (preset in samWithReceiverExtension.myPresets) {
options += SubpluginOption(PRESET_ARG_NAME, preset)
}
return options
}
override fun getArtifactName() = "kotlin-sam-with-receiver"
override fun getGroupName() = "org.jetbrains.kotlin"
override fun getCompilerPluginId() = "org.jetbrains.kotlin.samWithReceiver"
}
@@ -0,0 +1 @@
implementation-class=org.jetbrains.kotlin.samWithReceiver.gradle.SamWithReceiverGradleSubplugin
@@ -0,0 +1 @@
implementation-class=org.jetbrains.kotlin.samWithReceiver.gradle.SamWithReceiverGradleSubplugin
@@ -0,0 +1 @@
org.jetbrains.kotlin.samWithReceiver.gradle.SamWithReceiverKotlinGradleSubplugin