From a5bad9b48a7f2b4ed24afd67a472e22241cb17a2 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 3 Nov 2015 10:16:10 -0800 Subject: [PATCH] Better PluginInfo. --- src/main/kotlin/com/beust/kobalt/Main.kt | 11 ++++++++--- .../com/beust/kobalt/api/KobaltContext.kt | 5 +---- .../com/beust/kobalt/api/KobaltPluginFile.kt | 18 +++++++++++++++++- .../com/beust/kobalt/internal/JvmCompiler.kt | 10 ++-------- .../internal/remote/GetDependenciesCommand.kt | 8 +++++--- .../beust/kobalt/kotlin/BuildFileCompiler.kt | 11 +++++------ .../kobalt/plugin/android/AndroidPlugin.kt | 2 +- 7 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 852a391f..24a04e41 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -2,6 +2,8 @@ package com.beust.kobalt import com.beust.jcommander.JCommander import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.api.PluginInfo +import com.beust.kobalt.api.PluginInfoDescription import com.beust.kobalt.api.Project import com.beust.kobalt.internal.TaskManager import com.beust.kobalt.internal.remote.KobaltClient @@ -54,7 +56,8 @@ private class Main @Inject constructor( val github: GithubApi, val updateKobalt: UpdateKobalt, val client: KobaltClient, - val server: KobaltServer) { + val server: KobaltServer, + val pluginInfoDescription: PluginInfoDescription) { data class RunInfo(val jc: JCommander, val args: Args) @@ -117,8 +120,9 @@ private class Main @Inject constructor( error(buildFile.path.toFile().path + " does not exist") } else { var allProjects = listOf() + val pluginInfo = PluginInfo(pluginInfoDescription) try { - allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args) + allProjects = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo).compileBuildFiles(args) } catch(ex: Throwable) { // This can happen if the ABI for the build script file changed. Try to wipe .kobalt. log(2, "Couldn't parse preBuildScript.jar: ${ex.message}") @@ -127,7 +131,8 @@ private class Main @Inject constructor( return 1 } else { log(1, "Deleted .kobalt") - allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args) + allProjects = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo) + .compileBuildFiles(args) } } diff --git a/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt b/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt index 6810eaa4..2d4c8e67 100644 --- a/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt +++ b/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt @@ -5,9 +5,6 @@ import com.beust.kobalt.Plugins public class KobaltContext(val args: Args) { fun findPlugin(name: String) = Plugins.findPlugin(name) - var pluginFile: KobaltPluginFile? = null - // sourceContributors - // projectContributors - // compilerContributors + var pluginInfo: PluginInfo? = null } diff --git a/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt b/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt index 27e9bbd8..e972610c 100644 --- a/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt +++ b/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt @@ -19,7 +19,10 @@ interface IClasspathContributor { fun entriesFor(project: Project) : Collection } -class KobaltPluginFile { +/** + * All the information gathered from the various plugin.xml that were collected. + */ +class PluginInfoDescription { fun instanceOf(c: Class) : T = Kobalt.INJECTOR.getInstance(c) val projectContributors : ArrayList> = @@ -33,4 +36,17 @@ class KobaltPluginFile { // source files // compilers // --init +} + +/** + * Turn the classes found in PluginInfoDescription into concrete objects that plugins can then use. + */ +class PluginInfo(val description: PluginInfoDescription) { + val projectContributors = arrayListOf() + val classpathContributors = arrayListOf() + + init { + classpathContributors.addAll(description.classpathContributors.map { description.instanceOf(it) }) + projectContributors.addAll(description.projectContributors.map { description.instanceOf(it) }) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt index 53c4160f..a3562840 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -55,14 +55,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) private fun runClasspathContributors(context: KobaltContext?, project: Project) : Collection { val result = arrayListOf() - 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)) - } + context?.pluginInfo?.classpathContributors?.forEach { it: IClasspathContributor -> + result.addAll(it.entriesFor(project)) } return result } diff --git a/src/main/kotlin/com/beust/kobalt/internal/remote/GetDependenciesCommand.kt b/src/main/kotlin/com/beust/kobalt/internal/remote/GetDependenciesCommand.kt index 45b48e43..69501c41 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/remote/GetDependenciesCommand.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/remote/GetDependenciesCommand.kt @@ -1,6 +1,8 @@ package com.beust.kobalt.internal.remote import com.beust.kobalt.Args +import com.beust.kobalt.api.PluginInfo +import com.beust.kobalt.api.PluginInfoDescription import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.kotlin.BuildFileCompiler import com.beust.kobalt.maven.DependencyManager @@ -10,8 +12,8 @@ import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.log import com.google.gson.Gson import com.google.gson.JsonObject -import javax.inject.Inject import java.nio.file.Paths +import javax.inject.Inject /** * This command returns the list of dependencies for the given buildFile. @@ -24,11 +26,11 @@ import java.nio.file.Paths */ class GetDependenciesCommand @Inject constructor(val executors: KobaltExecutors, val buildFileCompilerFactory: BuildFileCompiler.IFactory, val args: Args, - val dependencyManager: DependencyManager) : ICommand { + val dependencyManager: DependencyManager, val pluginInfoDescription: PluginInfoDescription) : ICommand { override val name = "getDependencies" override fun run(sender: ICommandSender, received: JsonObject) { val buildFile = BuildFile(Paths.get(received.get("buildFile").asString), "GetDependenciesCommand") - val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile)) + val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile), PluginInfo(pluginInfoDescription)) scriptCompiler.observable.subscribe { buildScriptInfo -> if (buildScriptInfo.projects.size > 0) { sender.sendData(toData(buildScriptInfo)) diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index 4a6d8409..dd892f08 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -25,10 +25,10 @@ import java.util.jar.JarInputStream import javax.inject.Inject public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List, - val files: KFiles, val plugins: Plugins, val kobaltPluginFile: KobaltPluginFile) { + @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins) { interface IFactory { - fun create(@Assisted("buildFiles") buildFiles: List) : BuildFileCompiler + fun create(@Assisted("buildFiles") buildFiles: List, pluginInfo: PluginInfo) : BuildFileCompiler } val observable = PublishSubject.create() @@ -37,7 +37,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b fun compileBuildFiles(args: Args): List { val context = KobaltContext(args) - context.pluginFile = kobaltPluginFile + context.pluginInfo = pluginInfo Kobalt.context = context val allProjects = findProjects() @@ -226,9 +226,8 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b // Topological().let { topologicalProjects -> val all = hashSetOf() - kobaltPluginFile.projectContributors.forEach { cls -> - val ip: IProjectContributor = kobaltPluginFile.instanceOf(cls) - val descriptions = ip.projects() + pluginInfo.projectContributors.forEach { contributor -> + val descriptions = contributor.projects() descriptions.forEach { pd -> all.add(pd.project) pd.dependsOn.forEach { dependsOn -> 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 fc88f97d..8df28073 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.pluginFile?.classpathContributors?.add(this.javaClass) + context.pluginInfo?.classpathContributors?.add(this) // 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")