mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 08:38:13 -07:00
Introducing ITaskContributor.
This commit is contained in:
parent
6656c3f503
commit
cbd6e0fbe4
10 changed files with 100 additions and 45 deletions
|
@ -89,6 +89,11 @@ public class Plugins @Inject constructor (val taskManagerProvider : Provider<Tas
|
||||||
|
|
||||||
currentClass = currentClass.superclass
|
currentClass = currentClass.superclass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Collect all the tasks from the task contributors
|
||||||
|
context.pluginInfo.taskContributors.forEach {
|
||||||
|
taskManager.dynamicTasks.addAll(it.tasksFor(context).map { TaskManager.PluginDynamicTask(plugin, it) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that we have collected all static and dynamic tasks, turn them all into plug-in tasks
|
// Now that we have collected all static and dynamic tasks, turn them all into plug-in tasks
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package com.beust.kobalt.api
|
package com.beust.kobalt.api
|
||||||
|
|
||||||
import com.beust.kobalt.Plugins
|
import com.beust.kobalt.Plugins
|
||||||
import com.beust.kobalt.TaskResult
|
|
||||||
import com.beust.kobalt.Variant
|
|
||||||
import com.beust.kobalt.internal.TaskManager
|
import com.beust.kobalt.internal.TaskManager
|
||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
|
|
||||||
|
@ -22,25 +20,4 @@ abstract public class BasePlugin : IPlugin {
|
||||||
fun addProject(project: Project, dependsOn: Array<out Project>) {
|
fun addProject(project: Project, dependsOn: Array<out Project>) {
|
||||||
projects.add(ProjectDescription(project, dependsOn.toList()))
|
projects.add(ProjectDescription(project, dependsOn.toList()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register dynamic tasks corresponding to the variants found in the project,e.g. assembleDevDebug,
|
|
||||||
* assembleDevRelease, etc...
|
|
||||||
*/
|
|
||||||
protected fun addVariantTasks(project: Project, taskName: String,
|
|
||||||
runBefore : List<String> = emptyList(),
|
|
||||||
runAfter : List<String> = emptyList(),
|
|
||||||
runTask: (Project) -> TaskResult) {
|
|
||||||
Variant.allVariants(project).forEach { variant ->
|
|
||||||
val variantTaskName = variant.toTask(taskName)
|
|
||||||
taskManager.addTask(this, project, variantTaskName, variantTaskName,
|
|
||||||
runBefore = runBefore.map { variant.toTask(it) },
|
|
||||||
runAfter = runAfter.map { variant.toTask(it) },
|
|
||||||
task = { p: Project ->
|
|
||||||
context.variant = variant
|
|
||||||
runTask(project)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
17
src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt
Normal file
17
src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package com.beust.kobalt.api
|
||||||
|
|
||||||
|
import com.beust.kobalt.TaskResult
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plug-ins that need to add dynamic tasks (tasks that are not methods annotated with @Task) need
|
||||||
|
* to implement this interface.
|
||||||
|
*/
|
||||||
|
interface ITaskContributor : IContributor {
|
||||||
|
fun tasksFor(context: KobaltContext) : List<DynamicTask>
|
||||||
|
}
|
||||||
|
|
||||||
|
class DynamicTask(val taskName: String, val taskDescription: String = "",
|
||||||
|
val runBefore: List<String> = listOf<String>(),
|
||||||
|
val runAfter: List<String> = listOf<String>(),
|
||||||
|
val alwaysRunAfter: List<String> = listOf<String>(),
|
||||||
|
val closure: (Project) -> TaskResult)
|
34
src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt
Normal file
34
src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package com.beust.kobalt.api
|
||||||
|
|
||||||
|
import com.beust.kobalt.TaskResult
|
||||||
|
import com.beust.kobalt.Variant
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plug-ins that are ITaskContributor can use this class to manage their collection of tasks and
|
||||||
|
* implement the interface by delegating to an instance of this class (if injection permits).
|
||||||
|
*/
|
||||||
|
class TaskContributor : ITaskContributor {
|
||||||
|
val dynamicTasks = arrayListOf<DynamicTask>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register dynamic tasks corresponding to the variants found in the project,e.g. assembleDevDebug,
|
||||||
|
* assembleDevRelease, etc...
|
||||||
|
*/
|
||||||
|
fun addVariantTasks(project: Project, context: KobaltContext, taskName: String,
|
||||||
|
runBefore : List<String> = emptyList(),
|
||||||
|
runAfter : List<String> = emptyList(),
|
||||||
|
runTask: (Project) -> TaskResult) {
|
||||||
|
Variant.allVariants(project).forEach { variant ->
|
||||||
|
val variantTaskName = variant.toTask(taskName)
|
||||||
|
dynamicTasks.add(DynamicTask(variantTaskName, variantTaskName,
|
||||||
|
runBefore = runBefore.map { variant.toTask(it) },
|
||||||
|
runAfter = runAfter.map { variant.toTask(it) },
|
||||||
|
closure = { p: Project ->
|
||||||
|
context.variant = variant
|
||||||
|
runTask(project)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun tasksFor(context: KobaltContext) : List<DynamicTask> = dynamicTasks
|
||||||
|
}
|
|
@ -25,7 +25,9 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
open val depFactory: DepFactory,
|
open val depFactory: DepFactory,
|
||||||
open val dependencyManager: DependencyManager,
|
open val dependencyManager: DependencyManager,
|
||||||
open val executors: KobaltExecutors,
|
open val executors: KobaltExecutors,
|
||||||
open val jvmCompiler: JvmCompiler) : BasePlugin(), IProjectContributor {
|
open val jvmCompiler: JvmCompiler,
|
||||||
|
val taskContributor : TaskContributor = TaskContributor())
|
||||||
|
: BasePlugin(), IProjectContributor, ITaskContributor by taskContributor {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@ExportedProjectProperty(doc = "Projects this project depends on", type = "List<ProjectDescription>")
|
@ExportedProjectProperty(doc = "Projects this project depends on", type = "List<ProjectDescription>")
|
||||||
|
@ -52,7 +54,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
override fun apply(project: Project, context: KobaltContext) {
|
override fun apply(project: Project, context: KobaltContext) {
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
||||||
addVariantTasks(project, "compile", runTask = { taskCompile(project) })
|
taskContributor.addVariantTasks(project, context, "compile", runTask = { taskCompile(project) })
|
||||||
}
|
}
|
||||||
|
|
||||||
@Task(name = TASK_TEST, description = "Run the tests", runAfter = arrayOf("compile", "compileTest"))
|
@Task(name = TASK_TEST, description = "Run the tests", runAfter = arrayOf("compile", "compileTest"))
|
||||||
|
|
|
@ -72,6 +72,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
val docContributors = arrayListOf<IDocContributor>()
|
val docContributors = arrayListOf<IDocContributor>()
|
||||||
val sourceDirContributors = arrayListOf<ISourceDirectoryContributor>()
|
val sourceDirContributors = arrayListOf<ISourceDirectoryContributor>()
|
||||||
val buildConfigFieldContributors = arrayListOf<IBuildConfigFieldContributor>()
|
val buildConfigFieldContributors = arrayListOf<IBuildConfigFieldContributor>()
|
||||||
|
val taskContributors = arrayListOf<ITaskContributor>()
|
||||||
|
|
||||||
// Future contributors:
|
// Future contributors:
|
||||||
// source files
|
// source files
|
||||||
|
@ -140,6 +141,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
if (this is IDocContributor) docContributors.add(this)
|
if (this is IDocContributor) docContributors.add(this)
|
||||||
if (this is ISourceDirectoryContributor) sourceDirContributors.add(this)
|
if (this is ISourceDirectoryContributor) sourceDirContributors.add(this)
|
||||||
if (this is IBuildConfigFieldContributor) buildConfigFieldContributors.add(this)
|
if (this is IBuildConfigFieldContributor) buildConfigFieldContributors.add(this)
|
||||||
|
if (this is ITaskContributor) taskContributors.add(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,6 +168,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
docContributors.addAll(pluginInfo.docContributors)
|
docContributors.addAll(pluginInfo.docContributors)
|
||||||
sourceDirContributors.addAll(pluginInfo.sourceDirContributors)
|
sourceDirContributors.addAll(pluginInfo.sourceDirContributors)
|
||||||
buildConfigFieldContributors.addAll(pluginInfo.buildConfigFieldContributors)
|
buildConfigFieldContributors.addAll(pluginInfo.buildConfigFieldContributors)
|
||||||
|
taskContributors.addAll(pluginInfo.taskContributors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.beust.kobalt.internal
|
package com.beust.kobalt.internal
|
||||||
|
|
||||||
import com.beust.kobalt.*
|
import com.beust.kobalt.*
|
||||||
|
import com.beust.kobalt.api.DynamicTask
|
||||||
import com.beust.kobalt.api.IPlugin
|
import com.beust.kobalt.api.IPlugin
|
||||||
import com.beust.kobalt.api.PluginTask
|
import com.beust.kobalt.api.PluginTask
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
|
@ -220,15 +221,11 @@ public class TaskManager @Inject constructor(val args: Args) {
|
||||||
//
|
//
|
||||||
|
|
||||||
class StaticTask(val plugin: IPlugin, val method: Method, val taskAnnotation: Task)
|
class StaticTask(val plugin: IPlugin, val method: Method, val taskAnnotation: Task)
|
||||||
class DynamicTask(val plugin: IPlugin, val name: String, val description: String,
|
class PluginDynamicTask(val plugin: IPlugin, val task: DynamicTask)
|
||||||
val runBefore: List<String> = listOf<String>(),
|
|
||||||
val runAfter: List<String> = listOf<String>(),
|
|
||||||
val alwaysRunAfter: List<String> = listOf<String>(),
|
|
||||||
val closure: (Project) -> TaskResult)
|
|
||||||
|
|
||||||
val tasks = arrayListOf<PluginTask>()
|
val tasks = arrayListOf<PluginTask>()
|
||||||
val staticTasks = arrayListOf<StaticTask>()
|
val staticTasks = arrayListOf<StaticTask>()
|
||||||
val dynamicTasks = arrayListOf<DynamicTask>()
|
val dynamicTasks = arrayListOf<PluginDynamicTask>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn all the static and dynamic tasks into plug-in tasks, which are then suitable to be executed.
|
* Turn all the static and dynamic tasks into plug-in tasks, which are then suitable to be executed.
|
||||||
|
@ -239,9 +236,10 @@ public class TaskManager @Inject constructor(val args: Args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addDynamicTasks(projects: List<Project>) {
|
private fun addDynamicTasks(projects: List<Project>) {
|
||||||
dynamicTasks.forEach { task ->
|
dynamicTasks.forEach { dynamicTask ->
|
||||||
projects.filter { task.plugin.accept(it) }.forEach { project ->
|
val task = dynamicTask.task
|
||||||
addTask(task.plugin, project, task.name, task.description, task.runBefore, task.runAfter,
|
projects.filter { dynamicTask.plugin.accept(it) }.forEach { project ->
|
||||||
|
addTask(dynamicTask.plugin, project, task.taskName, task.taskDescription, task.runBefore, task.runAfter,
|
||||||
task.alwaysRunAfter, task.closure)
|
task.alwaysRunAfter, task.closure)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
||||||
val executors: KobaltExecutors)
|
val executors: KobaltExecutors)
|
||||||
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
|
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
|
||||||
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor, IClasspathInterceptor,
|
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor, IClasspathInterceptor,
|
||||||
ISourceDirectoryContributor, IBuildConfigFieldContributor {
|
ISourceDirectoryContributor, IBuildConfigFieldContributor, ITaskContributor {
|
||||||
companion object {
|
companion object {
|
||||||
const val PLUGIN_NAME = "Android"
|
const val PLUGIN_NAME = "Android"
|
||||||
const val TASK_GENERATE_DEX = "generateDex"
|
const val TASK_GENERATE_DEX = "generateDex"
|
||||||
|
@ -35,20 +35,25 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
||||||
|
|
||||||
fun isAndroid(project: Project) = configurationFor(project) != null
|
fun isAndroid(project: Project) = configurationFor(project) != null
|
||||||
|
|
||||||
|
val taskContributor : TaskContributor = TaskContributor()
|
||||||
|
|
||||||
override fun apply(project: Project, context: KobaltContext) {
|
override fun apply(project: Project, context: KobaltContext) {
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
if (accept(project)) {
|
if (accept(project)) {
|
||||||
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
|
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
|
||||||
|
|
||||||
addVariantTasks(project, "generateR", runBefore = listOf("compile"),
|
taskContributor.addVariantTasks(project, context, "generateR", runBefore = listOf("compile"),
|
||||||
runTask = { taskGenerateRFile(project) })
|
runTask = { taskGenerateRFile(project) })
|
||||||
addVariantTasks(project, "generateDex", runAfter = listOf("compile"), runBefore = listOf("assemble"),
|
taskContributor.addVariantTasks(project, context, "generateDex", runAfter = listOf("compile"),
|
||||||
|
runBefore = listOf("assemble"),
|
||||||
runTask = { taskGenerateDex(project) })
|
runTask = { taskGenerateDex(project) })
|
||||||
addVariantTasks(project, "signApk", runAfter = listOf("generateDex"), runBefore = listOf("assemble"),
|
taskContributor.addVariantTasks(project, context, "signApk", runAfter = listOf("generateDex"),
|
||||||
|
runBefore = listOf("assemble"),
|
||||||
runTask = { taskSignApk(project) })
|
runTask = { taskSignApk(project) })
|
||||||
addVariantTasks(project, "install", runAfter = listOf("signApk"),
|
taskContributor.addVariantTasks(project, context, "install", runAfter = listOf("signApk"),
|
||||||
runTask = { taskInstall(project) })
|
runTask = { taskInstall(project) })
|
||||||
addVariantTasks(project, "proguard", runBefore = listOf("install"), runAfter = listOf("compile"),
|
taskContributor.addVariantTasks(project, context, "proguard", runBefore = listOf("install"),
|
||||||
|
runAfter = listOf("compile"),
|
||||||
runTask = { taskProguard(project) })
|
runTask = { taskProguard(project) })
|
||||||
}
|
}
|
||||||
context.pluginInfo.classpathContributors.add(this)
|
context.pluginInfo.classpathContributors.add(this)
|
||||||
|
@ -462,7 +467,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ITaskContributor
|
||||||
|
override fun tasksFor(context: KobaltContext): List<DynamicTask> = taskContributor.dynamicTasks
|
||||||
}
|
}
|
||||||
|
|
||||||
class AndroidConfig(val project: Project,
|
class AndroidConfig(val project: Project,
|
||||||
|
|
|
@ -34,7 +34,8 @@ fun Project.application(init: ApplicationConfig.() -> Unit) {
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
val dependencyManager: DependencyManager) : ConfigPlugin<ApplicationConfig>(), IRunnerContributor {
|
val dependencyManager: DependencyManager)
|
||||||
|
: ConfigPlugin<ApplicationConfig>(), IRunnerContributor, ITaskContributor {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val PLUGIN_NAME = "Application"
|
const val PLUGIN_NAME = "Application"
|
||||||
|
@ -42,9 +43,12 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
|
|
||||||
override val name = PLUGIN_NAME
|
override val name = PLUGIN_NAME
|
||||||
|
|
||||||
|
val taskContributor : TaskContributor = TaskContributor()
|
||||||
|
|
||||||
override fun apply(project: Project, context: KobaltContext) {
|
override fun apply(project: Project, context: KobaltContext) {
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
addVariantTasks(project, "run", runAfter = listOf("install"), runTask = { taskRun(project) })
|
taskContributor.addVariantTasks(project, context, "run", runAfter = listOf("install"),
|
||||||
|
runTask = { taskRun(project) })
|
||||||
}
|
}
|
||||||
|
|
||||||
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("install"))
|
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("install"))
|
||||||
|
@ -121,5 +125,7 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
return TaskResult(exitCode == 0)
|
return TaskResult(exitCode == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ITaskContributor
|
||||||
|
override fun tasksFor(context: KobaltContext): List<DynamicTask> = taskContributor.dynamicTasks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,8 @@ import javax.inject.Singleton
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class PackagingPlugin @Inject constructor(val dependencyManager : DependencyManager,
|
class PackagingPlugin @Inject constructor(val dependencyManager : DependencyManager,
|
||||||
val executors: KobaltExecutors, val localRepo: LocalRepo) : ConfigPlugin<InstallConfig>() {
|
val executors: KobaltExecutors, val localRepo: LocalRepo)
|
||||||
|
: ConfigPlugin<InstallConfig>(), ITaskContributor {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val PLUGIN_NAME = "Packaging"
|
const val PLUGIN_NAME = "Packaging"
|
||||||
|
@ -51,10 +52,13 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
||||||
|
|
||||||
private val packages = arrayListOf<PackageConfig>()
|
private val packages = arrayListOf<PackageConfig>()
|
||||||
|
|
||||||
|
val taskContributor : TaskContributor = TaskContributor()
|
||||||
|
|
||||||
override fun apply(project: Project, context: KobaltContext) {
|
override fun apply(project: Project, context: KobaltContext) {
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
project.projectProperties.put(LIBS_DIR, libsDir(project))
|
project.projectProperties.put(LIBS_DIR, libsDir(project))
|
||||||
addVariantTasks(project, "assemble", runAfter = listOf("compile"), runTask = { taskAssemble(project) })
|
taskContributor.addVariantTasks(project, context, "assemble", runAfter = listOf("compile"),
|
||||||
|
runTask = { taskAssemble(project) })
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs").path
|
private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs").path
|
||||||
|
@ -269,6 +273,9 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
||||||
|
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ITaskContributor
|
||||||
|
override fun tasksFor(context: KobaltContext): List<DynamicTask> = taskContributor.dynamicTasks
|
||||||
}
|
}
|
||||||
|
|
||||||
@Directive
|
@Directive
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue