1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-25 16:07:12 -07:00

Introducing IClasspathInterceptors.

Automatically adds the "aar" packaging to com.android.support.
This commit is contained in:
Cedric Beust 2015-11-28 11:43:41 -08:00
parent 7af789d282
commit a2a5ff429b
6 changed files with 78 additions and 11 deletions

View file

@ -12,6 +12,7 @@ import com.beust.kobalt.internal.remote.KobaltServer
import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.Http
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.maven.dependency.IClasspathDependency
import com.beust.kobalt.misc.*
import com.google.inject.Guice
import java.io.File
@ -162,6 +163,11 @@ private class Main @Inject constructor(
//
allProjects.forEach { addReposFromContributors(it) }
//
// Run all their dependencies through the IDependencyInterceptors
//
runClasspathInterceptors(allProjects, pluginInfo)
log(2, "Final list of repos:\n " + Kobalt.repos.joinToString("\n "))
if (args.dependency != null) {
@ -203,6 +209,31 @@ private class Main @Inject constructor(
return result
}
private fun runClasspathInterceptors(allProjects: List<Project>, pluginInfo: PluginInfo) {
allProjects.forEach {
runClasspathInterceptors(it.compileDependencies)
runClasspathInterceptors(it.compileProvidedDependencies)
runClasspathInterceptors(it.compileRuntimeDependencies)
runClasspathInterceptors(it.testProvidedDependencies)
runClasspathInterceptors(it.testDependencies)
}
}
private fun runClasspathInterceptors(dependencies: ArrayList<IClasspathDependency>) = with(dependencies) {
val deps = interceptDependencies(pluginInfo, this)
clear()
addAll(deps)
}
private fun interceptDependencies(pluginInfo: PluginInfo, dependencies: ArrayList<IClasspathDependency>)
: ArrayList<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
pluginInfo.classpathInterceptors.forEach {
result.addAll(it.intercept(dependencies))
}
return result
}
private fun findBuildFile(): File {
val files = arrayListOf("Build.kt", "build.kobalt", KFiles.src("build.kobalt"),
KFiles.src("Build.kt"))

View file

@ -0,0 +1,10 @@
package com.beust.kobalt.api
import com.beust.kobalt.maven.dependency.IClasspathDependency
/**
* Modify a list of dependencies before Kobalt starts using them.
*/
interface IClasspathInterceptor : IInterceptor {
fun intercept(dependencies: List<IClasspathDependency>) : List<IClasspathDependency>
}

View file

@ -67,6 +67,9 @@ class KobaltPluginXml {
@XmlElement(name = "test-runner-contributors") @JvmField
var testRunnerContributors: ClassNameXml? = null
@XmlElement(name = "classpath-interceptors") @JvmField
var classpathInterceptors: ClassNameXml? = null
}
class ContributorXml {
@ -96,6 +99,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
val buildDirectoryInterceptors = arrayListOf<IBuildDirectoryIncerceptor>()
val runnerContributors = arrayListOf<IRunnerContributor>()
val testRunnerContributors = arrayListOf<IRunnerContributor>()
val classpathInterceptors = arrayListOf<IClasspathInterceptor>()
// Future contributors:
// source files
@ -175,6 +179,9 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
xml.testRunnerContributors?.className?.forEach {
testRunnerContributors.add(factory.instanceOf(forName(it)) as IRunnerContributor)
}
xml.classpathInterceptors?.className?.forEach {
classpathInterceptors.add(factory.instanceOf(forName(it)) as IClasspathInterceptor)
}
}
fun addPluginInfo(pluginInfo: PluginInfo) {

View file

@ -51,9 +51,9 @@ public class MavenDependency @Inject constructor(mavenId: MavenId,
val executor = Kobalt.INJECTOR.getInstance(Key.get(ExecutorService::class.java, DependencyExecutor::class.java))
val depFactory = Kobalt.INJECTOR.getInstance(DepFactory::class.java)
fun create(id: String, ex: ExecutorService = executor) : IClasspathDependency {
return depFactory.create(id, ex)
}
fun create(id: String, ex: ExecutorService = executor) = depFactory.create(id, ex)
fun create(mavenId: MavenId, ex: ExecutorService = executor) = depFactory.create(mavenId.toId, ex)
}

View file

@ -8,10 +8,8 @@ import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.IClasspathDependency
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.RunCommand
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.*
import com.beust.kobalt.plugin.java.JavaCompiler
import com.beust.kobalt.plugin.packaging.JarUtils
import com.google.common.collect.HashMultimap
@ -24,10 +22,10 @@ import java.nio.file.Path
import java.nio.file.Paths
@Singleton
public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, val merger: Merger)
public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, val merger: Merger,
val executors: KobaltExecutors)
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor {
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor, IClasspathInterceptor {
companion object {
const val PLUGIN_NAME = "Android"
const val TASK_GENERATE_DEX = "generateDex"
@ -407,6 +405,24 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
return TaskResult()
}
}
/**
* Automatically add the "aar" packaging for support libraries.
*/
// IClasspathInterceptor
override fun intercept(dependencies: List<IClasspathDependency>): List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
dependencies.forEach {
if (it is MavenDependency && it.groupId == "com.android.support") {
val id = MavenId.create(it.groupId, it.artifactId, "aar", it.version)
result.add(MavenDependency.create(id, executors.miscExecutor))
} else {
result.add(it)
}
}
return result
}
}
class AndroidConfig(val project: Project, var compileSdkVersion : String = "23",

View file

@ -45,4 +45,7 @@
<class-name>com.beust.kobalt.internal.JUnitRunner</class-name>
<class-name>com.beust.kobalt.internal.TestNgRunner</class-name>
</test-runner-contributors>
<classpath-interceptors>
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
</classpath-interceptors>
</kobalt-plugin>