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
@@ -63,6 +63,9 @@ public interface KotlinPaths {
@NotNull
File getNoArgPluginJarPath();
@NotNull
File getSamWithReceiverJarPath();
@NotNull
File getCompilerPath();
@@ -106,6 +106,12 @@ public class KotlinPathsFromHomeDir implements KotlinPaths {
return getLibraryFile(PathUtil.NOARG_PLUGIN_JAR_NAME);
}
@NotNull
@Override
public File getSamWithReceiverJarPath() {
return getLibraryFile(PathUtil.SAM_WITH_RECEIVER_PLUGIN_JAR_NAME);
}
@NotNull
@Override
public File getCompilerPath() {
@@ -33,6 +33,7 @@ public class PathUtil {
public static final String JS_LIB_10_JAR_NAME = "kotlin-jslib.jar";
public static final String ALLOPEN_PLUGIN_JAR_NAME = "allopen-compiler-plugin.jar";
public static final String NOARG_PLUGIN_JAR_NAME = "noarg-compiler-plugin.jar";
public static final String SAM_WITH_RECEIVER_PLUGIN_JAR_NAME = "sam-with-receiver-compiler-plugin.jar";
public static final String JS_LIB_SRC_JAR_NAME = "kotlin-stdlib-js-sources.jar";
public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar";
public static final String KOTLIN_JAVA_RUNTIME_JRE7_JAR = "kotlin-stdlib-jre7.jar";
+1
View File
@@ -40,6 +40,7 @@
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<gradleProjectImportHandler implementation="org.jetbrains.kotlin.allopen.ide.AllOpenGradleProjectImportHandler"/>
<gradleProjectImportHandler implementation="org.jetbrains.kotlin.noarg.ide.NoArgGradleProjectImportHandler"/>
<gradleProjectImportHandler implementation="org.jetbrains.kotlin.samWithReceiver.ide.SamWithReceiverGradleProjectImportHandler"/>
<projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleModuleConfigurator"/>
<projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinJsGradleModuleConfigurator"/>
+1
View File
@@ -4,6 +4,7 @@
<projectConfigurator implementation="org.jetbrains.kotlin.idea.maven.configuration.KotlinJavascriptMavenConfigurator"/>
<mavenProjectImportHandler implementation="org.jetbrains.kotlin.allopen.ide.AllOpenMavenProjectImportHandler"/>
<mavenProjectImportHandler implementation="org.jetbrains.kotlin.noarg.ide.NoArgMavenProjectImportHandler"/>
<mavenProjectImportHandler implementation="org.jetbrains.kotlin.samWithReceiver.ide.SamWithReceiverMavenProjectImportHandler"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains.idea.maven">
+2
View File
@@ -93,11 +93,13 @@
<module>tools/kotlin-annotation-processing-maven</module>
<module>tools/kotlin-maven-allopen</module>
<module>tools/kotlin-maven-noarg</module>
<module>tools/kotlin-maven-sam-with-receiver</module>
<module>tools/kotlin-sam-with-receiver-compiler-plugin</module>
<module>tools/kotlin-source-sections-compiler-plugin</module>
<module>tools/kotlin-script-util</module>
<module>tools/kotlin-android-extensions</module>
<module>tools/kotlin-android-extensions-runtime</module>
<module>tools/kotlin-maven-plugin-test</module>
<module>examples/annotation-processor-example</module>
@@ -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
@@ -25,10 +25,10 @@ import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import org.jetbrains.kotlin.allopen.AbstractAllOpenDeclarationAttributeAltererExtension
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.PLUGIN_ID
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.ANNOTATION_OPTION
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
import java.util.*
class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) : AbstractAllOpenDeclarationAttributeAltererExtension() {
@@ -48,14 +48,7 @@ class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) : Abs
if (modifierListOwner == null) return emptyList()
val module = ModuleUtilCore.findModuleForPsiElement(modifierListOwner) ?: return emptyList()
return cache.value.getOrPut(module) {
val kotlinFacet = KotlinFacet.get(module) ?: return@getOrPut emptyList()
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return@getOrPut emptyList()
commonArgs.pluginOptions?.filter { it.startsWith(ANNOTATION_OPTION_PREFIX) }
?.map { it.substring(ANNOTATION_OPTION_PREFIX.length) }
?: emptyList()
}
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
}
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
@@ -16,10 +16,21 @@
package org.jetbrains.kotlin.annotation.plugin.ide
import com.intellij.openapi.module.Module
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import java.io.File
fun Module.getSpecialAnnotations(prefix: String): List<String> {
val kotlinFacet = org.jetbrains.kotlin.idea.facet.KotlinFacet.get(this) ?: return emptyList()
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return emptyList()
return commonArgs.pluginOptions
?.filter { it.startsWith(prefix) }
?.map { it.substring(prefix.length) }
?: emptyList()
}
internal class AnnotationBasedCompilerPluginSetup(val annotationFqNames: List<String>, val classpath: List<String>)
internal fun modifyCompilerArgumentsForPlugin(
@@ -23,7 +23,7 @@ import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import org.jetbrains.kotlin.idea.facet.KotlinFacet
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.ANNOTATION_OPTION
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.PLUGIN_ID
import org.jetbrains.kotlin.noarg.diagnostic.AbstractNoArgDeclarationChecker
@@ -43,15 +43,7 @@ class IdeNoArgDeclarationChecker(val project: Project) : AbstractNoArgDeclaratio
if (modifierListOwner == null) return emptyList()
val module = ModuleUtilCore.findModuleForPsiElement(modifierListOwner) ?: return emptyList()
return cache.value.getOrPut(module) {
val kotlinFacet = KotlinFacet.get(module) ?: return@getOrPut emptyList()
val commonArgs = kotlinFacet.configuration.settings.compilerArguments ?: return@getOrPut emptyList()
commonArgs.pluginOptions
?.filter { it.startsWith(ANNOTATION_OPTION_PREFIX) }
?.map { it.substring(ANNOTATION_OPTION_PREFIX.length) }
?: emptyList()
}
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
}
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
@@ -29,17 +29,26 @@ import org.jetbrains.kotlin.container.get
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverConfigurationKeys.ANNOTATION
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverConfigurationKeys.PRESET
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor.Companion.SUPPORTED_PRESETS
object SamWithReceiverConfigurationKeys {
val ANNOTATION: CompilerConfigurationKey<List<String>> =
CompilerConfigurationKey.create("annotation qualified name")
val PRESET: CompilerConfigurationKey<List<String>> = CompilerConfigurationKey.create("annotation preset")
}
class SamWithReceiverCommandLineProcessor : CommandLineProcessor {
companion object {
val SUPPORTED_PRESETS = emptyMap<String, List<String>>()
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified names",
required = false, allowMultipleOccurrences = true)
val PRESET_OPTION = CliOption("preset", "<name>", "Preset name (${SUPPORTED_PRESETS.keys.joinToString()})",
required = false, allowMultipleOccurrences = true)
val PLUGIN_ID = "org.jetbrains.kotlin.samWithReceiver"
}
@@ -48,13 +57,17 @@ class SamWithReceiverCommandLineProcessor : CommandLineProcessor {
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value)
PRESET_OPTION -> configuration.appendList(PRESET, value)
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
}
}
class SamWithReceiverComponentRegistrar : ComponentRegistrar {
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
val annotations = configuration.get(SamWithReceiverConfigurationKeys.ANNOTATION) ?: return
val annotations = configuration.get(ANNOTATION)?.toMutableList() ?: mutableListOf()
configuration.get(PRESET)?.forEach { preset ->
SUPPORTED_PRESETS[preset]?.let { annotations += it }
}
if (annotations.isEmpty()) return
StorageComponentContainerContributor.registerExtension(project, CliSamWithReceiverComponentContributor(annotations))
@@ -8,9 +8,14 @@
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="plugin-api" />
<orderEntry type="module" module-name="sam-with-receiver-cli" />
<orderEntry type="module" module-name="frontend" />
<orderEntry type="module" module-name="idea" />
<orderEntry type="module" module-name="idea-maven" />
<orderEntry type="module" module-name="idea-analysis" />
<orderEntry type="module" module-name="frontend.java" />
<orderEntry type="module" module-name="annotation-based-compiler-plugins-ide-support" />
<orderEntry type="module" module-name="util" />
</component>
</module>
@@ -16,20 +16,43 @@
package org.jetbrains.kotlin.samWithReceiver.ide
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootModificationTracker
import com.intellij.psi.util.CachedValueProvider.*
import com.intellij.psi.util.CachedValuesManager
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.container.get
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
import org.jetbrains.kotlin.idea.caches.resolve.ModuleProductionSourceInfo
import org.jetbrains.kotlin.idea.caches.resolve.ScriptDependenciesModuleInfo
import org.jetbrains.kotlin.idea.caches.resolve.ScriptModuleInfo
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverResolverExtension
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor.Companion.PLUGIN_ID
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor.Companion.ANNOTATION_OPTION
import java.util.*
class IdeSamWithReceiverComponentContributor(val project: Project) : StorageComponentContainerContributor {
private companion object {
val ANNOTATION_OPTION_PREFIX = "plugin:$PLUGIN_ID:${ANNOTATION_OPTION.name}="
}
private val cache = CachedValuesManager.getManager(project).createCachedValue({
Result.create(WeakHashMap<Module, List<String>>(), ProjectRootModificationTracker.getInstance(project))
}, /* trackValue = */ false)
private fun getAnnotationsForModule(module: Module): List<String> {
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
}
class IdeSamWithReceiverComponentContributor : StorageComponentContainerContributor {
override fun onContainerComposed(container: ComponentProvider, moduleInfo: ModuleInfo?) {
val annotations = when (moduleInfo) {
is ScriptModuleInfo -> moduleInfo.scriptDefinition.annotationsForSamWithReceivers
is ScriptDependenciesModuleInfo -> moduleInfo.scriptModuleInfo?.scriptDefinition?.annotationsForSamWithReceivers
is ModuleProductionSourceInfo -> getAnnotationsForModule(moduleInfo.module)
else -> null
} ?: return
@@ -0,0 +1,39 @@
/*
* 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.ide
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractGradleImportHandler
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor
import org.jetbrains.kotlin.utils.PathUtil
class SamWithReceiverGradleProjectImportHandler : AbstractGradleImportHandler() {
override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID
override val pluginName = "sam-with-receiver"
override val annotationOptionName = SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name
override val dataStorageTaskName = "samWithReceiverDataStorageTask"
override val pluginJarFileFromIdea = PathUtil.getKotlinPathsForIdeaPlugin().allOpenPluginJarPath
override fun getAnnotationsForPreset(presetName: String): List<String> {
for ((name, annotations) in SamWithReceiverCommandLineProcessor.SUPPORTED_PRESETS.entries) {
if (presetName == name) {
return annotations
}
}
return super.getAnnotationsForPreset(presetName)
}
}
@@ -0,0 +1,54 @@
/*
* 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.ide
import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverCommandLineProcessor
import org.jetbrains.kotlin.utils.PathUtil
class SamWithReceiverMavenProjectImportHandler : AbstractMavenImportHandler() {
private companion object {
val ANNOTATION_PARAMETER_PREFIX = "sam-with-receiver:${SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name}="
}
override val compilerPluginId = SamWithReceiverCommandLineProcessor.PLUGIN_ID
override val pluginName = "samWithReceiver"
override val annotationOptionName = SamWithReceiverCommandLineProcessor.ANNOTATION_OPTION.name
override val mavenPluginArtifactName = "kotlin-maven-sam-with-receiver"
override val pluginJarFileFromIdea = PathUtil.getKotlinPathsForIdeaPlugin().allOpenPluginJarPath
override fun getAnnotations(enabledCompilerPlugins: List<String>, compilerPluginOptions: List<String>): List<String>? {
if ("sam-with-receiver" !in enabledCompilerPlugins) {
return null
}
val annotations = mutableListOf<String>()
for ((presetName, presetAnnotations) in SamWithReceiverCommandLineProcessor.SUPPORTED_PRESETS) {
if (presetName in enabledCompilerPlugins) {
annotations.addAll(presetAnnotations)
}
}
annotations.addAll(compilerPluginOptions.mapNotNull { text ->
if (!text.startsWith(ANNOTATION_PARAMETER_PREFIX)) return@mapNotNull null
text.substring(ANNOTATION_PARAMETER_PREFIX.length)
})
return annotations
}
}