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

New PluginProperties implementation.

This commit is contained in:
Cedric Beust 2015-11-03 10:56:35 -08:00
parent 642f4fc0bc
commit 561f87726e
9 changed files with 56 additions and 23 deletions

View file

@ -17,15 +17,4 @@ abstract public class BasePlugin : Plugin {
fun addProject(project: Project, dependsOn: Array<out Project>) {
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<String, Any>()
companion object {
val BUILD_DIR = "buildDir"
val LIBS_DIR = "libsDir"
}
}

View file

@ -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
}

View file

@ -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<String, HashMap<String, Any>>()
fun put(pluginName: String, key: String, value: Any) {
var map = pluginProperties[pluginName]
if (map == null) {
map = hashMapOf<String, Any>()
pluginProperties.put(pluginName, map)
}
map.put(key, value)
}
fun get(pluginName: String, key: String) = pluginProperties[pluginName]?.get(key)
}

View file

@ -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<String> = arrayOf()
)
/**
* Plugins that export properties should annotated those with this annotation so they can be documented.
*/
@Retention(AnnotationRetention.RUNTIME)
annotation class ExportedProperty

View file

@ -55,7 +55,7 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
private fun runClasspathContributors(context: KobaltContext?, project: Project) :
Collection<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
context?.pluginInfo?.classpathContributors?.forEach { it: IClasspathContributor ->
context!!.pluginInfo.classpathContributors.forEach { it: IClasspathContributor ->
result.addAll(it.entriesFor(project))
}
return result

View file

@ -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")
}
/**

View file

@ -25,7 +25,8 @@ import java.util.jar.JarInputStream
import javax.inject.Inject
public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List<BuildFile>,
@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<BuildFile>, pluginInfo: PluginInfo) : BuildFileCompiler
@ -38,6 +39,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
fun compileBuildFiles(args: Args): List<Project> {
val context = KobaltContext(args)
context.pluginInfo = pluginInfo
context.pluginProperties = pluginProperties
Kobalt.context = context
val allProjects = findProjects()

View file

@ -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()))

View file

@ -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")