Kotlin Gradle subplugin support

This commit is contained in:
Yan Zhulanow
2014-11-14 16:19:13 +03:00
parent 781c565ec9
commit fbed5426e2
12 changed files with 329 additions and 48 deletions
+4
View File
@@ -568,6 +568,10 @@
public protected *;
}
-keepclassmembers class com.intellij.openapi.vfs.VirtualFile {
public InputStream getInputStream();
}
-keep class jet.** {
public protected *;
}
+1
View File
@@ -84,6 +84,7 @@
<module>tools/kotlin-js-library</module>
<module>tools/kotlin-gradle-plugin</module>
<module>tools/kotlin-gradle-plugin-core</module>
<module>tools/kotlin-gradle-plugin-api</module>
<module>tools/kotlin-android-compiler-plugin</module>
<module>tools/kotlin-maven-plugin</module>
<module>tools/kotlin-maven-plugin-test</module>
@@ -17,7 +17,7 @@
</parent>
<artifactId>kotlin-android-compiler-plugin</artifactId>
<packaging>pom</packaging>
<packaging>jar</packaging>
<description>Android compiler plugin for Kotlin</description>
@@ -26,40 +26,95 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-gradle-plugin-core</artifactId>
<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>
<dependency>
<groupId>com.android.tools.build</groupId>
<artifactId>gradle</artifactId>
<version>0.4.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<configuration>
<annotationPaths>
<annotationPath>${basedir}/kotlinAnnotation</annotationPath>
</annotationPaths>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<id>prepare</id>
<phase>compile</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${kotlin-sdk}/lib/android-compiler-plugin.jar</file>
<type>jar</type>
</artifact>
</artifacts>
<tasks>
<unzip src="${kotlin-sdk}/lib/android-compiler-plugin.jar" dest="target/classes/" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jetbrains-utils</id>
<url>http://repository.jetbrains.com/utils</url>
</repository>
</repositories>
</project>
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2014 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.android
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin
import org.gradle.api
import com.android.build.gradle.BaseExtension
import java.io.File
import org.gradle.api.tasks.compile.AbstractCompile
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
public class AndroidSubplugin : KotlinGradleSubplugin {
override fun getExtraArguments(project: api.Project, task: AbstractCompile): List<SubpluginOption>? {
val androidExtension = project.getExtensions().getByName("android") as? BaseExtension
if (androidExtension == null) return null
val sourceSets = androidExtension.getSourceSets()
val mainSourceSet = sourceSets.getByName("main")
val resourceDir = mainSourceSet.getRes().getSrcDirs().firstOrNull()
val manifestFile = mainSourceSet.getManifest().getSrcFile()
if (resourceDir != null) {
val layoutDir = File(resourceDir, "layout")
task.source(layoutDir)
return listOf(
SubpluginOption("androidRes", layoutDir.getAbsolutePath()),
SubpluginOption("androidManifest", manifestFile.getAbsolutePath())
)
}
return null
}
override fun getPluginName(): String {
return "org.jetbrains.kotlin.android"
}
override fun getGroupName(): String {
return "org.jetbrains.kotlin"
}
override fun getArtifactName(): String {
return "kotlin-android-compiler-plugin"
}
}
@@ -0,0 +1,81 @@
<?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-plugin-anno.version>1.4.1</maven-plugin-anno.version>
<maven.version>3.0.4</maven.version>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-gradle-plugin-api</artifactId>
<packaging>jar</packaging>
<description>Gradle plugin API for Kotlin</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>gradle-api</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.android.tools.build</groupId>
<artifactId>gradle</artifactId>
<version>0.4.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<configuration>
<annotationPaths>
<annotationPath>${basedir}/kotlinAnnotation</annotationPath>
</annotationPaths>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2014 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.gradle.plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.AbstractCompile
public class SubpluginOption(val key: String, val value: String)
public trait KotlinGradleSubplugin {
public fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption>?
public fun getPluginName(): String
public fun getGroupName(): String
public fun getArtifactName(): String
}
@@ -25,6 +25,12 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-gradle-plugin-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency> <!-- required in runtime -->
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
@@ -46,8 +46,8 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
private val logger = Logging.getLogger(this.javaClass)
override fun getLogger() = logger
public var resPath: String = ""
public var manifestPath: String = ""
var compilerPluginClasspaths: Array<String> = array()
var compilerPluginArguments: Array<String> = array()
// override setSource to track source directory sets
override fun setSource(source: Any?) {
@@ -97,10 +97,14 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
afterCompileHook(args)
}
protected fun isJava(it: File): Boolean = it.extension.equalsIgnoreCase(JavaFileType.INSTANCE.getDefaultExtension())
protected fun isKotlin(it: File): Boolean = it.extension.equalsIgnoreCase(JetFileType.INSTANCE.getDefaultExtension())
private fun getKotlinSources(): List<File> = getSource().filter { it.isKotlinFile() }
private fun getKotlinSources(): List<File> = getSource().filter{ isKotlin(it) }
private fun File.isKotlinFile(): Boolean {
return when (FilenameUtils.getExtension(getName()).toLowerCase()) {
"kt", "kts" -> true
else -> false
}
}
private fun populateCommonArgs(args: T, sources: List<File>) {
args.freeArgs = sources.map { it.getAbsolutePath() }
@@ -131,8 +135,8 @@ public open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments
val srcDirsSources = HashSet<SourceDirectorySet>()
override fun populateTargetSpecificArgs(args: K2JVMCompilerArguments) {
args.pluginClasspaths = kotlinOptions.pluginClasspaths
args.pluginOptions = kotlinOptions.pluginOptions
args.pluginClasspaths = compilerPluginClasspaths
args.pluginOptions = compilerPluginArguments
if (StringUtils.isEmpty(kotlinOptions.classpath)) {
val existingClasspathEntries = getClasspath().filter({ it != null && it.exists() })
@@ -161,11 +165,13 @@ public open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments
private fun getJavaSourceRoots(): Set<File> =
getSource()
.filter { isJava(it) }
.filter { it.isJavaFile() }
.map { findSrcDirRoot(it) }
.filterNotNull()
.toSet()
private fun File.isJavaFile() = extension.equalsIgnoreCase(JavaFileType.INSTANCE.getDefaultExtension())
override fun afterCompileHook(args: K2JVMCompilerArguments) {
getLogger().debug("Copying resulting files to classes")
@@ -49,6 +49,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-gradle-plugin-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
@@ -39,6 +39,8 @@ import groovy.lang.Closure
import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.android.AndroidCommandLineProcessor
import java.util.ServiceLoader
import org.jetbrains.kotlin.compiler.plugin.getPluginOptionString
val DEFAULT_ANNOTATIONS = "org.jebrains.kotlin.gradle.defaultAnnotations"
@@ -144,6 +146,8 @@ class Kotlin2JvmSourceSetProcessor(
}
}
}
loadSubplugins(project).addSubpluginArguments(project, kotlinTask)
}
}
@@ -304,10 +308,6 @@ open class KotlinAndroidPlugin [Inject] (val scriptHandler: ScriptHandler, val t
project.getExtensions().add(DEFAULT_ANNOTATIONS, GradleUtils(scriptHandler, project).resolveKotlinPluginDependency("kotlin-android-sdk-annotations"))
}
private fun makePluginOption(option: CliOption, value: String): String {
return "plugin:${AndroidCommandLineProcessor.ANDROID_COMPILER_PLUGIN_ID}:${option.name}=$value"
}
private fun processVariants(variants: DefaultDomainObjectSet<out BaseVariant>, project: Project, androidExt: BaseExtension): Unit {
val logger = project.getLogger()
val kotlinOptions = getExtension<Any?>(androidExt, "kotlinOptions")
@@ -320,6 +320,8 @@ open class KotlinAndroidPlugin [Inject] (val scriptHandler: ScriptHandler, val t
sourceSets.getByName("androidTest")
}
val subpluginEnvironment = loadSubplugins(project)
for (variant in variants) {
if (variant is LibraryVariant || variant is ApkVariant) {
val buildTypeSourceSetName = AndroidGradleWrapper.getVariantName(variant)
@@ -330,17 +332,6 @@ open class KotlinAndroidPlugin [Inject] (val scriptHandler: ScriptHandler, val t
val javaTask = variant.getJavaCompile()!!
val variantName = variant.getName()
val resourceDir = AndroidGradleWrapper.getResourceDirs(mainSourceSet).firstOrNull()
val manifestFile = AndroidGradleWrapper.getManifestFile(mainSourceSet)
if (resourceDir != null) {
val layoutDir = File(resourceDir, "layout")
kotlinOptions.pluginOptions = array(
makePluginOption("androidRes", layoutDir.getAbsolutePath()),
makePluginOption("androidManifest", manifestFile.getAbsolutePath())
)
}
val kotlinTaskName = "compile${variantName.capitalize()}Kotlin"
val kotlinTask = tasksProvider.createKotlinJVMTask(project, kotlinTaskName)
if (kotlinOptions != null) {
@@ -400,6 +391,8 @@ open class KotlinAndroidPlugin [Inject] (val scriptHandler: ScriptHandler, val t
}
}
subpluginEnvironment.addSubpluginArguments(project, kotlinTask)
kotlinTask doFirst {
var plugin = project.getPlugins().findPlugin("android")
if (null == plugin) {
@@ -429,9 +422,57 @@ open class KotlinAndroidPlugin [Inject] (val scriptHandler: ScriptHandler, val t
val result = (obj as HasConvention).getConvention().getPlugins()[extensionName]
return result as T
}
}
private fun loadSubplugins(project: Project): SubpluginEnvironment {
try {
val subplugins = ServiceLoader.load(
javaClass<KotlinGradleSubplugin>(), project.getBuildscript().getClassLoader()).toList()
val subpluginDependencyNames =
subplugins.mapTo(hashSetOf<String>()) { it.getGroupName() + ":" + it.getArtifactName() }
val classpath = project.getBuildscript().getConfigurations().getByName("classpath")
val subpluginClasspaths = hashMapOf<KotlinGradleSubplugin, List<String>>()
for (subplugin in subplugins) {
val files = classpath.getDependencies()
.filter { subpluginDependencyNames.contains(it.getGroup() + ":" + it.getName()) }
.flatMap { classpath.files(it).map { it.getAbsolutePath() } }
subpluginClasspaths.put(subplugin, files)
}
return SubpluginEnvironment(subpluginClasspaths, subplugins)
} catch (e: NoClassDefFoundError) {
// Skip plugin loading if KotlinGradleSubplugin is not defined.
// It is true now for tests in kotlin-gradle-plugin-core.
return SubpluginEnvironment(mapOf(), listOf())
}
}
private class SubpluginEnvironment(
val subpluginClasspaths: Map<KotlinGradleSubplugin, List<String>>,
val subplugins: List<KotlinGradleSubplugin>
) {
fun addSubpluginArguments(project: Project, compileTask: KotlinCompile) {
val realPluginClasspaths = arrayListOf<String>()
val pluginArguments = arrayListOf<String>()
subplugins.forEach { subplugin ->
val args = subplugin.getExtraArguments(project, compileTask)
if (args != null) {
realPluginClasspaths.addAll(subpluginClasspaths[subplugin])
for (arg in args) {
val option = getPluginOptionString(subplugin.getPluginName(), arg.key, arg.value)
pluginArguments.add(option)
}
}
}
compileTask.compilerPluginClasspaths = realPluginClasspaths.copyToArray()
compileTask.compilerPluginArguments = pluginArguments.copyToArray()
}
}
open class GradleUtils(val scriptHandler: ScriptHandler, val project: ProjectInternal) {
public fun resolveDependencies(vararg coordinates: String): Collection<File> {
@@ -46,16 +46,6 @@ class AndroidGradleWrapper {
return androidSourceSet.getJava().getSrcDirs()
}
@NotNull
static def Set<File> getResourceDirs(Object androidSourceSet) {
return androidSourceSet.getRes().getSrcDirs()
}
@NotNull
static def File getManifestFile(Object androidSourceSet) {
return androidSourceSet.getManifest().getSrcFile()
}
@NotNull
static def List<String> getProductFlavorsNames(ApkVariant variant) {
return variant.getProductFlavors().iterator().collect { it.getName() }