From 561f87726e4b2757d96967f78ce1eb6664649ebd Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 3 Nov 2015 10:56:35 -0800 Subject: [PATCH] New PluginProperties implementation. --- .../kotlin/com/beust/kobalt/api/BasePlugin.kt | 11 --------- .../com/beust/kobalt/api/KobaltContext.kt | 3 ++- .../com/beust/kobalt/api/PluginProperties.kt | 23 +++++++++++++++++++ .../kobalt/api/annotation/Annotations.kt | 12 ++++++++-- .../com/beust/kobalt/internal/JvmCompiler.kt | 2 +- .../kobalt/internal/JvmCompilerPlugin.kt | 7 +++++- .../beust/kobalt/kotlin/BuildFileCompiler.kt | 4 +++- .../kobalt/plugin/android/AndroidPlugin.kt | 9 ++++---- .../plugin/packaging/PackagingPlugin.kt | 8 +++++-- 9 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/api/PluginProperties.kt diff --git a/src/main/kotlin/com/beust/kobalt/api/BasePlugin.kt b/src/main/kotlin/com/beust/kobalt/api/BasePlugin.kt index 3b4d4a7d..7b5b7a95 100644 --- a/src/main/kotlin/com/beust/kobalt/api/BasePlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/api/BasePlugin.kt @@ -17,15 +17,4 @@ abstract public class BasePlugin : Plugin { fun addProject(project: Project, dependsOn: Array) { projects.add(ProjectDescription(project, dependsOn.toList())) } - - /** - * Plugins can expose properties to other plugins through this map. - * TODO: move this to its own class, it shouldn't be polluting BasePlugin - */ - val pluginProperties = hashMapOf() - - companion object { - val BUILD_DIR = "buildDir" - val LIBS_DIR = "libsDir" - } } diff --git a/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt b/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt index 2d4c8e67..f8e9e630 100644 --- a/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt +++ b/src/main/kotlin/com/beust/kobalt/api/KobaltContext.kt @@ -5,6 +5,7 @@ import com.beust.kobalt.Plugins public class KobaltContext(val args: Args) { fun findPlugin(name: String) = Plugins.findPlugin(name) - var pluginInfo: PluginInfo? = null + lateinit var pluginInfo: PluginInfo + lateinit var pluginProperties: PluginProperties } diff --git a/src/main/kotlin/com/beust/kobalt/api/PluginProperties.kt b/src/main/kotlin/com/beust/kobalt/api/PluginProperties.kt new file mode 100644 index 00000000..a92d767b --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/api/PluginProperties.kt @@ -0,0 +1,23 @@ +package com.beust.kobalt.api + +import com.google.inject.Inject +import java.util.* + +/** + * Plugins can publish to and read from this object in order to exchange information. Keys stored in + * these maps should be annotated with @ExportedProperty. + */ +class PluginProperties @Inject constructor() { + private val pluginProperties = hashMapOf>() + + fun put(pluginName: String, key: String, value: Any) { + var map = pluginProperties[pluginName] + if (map == null) { + map = hashMapOf() + pluginProperties.put(pluginName, map) + } + map.put(key, value) + } + + fun get(pluginName: String, key: String) = pluginProperties[pluginName]?.get(key) +} diff --git a/src/main/kotlin/com/beust/kobalt/api/annotation/Annotations.kt b/src/main/kotlin/com/beust/kobalt/api/annotation/Annotations.kt index 949402ac..7ca7edc4 100644 --- a/src/main/kotlin/com/beust/kobalt/api/annotation/Annotations.kt +++ b/src/main/kotlin/com/beust/kobalt/api/annotation/Annotations.kt @@ -1,7 +1,9 @@ package com.beust.kobalt.api.annotation -import kotlin.annotation.Retention - +/** + * Plugins that export directives should annotated those with this annotation so they can be documented and also + * receive special treatment for auto completion in the plug-in. + */ annotation class Directive @Retention(AnnotationRetention.RUNTIME) @@ -17,3 +19,9 @@ annotation class Task(val name: String, /** Tasks that this task will always run after */ val alwaysRunAfter: Array = arrayOf() ) + +/** + * Plugins that export properties should annotated those with this annotation so they can be documented. + */ +@Retention(AnnotationRetention.RUNTIME) +annotation class ExportedProperty \ 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 a3562840..b3ed7af3 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -55,7 +55,7 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) private fun runClasspathContributors(context: KobaltContext?, project: Project) : Collection { val result = arrayListOf() - context?.pluginInfo?.classpathContributors?.forEach { it: IClasspathContributor -> + context!!.pluginInfo.classpathContributors.forEach { it: IClasspathContributor -> result.addAll(it.entriesFor(project)) } return result diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index 15978c7c..497c50fa 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -4,6 +4,7 @@ import com.beust.kobalt.api.BasePlugin import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Directive +import com.beust.kobalt.api.annotation.ExportedProperty import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.maven.* import com.beust.kobalt.misc.KFiles @@ -24,6 +25,9 @@ abstract class JvmCompilerPlugin @Inject constructor( open val jvmCompiler: JvmCompiler) : BasePlugin() { companion object { + @ExportedProperty + const val BUILD_DIR = "buildDir" + const val TASK_CLEAN = "clean" const val TASK_TEST = "test" @@ -41,10 +45,11 @@ abstract class JvmCompilerPlugin @Inject constructor( var context: KobaltContext? = null + override fun apply(project: Project, context: KobaltContext) { super.apply(project, context) this.context = context - pluginProperties.put(BUILD_DIR, project.buildDirectory + File.separator + "classes") + context.pluginProperties.put(name, BUILD_DIR, project.buildDirectory + File.separator + "classes") } /** diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index a97fff51..e8d74cf8 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -25,7 +25,8 @@ import java.util.jar.JarInputStream import javax.inject.Inject public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List, - @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins) { + @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins, + val pluginProperties: PluginProperties) { interface IFactory { fun create(@Assisted("buildFiles") buildFiles: List, pluginInfo: PluginInfo) : BuildFileCompiler @@ -38,6 +39,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b fun compileBuildFiles(args: Args): List { val context = KobaltContext(args) context.pluginInfo = pluginInfo + context.pluginProperties = pluginProperties 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 8df28073..54fcae08 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt @@ -14,6 +14,7 @@ import com.beust.kobalt.misc.RunCommand import com.beust.kobalt.misc.log import com.beust.kobalt.plugin.java.JavaCompiler import com.beust.kobalt.plugin.packaging.JarUtils +import com.beust.kobalt.plugin.packaging.PackagingPlugin import com.google.common.collect.HashMultimap import com.google.inject.Inject import com.google.inject.Singleton @@ -38,7 +39,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) : val ANDROID_HOME = "/Users/beust/android/adt-bundle-mac-x86_64-20140702/sdk" override val name = "android" - var context: KobaltContext? = null + lateinit var context: KobaltContext override fun apply(project: Project, context: KobaltContext) { log(1, "Applying plug-in Android on project $project") @@ -46,7 +47,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) : if (accept(project)) { project.compileDependencies.add(FileDependency(androidJar(project).toString())) } - context.pluginInfo?.classpathContributors?.add(this) + 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") @@ -93,8 +94,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) : val generated = generated(project) val buildToolsDir = buildToolsVersion(project) val dx = "$ANDROID_HOME/build-tools/$buildToolsDir/dx" - val buildDir = (Plugins.findPlugin("java") as BasePlugin).pluginProperties[BUILD_DIR] - val libsDir = (Plugins.findPlugin("packaging") as BasePlugin).pluginProperties[LIBS_DIR] + val buildDir = context.pluginProperties.get("java", JvmCompilerPlugin.BUILD_DIR) + val libsDir = context.pluginProperties.get("packaging", PackagingPlugin.LIBS_DIR) File(libsDir!!.toString()).mkdirs() RunCommand(dx).run(listOf("--dex", "--output", KFiles.joinDir(libsDir!!.toString(), "classes.dex"), buildDir!!.toString())) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index 995e1180..b25f74e2 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -8,6 +8,7 @@ import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.api.annotation.Directive +import com.beust.kobalt.api.annotation.ExportedProperty import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.glob import com.beust.kobalt.internal.JvmCompilerPlugin @@ -40,7 +41,10 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana val executors: KobaltExecutors, val localRepo: LocalRepo) : BasePlugin() { companion object { - public const val TASK_ASSEMBLE : String = "assemble" + @ExportedProperty + const val LIBS_DIR = "libsDir" + + const val TASK_ASSEMBLE : String = "assemble" } override val name = "packaging" @@ -49,7 +53,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana override fun apply(project: Project, context: KobaltContext) { super.apply(project, context) - pluginProperties.put(LIBS_DIR, libsDir(project)) + context.pluginProperties.put(name, LIBS_DIR, libsDir(project)) } private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs")