diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 9b54250e..465bf5d6 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -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, pluginInfo: PluginInfo) { + allProjects.forEach { + runClasspathInterceptors(it.compileDependencies) + runClasspathInterceptors(it.compileProvidedDependencies) + runClasspathInterceptors(it.compileRuntimeDependencies) + runClasspathInterceptors(it.testProvidedDependencies) + runClasspathInterceptors(it.testDependencies) + } + } + + private fun runClasspathInterceptors(dependencies: ArrayList) = with(dependencies) { + val deps = interceptDependencies(pluginInfo, this) + clear() + addAll(deps) + } + + private fun interceptDependencies(pluginInfo: PluginInfo, dependencies: ArrayList) + : ArrayList { + val result = arrayListOf() + 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")) diff --git a/src/main/kotlin/com/beust/kobalt/api/IClasspathInterceptor.kt b/src/main/kotlin/com/beust/kobalt/api/IClasspathInterceptor.kt new file mode 100644 index 00000000..7a949959 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/api/IClasspathInterceptor.kt @@ -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) : List +} diff --git a/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt b/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt index d84403b3..6101da99 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt @@ -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() val runnerContributors = arrayListOf() val testRunnerContributors = arrayListOf() + val classpathInterceptors = arrayListOf() // 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) { diff --git a/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt b/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt index 086f07ca..a2a133b0 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt @@ -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) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt index 32186e8c..5ed46146 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt @@ -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) - : ConfigPlugin(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor, - ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor { - +public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, val merger: Merger, + val executors: KobaltExecutors) + : ConfigPlugin(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor, + 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): List { + val result = arrayListOf() + 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", diff --git a/src/main/resources/META-INF/kobalt-plugin.xml b/src/main/resources/META-INF/kobalt-plugin.xml index e9435e2d..6efbc7d7 100644 --- a/src/main/resources/META-INF/kobalt-plugin.xml +++ b/src/main/resources/META-INF/kobalt-plugin.xml @@ -45,4 +45,7 @@ com.beust.kobalt.internal.JUnitRunner com.beust.kobalt.internal.TestNgRunner + + com.beust.kobalt.plugin.android.AndroidPlugin + \ No newline at end of file