From bcbcfa3144119f69d67b5121b3791aefc8158388 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 3 Nov 2015 09:51:45 -0800 Subject: [PATCH] Better IClasspathContributor handling. --- .../beust/kobalt/api/IClasspathContributor.kt | 11 ------- .../com/beust/kobalt/api/KobaltContext.kt | 3 +- .../com/beust/kobalt/api/KobaltPluginFile.kt | 18 +++++++++-- .../com/beust/kobalt/internal/JvmCompiler.kt | 11 +++++-- .../beust/kobalt/kotlin/BuildFileCompiler.kt | 1 + .../kobalt/plugin/android/AndroidPlugin.kt | 2 +- .../kobalt/plugin/kotlin/KotlinCompiler.kt | 26 +++++++--------- .../kobalt/plugin/kotlin/KotlinPlugin.kt | 31 +++++++++++++------ 8 files changed, 62 insertions(+), 41 deletions(-) delete mode 100644 src/main/kotlin/com/beust/kobalt/api/IClasspathContributor.kt diff --git a/src/main/kotlin/com/beust/kobalt/api/IClasspathContributor.kt b/src/main/kotlin/com/beust/kobalt/api/IClasspathContributor.kt deleted file mode 100644 index 47720e7e..00000000 --- a/src/main/kotlin/com/beust/kobalt/api/IClasspathContributor.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.beust.kobalt.api - -import com.beust.kobalt.maven.IClasspathDependency - -/** - * Implement this interface in order to add your own entries to the classpath. A list of contributors - * can be found on the `KobaltContext`. - */ -interface IClasspathContributor { - fun entriesFor(project: Project) : Collection -} diff --git a/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt b/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt index c0d86336..6810eaa4 100644 --- a/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt +++ b/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt @@ -2,11 +2,10 @@ package com.beust.kobalt.api import com.beust.kobalt.Args import com.beust.kobalt.Plugins -import java.util.* public class KobaltContext(val args: Args) { fun findPlugin(name: String) = Plugins.findPlugin(name) - val classpathContributors: ArrayList = arrayListOf() + var pluginFile: KobaltPluginFile? = null // sourceContributors // projectContributors // compilerContributors diff --git a/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt b/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt index 07586ad1..27e9bbd8 100644 --- a/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt +++ b/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt @@ -1,7 +1,9 @@ package com.beust.kobalt.api +import com.beust.kobalt.maven.IClasspathDependency import com.beust.kobalt.plugin.java.JavaPlugin import com.beust.kobalt.plugin.kotlin.KotlinPlugin +import java.util.* class ProjectDescription(val project: Project, val dependsOn: List) @@ -9,11 +11,23 @@ interface IProjectContributor { fun projects() : List } +/** + * Implement this interface in order to add your own entries to the classpath. A list of contributors + * can be found on the `KobaltContext`. + */ +interface IClasspathContributor { + fun entriesFor(project: Project) : Collection +} + class KobaltPluginFile { - fun instanceOf(c: Class) = Kobalt.INJECTOR.getInstance(c) - val projectContributors : List> = + fun instanceOf(c: Class) : T = Kobalt.INJECTOR.getInstance(c) + + val projectContributors : ArrayList> = arrayListOf(JavaPlugin::class.java, KotlinPlugin::class.java) + val classpathContributors: ArrayList> = + arrayListOf(KotlinPlugin::class.java) + // Future contributors: // compilerArgs // source files diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt index de7ea2b5..53c4160f 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.internal +import com.beust.kobalt.api.IClasspathContributor import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.maven.DependencyManager @@ -54,8 +55,14 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) private fun runClasspathContributors(context: KobaltContext?, project: Project) : Collection { val result = arrayListOf() - context?.classpathContributors?.forEach { - result.addAll(it.entriesFor(project)) + val classes : List>? = context?.pluginFile?.classpathContributors + if (classes != null) { + val contributors: List = classes.map { + context?.pluginFile?.instanceOf(it)!! + } + contributors.forEach { it: IClasspathContributor -> + result.addAll(it.entriesFor(project)) + } } return result } diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index 480566ec..4a6d8409 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -37,6 +37,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b fun compileBuildFiles(args: Args): List { val context = KobaltContext(args) + context.pluginFile = kobaltPluginFile Kobalt.context = context val allProjects = findProjects() 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 67e14d73..fc88f97d 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt @@ -46,7 +46,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) : if (accept(project)) { project.compileDependencies.add(FileDependency(androidJar(project).toString())) } - context.classpathContributors.add(this) + context.pluginFile?.classpathContributors?.add(this.javaClass) // TODO: Find a more flexible way of enabling this, e.g. creating a contributor for it (Kobalt.findPlugin("java") as JvmCompilerPlugin).addCompilerArgs("-target", "1.6", "-source", "1.6") diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt index 88b960d0..aff0c980 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt @@ -7,7 +7,10 @@ import com.beust.kobalt.internal.CompilerActionInfo import com.beust.kobalt.internal.ICompilerAction import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.TaskResult -import com.beust.kobalt.maven.* +import com.beust.kobalt.maven.DepFactory +import com.beust.kobalt.maven.FileDependency +import com.beust.kobalt.maven.IClasspathDependency +import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.log import org.jetbrains.kotlin.cli.common.CLICompiler @@ -27,7 +30,9 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo, val depFactory: DepFactory, val executors: KobaltExecutors, val jvmCompiler: JvmCompiler) { - private val KOTLIN_VERSION = "1.0.0-beta-1038" + companion object { + val KOTLIN_VERSION = "1.0.0-beta-1038" + } val compilerAction = object: ICompilerAction { override fun compile(info: CompilerActionInfo): TaskResult { @@ -44,13 +49,6 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo, } } - private fun getKotlinCompilerJar(name: String) : String { - val id = "org.jetbrains.kotlin:$name:$KOTLIN_VERSION" - val dep = MavenDependency.create(id, executors.miscExecutor) - val result = dep.jarFile.get().absolutePath - return result - } - /** * Create an ICompilerAction based on the parameters and send it to JvmCompiler.doCompile(). */ @@ -66,12 +64,12 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo, executor.shutdown() - val classpathList = arrayListOf( - getKotlinCompilerJar("kotlin-stdlib"), - getKotlinCompilerJar("kotlin-compiler-embeddable")) - .map { FileDependency(it) } +// val classpathList = arrayListOf( +// getKotlinCompilerJar("kotlin-stdlib"), +// getKotlinCompilerJar("kotlin-compiler-embeddable")) +// .map { FileDependency(it) } - val dependencies = compileDependencies + classpathList + otherClasspath.map { FileDependency(it)} + val dependencies = compileDependencies + otherClasspath.map { FileDependency(it)} val info = CompilerActionInfo(project?.directory, dependencies, sourceFiles, outputDir, args) return jvmCompiler.doCompile(project, context, compilerAction, info) } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt index 2c337d1b..4596a2db 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -1,18 +1,12 @@ package com.beust.kobalt.plugin.kotlin -import com.beust.kobalt.api.BasePlugin -import com.beust.kobalt.api.IProjectContributor -import com.beust.kobalt.api.Kobalt -import com.beust.kobalt.api.Project +import com.beust.kobalt.api.* import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.TaskResult -import com.beust.kobalt.maven.DepFactory -import com.beust.kobalt.maven.DependencyManager -import com.beust.kobalt.maven.IClasspathDependency -import com.beust.kobalt.maven.LocalRepo +import com.beust.kobalt.maven.* import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors import java.io.File @@ -28,7 +22,7 @@ class KotlinPlugin @Inject constructor( override val executors: KobaltExecutors, override val jvmCompiler: JvmCompiler) : JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler), - IProjectContributor { + IProjectContributor, IClasspathContributor { init { Kobalt.registerCompiler(KotlinCompilerInfo()) @@ -91,7 +85,26 @@ class KotlinPlugin @Inject constructor( }.compile(project, context) } + // interface IProjectContributor override fun projects() = projects + + private fun getKotlinCompilerJar(name: String) : String { + val id = "org.jetbrains.kotlin:$name:${KotlinCompiler.KOTLIN_VERSION}" + val dep = MavenDependency.create(id, executors.miscExecutor) + val result = dep.jarFile.get().absolutePath + return result + } + + + // interface IClasspathContributor + override fun entriesFor(project: Project) : List = + if (project is KotlinProject) { + // All Kotlin projects automatically get the Kotlin runtime added to their class path + arrayListOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-compiler-embeddable")) + .map { FileDependency(it) } + } else { + arrayListOf() + } } /**