From 4556e93d2f12cd211376dcdbe3dde14433d11849 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 11 Oct 2015 19:47:37 -0700 Subject: [PATCH] More plug-in doc. --- plug-in-development/index.html | 92 +++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/plug-in-development/index.html b/plug-in-development/index.html index a6bd002..019518d 100644 --- a/plug-in-development/index.html +++ b/plug-in-development/index.html @@ -351,11 +351,15 @@ myConfig(project) { public fun myConfig(project: Project, init: Info.() -> Unit) : Info { val info = Info() info.init() - (Plugins.getPlugin("my-plug-in") as MyPlugin).addInfo(info) + (Kobalt.findPlugin("my-plug-in") as MyPlugin).info = info return info } - +

+ Obviously, you can choose any kind of API to communicate between the directive and its plug-in. In the code + above, I chose to directly overrid the entire Info field, but you could instead choose to call + a function, just set one boolean instead of the whole object, etc... +

Tasks

Tasks are provided by plug-ins and can be invoked from the command line, e.g. ./gradlew assemble. There are two kinds of tasks: static and dynamic. @@ -418,11 +422,95 @@ fun lineCount(project: Project): TaskResult { Note that there is no alwaysRunBefore annotation since runBefore achieves the same functionality.

+

+ Here are a few different scenarios to illustrate how the three attributes work for the task exampleTask: +

+

+Result of the command ./kobaltw --dryRun compile +

+ + + + + + + + + + + + + + + + + + +
Configuration for exampleTaskResult
runBefore = "compile" +
kobalt-line-count:clean
+kobalt-line-count:exampleTask
+kobalt-line-count:compile
+
runAfter = "compile" +
kobalt-line-count:clean
+kobalt-line-count:compile
+
alwaysRunAfter = "compile" +
kobalt-line-count:clean
+kobalt-line-count:compile
+kobalt-line-count:exampleTask
+

Dynamic tasks

+

+ Dynamic tasks are useful when you want your plug-in to generate one or several tasks that depend on + some other runtime information (therefore, you can't declare a method and put a @Task + annotation on it). Here is the simplest dynamic task you can create in your plug-in class: +

+
+override fun apply(project: Project, context: KobaltContext) {
+    println("*** Adding dynamic task")
+    addTask(project, "dynamicTask") {
+        println("Dynamic task")
+        TaskResult()
+    }
+}
+
+

+ Like a regular task method, the closure you pass to addTask() has to return a TaskResult + object to indicate whether it succeeded or failed. You can + then see your dynamic task in the list of tasks and run it directly: +

+
+$ ./kobaltw --tasks
+  ===== kobalt-line-count =====
+    dynamicTask
+    lineCount           Count the lines
+$ ./kobaltw dynamicTask
+Dynamic task
+
+

+ The addTask() method lets you specify any attribute you can specify on the @Task + annotation: description, runBefore, etc... For example, here is how we would + specify that this task should always run after compile: +

+
+addTask(project, "dynamicTask", alwaysRunAfter = listOf("compile")) {
+    println("Dynamic task")
+    TaskResult()
+}
+
+

+ Let's test it: +

+ +
+$ ./kobaltw --dryRun compile
+kobalt-line-count:clean
+kobalt-line-count:compile
+kobalt-line-count:exampleTask
+