Skip to content

Commit

Permalink
Move Android Extensions subplugin to the main kotlin-gradle-plugin ar…
Browse files Browse the repository at this point in the history
…tifact
  • Loading branch information
yanex committed Jan 26, 2016
1 parent d949301 commit 26ed1c3
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 64 deletions.
Expand Up @@ -23,7 +23,11 @@ import org.jetbrains.kotlin.gradle.plugin.SubpluginOption

public class ExampleSubplugin : KotlinGradleSubplugin {

override fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption>? {
override fun isApplicable(project: Project, task: AbstractCompile): Boolean {
return true
}

override fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption> {
println("ExampleSubplugin loaded")
return listOf(SubpluginOption("exampleKey", "exampleValue"))
}
Expand Down
42 changes: 0 additions & 42 deletions libraries/tools/kotlin-android-extensions/pom.xml
Expand Up @@ -21,49 +21,7 @@

<description>Android compiler plugin for Kotlin</description>

<dependencies>
<dependency>
<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</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>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.android.tools.build</groupId>
<artifactId>gradle</artifactId>
<version>1.1.0</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>
<artifactId>kotlin-maven-plugin</artifactId>
Expand Down

This file was deleted.

Expand Up @@ -22,7 +22,8 @@ import org.gradle.api.tasks.compile.AbstractCompile
public class SubpluginOption(val key: String, val value: String)

public interface KotlinGradleSubplugin {
public fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption>?
public fun isApplicable(project: Project, task: AbstractCompile): Boolean
public fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption>
public fun getPluginName(): String
public fun getGroupName(): String
public fun getArtifactName(): String
Expand Down
5 changes: 5 additions & 0 deletions libraries/tools/kotlin-gradle-plugin/pom.xml
Expand Up @@ -25,6 +25,11 @@
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-android-extensions</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.android.tools.build</groupId>
<artifactId>gradle</artifactId>
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
* Copyright 2010-2015 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.
Expand All @@ -14,10 +14,11 @@
* limitations under the License.
*/

package org.jetbrains.kotlin.android
package org.jetbrains.kotlin.gradle.internal

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.AbstractCompile
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin
Expand All @@ -27,11 +28,33 @@ import org.w3c.dom.Document
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory

// Use apply plugin: 'kotlin-android-extensions' to enable Android Extensions in an Android project.
// Just a marker plugin.
public class AndroidExtensionsSubpluginIndicator : Plugin<Project> {
override fun apply(target: Project?) {}
}

public class AndroidSubplugin : KotlinGradleSubplugin {
private companion object {
@Volatile
var migrateWarningReported: Boolean = false
}

override fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption>? {
val androidExtension = project.extensions.getByName("android") as? BaseExtension ?: return null
override fun isApplicable(project: Project, task: AbstractCompile): Boolean {
project.extensions.getByName("android") as? BaseExtension ?: return false
if (project.plugins.findPlugin(AndroidExtensionsSubpluginIndicator::class.java) == null) {
val dependencies = project.buildscript.configurations.getByName("classpath").dependencies
if (dependencies.any { it.name == getArtifactName() && it.group == getGroupName() } && !migrateWarningReported) {
project.logger.warn("To enable Android Extensions, use: \"apply plugin: 'kotlin-android-extensions'\"")
migrateWarningReported = true
}
return false
}
return true
}

override fun getExtraArguments(project: Project, task: AbstractCompile): List<SubpluginOption> {
val androidExtension = project.extensions.getByName("android") as? BaseExtension ?: return emptyList()
val sourceSets = androidExtension.sourceSets

val pluginOptions = arrayListOf<SubpluginOption>()
Expand Down
Expand Up @@ -455,19 +455,21 @@ open class KotlinAndroidPlugin @Inject constructor(val scriptHandler: ScriptHand

private fun loadSubplugins(project: Project): SubpluginEnvironment {
try {
val subplugins = ServiceLoader.load(
KotlinGradleSubplugin::class.java, project.buildscript.classLoader).toList()
val subpluginDependencyNames =
subplugins.mapTo(hashSetOf<String>()) { it.getGroupName() + ":" + it.getArtifactName() }
val subplugins = ServiceLoader.load(KotlinGradleSubplugin::class.java, project.buildscript.classLoader).toList()

val classpath = project.buildscript.configurations.getByName("classpath")
val subpluginClasspaths = hashMapOf<KotlinGradleSubplugin, List<String>>()
val resolvedClasspathArtifacts = classpath.resolvedConfiguration.resolvedArtifacts.toList()
val subpluginClasspaths = hashMapOf<KotlinGradleSubplugin, List<File>>()

for (subplugin in subplugins) {
val files = classpath.dependencies
.filter { subpluginDependencyNames.contains(it.group + ":" + it.name) }
.flatMap { classpath.files(it).map { it.absolutePath } }
subpluginClasspaths.put(subplugin, files)
val file = resolvedClasspathArtifacts
.firstOrNull {
val id = it.moduleVersion.id
subplugin.getGroupName() == id.group && subplugin.getArtifactName() == id.name
}?.file
if (file != null) {
subpluginClasspaths.put(subplugin, listOf(file))
}
}

return SubpluginEnvironment(subpluginClasspaths, subplugins)
Expand All @@ -479,7 +481,7 @@ private fun loadSubplugins(project: Project): SubpluginEnvironment {
}

class SubpluginEnvironment(
val subpluginClasspaths: Map<KotlinGradleSubplugin, List<String>>,
val subpluginClasspaths: Map<KotlinGradleSubplugin, List<File>>,
val subplugins: List<KotlinGradleSubplugin>
) {

Expand All @@ -488,17 +490,18 @@ class SubpluginEnvironment(
val pluginArguments = arrayListOf<String>()
fun getPluginOptionString(pluginId: String, key: String, value: String) = "plugin:$pluginId:$key=$value"

subplugins.forEach { subplugin ->
val args = subplugin.getExtraArguments(project, compileTask)
for (subplugin in subplugins) {
if (!subplugin.isApplicable(project, compileTask)) continue

with (subplugin) {
project.logger.kotlinDebug("Subplugin ${getPluginName()} (${getGroupName()}:${getArtifactName()}) loaded.")
}

val subpluginClasspath = subpluginClasspaths[subplugin]
if (args != null && subpluginClasspath != null) {
realPluginClasspaths.addAll(subpluginClasspath)
for (arg in args) {
if (subpluginClasspath != null) {
subpluginClasspath.forEach { realPluginClasspaths.add(it.absolutePath) }

for (arg in subplugin.getExtraArguments(project, compileTask)) {
val option = getPluginOptionString(subplugin.getPluginName(), arg.key, arg.value)
pluginArguments.add(option)
}
Expand Down
@@ -0,0 +1 @@
implementation-class=org.jetbrains.kotlin.gradle.internal.AndroidExtensionsSubpluginIndicator
@@ -0,0 +1 @@
org.jetbrains.kotlin.gradle.internal.AndroidSubplugin

0 comments on commit 26ed1c3

Please sign in to comment.