Added taskName, dependsOn.

Changed commandLine(args) from List to vararg.
This commit is contained in:
Erik C. Thauvin 2017-04-28 00:26:04 -07:00
parent d990c6d598
commit 395fa127b9
4 changed files with 97 additions and 46 deletions

View file

@ -17,7 +17,7 @@ val p = project {
name = "example" name = "example"
exec { exec {
commandLine(listOf("echo", "Hello, World!")) commandLine("echo", "Hello, World!")
} }
} }
``` ```
@ -35,9 +35,9 @@ The `commandLine` directive is used to execute command line(s) during the build
```kotlin ```kotlin
exec { exec {
commandLine(listOf("cmd", "/c", "stop.bat"), dir = "../tomcat/bin", os = setOf(Os.WINDOWS)) commandLine("cmd", "/c", "stop.bat", dir = "../tomcat/bin", os = setOf(Os.WINDOWS))
commandLine(listOf("./stop.sh"), dir = "../tomcat/bin", os = setOf(Os.MAC, Os.LINUX)) commandLine("./stop.sh", dir = "../tomcat/bin", os = setOf(Os.MAC, Os.LINUX))
commandLine(listOf("/bin/sh", "-c", "ps aux | grep tomcat"), fail = setOf(Fail.EXIT)) commandLine("/bin/sh", "-c", "ps aux | grep tomcat", fail = setOf(Fail.EXIT))
} }
``` ```
@ -45,12 +45,13 @@ exec {
### `args` ### `args`
The full command line including the executable and all parameters. The full command line arguments including the executable and all parameters.
```kotlin ```kotlin
exec { exec {
commandLine(listOf("ls", "-l")) commandLine(args="ls")
commandLine(args = listOf("cmd", "/c", "dir /Q")) commandLine("ls", "-l")
commandLine("cmd", "/c", "dir /Q")
} }
``` ```
@ -60,7 +61,7 @@ The working directory in which the command should be executed. Defaults to the p
```kotlin ```kotlin
exec { exec {
commandLine(listOf("cmd", "/c", "stop.bat"), dir = "../tomcat/bin") commandLine("cmd", "/c", "stop.bat", dir = "../tomcat/bin")
commandLine("./stop.sh", dir = "../tomcat/bin") commandLine("./stop.sh", dir = "../tomcat/bin")
} }
``` ```
@ -85,8 +86,8 @@ Name | Operating System
```kotlin ```kotlin
exec { exec {
commandLine(listOf("cmd", "/c", "stop.cmd"), os = setOf(Os.WINDOWS)) commandLine("cmd", "/c", "stop.cmd", os = setOf(Os.WINDOWS))
commandLine(listOf("./stop.sh"), os = setOf(Os.LINUX, Os.MAC)) commandLine("./stop.sh", os = setOf(Os.LINUX, Os.MAC))
} }
``` ```
@ -110,8 +111,41 @@ Name | Failure When
```kotlin ```kotlin
exec { exec {
commandLine(listOf("cmd", "/c", "stop.bat"), fail = setOf(Fail.EXIT)) commandLine("cmd", "/c", "stop.bat", fail = setOf(Fail.EXIT))
commandLine(listOf("./stop.sh"), fail = setOf(Fail.EXIT, Fail.STDOUT)) commandLine("./stop.sh", fail = setOf(Fail.EXIT, Fail.STDOUT))
}
```
## taskName
Additionally, you can specify a task name to easily identify multiple `exec` directives.
```kotlin
exec {
taskName = "start"
commandLine("./start.sh", os = setOf(Os.LINUX, Os.MAC))
}
exec {
taskName = "stop"
commandLine("./stop.sh", os = setOf(Os.LINUX, Os.MAC))
}
```
```shell
./kobaltw start
./kobaltw stop
```
## dependsOn
By default the `exec` task depends on `assemble`, use the `dependsOn` parameter to change the dependencies:
```kotlin
exec {
dependsOn = listOf("assemble", "run")
commandLine("cmd", "/c", "start.bat", fail = setOf(Fail.EXIT))
} }
``` ```
@ -125,8 +159,8 @@ You could also redirect the error stream to a file:
```kotlin ```kotlin
exec { exec {
commandLine(listOf("/bin/sh", "-c", "./stop.sh 2> error.txt"), os = setOf(Os.LINUX)) commandLine("/bin/sh", "-c", "./stop.sh 2> error.txt", os = setOf(Os.LINUX))
commandLine(listOf("cmd", "/c", "stop.bat 2> error.txt"), os = setOf(Os.WINDOWS)) commandLine("cmd", "/c", "stop.bat 2> error.txt", os = setOf(Os.WINDOWS))
} }
``` ```

View file

@ -3,13 +3,14 @@ import com.beust.kobalt.plugin.application.*
import com.beust.kobalt.plugin.packaging.* import com.beust.kobalt.plugin.packaging.*
import net.thauvin.erik.kobalt.plugin.exec.* import net.thauvin.erik.kobalt.plugin.exec.*
// ./kobaltw exec echo ps --log 2
// ./kobaltw exec --log 2 // ./kobaltw exec --log 2
// ./kobaltw example:exec --log 2 // ./kobaltw echo --log 2
// ./kobaltw example2:exec --log 2 // ./kobaltw ps --log 2
val bs = buildScript { val bs = buildScript {
//repos(file("K:/maven/repository")) repos(file("K:/maven/repository"))
plugins("net.thauvin.erik:kobalt-exec:") plugins("net.thauvin.erik:kobalt-exec:0.6.6")
} }
val example = project { val example = project {
@ -29,20 +30,23 @@ val example = project {
} }
exec { exec {
commandLine(listOf("echo", "Test Example 1"), os = setOf(Os.LINUX)) commandLine("echo", "Test Example 1", os = setOf(Os.LINUX))
commandLine(listOf("cmd", "/c", "echo", "Test Example 1"), os = setOf(Os.WINDOWS)) commandLine("cmd", "/c", "echo", "Test Example 1", os = setOf(Os.WINDOWS))
commandLine(args = listOf("ls", "-l"), dir = "../libs", os = setOf(Os.LINUX)) commandLine("ls", "-l", dir = "../libs", os = setOf(Os.LINUX))
commandLine(args = listOf("cmd", "/c", "dir /Q"), dir = "../libs", os = setOf(Os.WINDOWS)) commandLine("cmd", "/c", "dir /Q", dir = "../libs", os = setOf(Os.WINDOWS))
} }
}
val example2 = project {
name = "example2"
exec { exec {
commandLine(listOf("cmd", "/c", "echo", "Test Example 2"), os = setOf(Os.WINDOWS)) taskName = "echo"
commandLine(listOf("echo", "Test example 2"), os = setOf(Os.LINUX)) dependsOn = listOf("exec", "run")
//commandLine(listOf("cmd", "/c", "tasklist | find \"cmd.exe\""), os = setOf(Os.WINDOWS), fail = setOf(Fail.NONE)) commandLine("cmd", "/c", "echo", "Test Example 2", os = setOf(Os.WINDOWS))
commandLine(listOf("/bin/sh", "-c", "ps aux | grep bash"), os = setOf(Os.LINUX)) commandLine("echo", "Test example 2", os = setOf(Os.LINUX))
}
exec {
taskName = "ps"
dependsOn = listOf() // no dependencies
commandLine("cmd", "/c", "tasklist | find \"cmd.exe\"", os = setOf(Os.WINDOWS), fail = setOf(Fail.NONE))
commandLine("/bin/sh", "-c", "ps aux | grep bash", os = setOf(Os.LINUX))
} }
} }

View file

@ -1,10 +1,16 @@
import com.beust.kobalt.* import com.beust.kobalt.buildScript
import com.beust.kobalt.file
import com.beust.kobalt.plugin.packaging.assemble import com.beust.kobalt.plugin.packaging.assemble
import com.beust.kobalt.plugin.publish.autoGitTag import com.beust.kobalt.plugin.publish.autoGitTag
import com.beust.kobalt.plugin.publish.bintray import com.beust.kobalt.plugin.publish.bintray
import com.beust.kobalt.profile
import com.beust.kobalt.project
import net.thauvin.erik.kobalt.plugin.versioneye.versionEye import net.thauvin.erik.kobalt.plugin.versioneye.versionEye
import org.apache.maven.model.* import org.apache.maven.model.Developer
import org.apache.maven.model.License
import org.apache.maven.model.Model
import org.apache.maven.model.Scm
val bs = buildScript { val bs = buildScript {
repos(file("K:/maven/repository")) repos(file("K:/maven/repository"))
@ -19,7 +25,7 @@ val p = project {
name = "kobalt-exec" name = "kobalt-exec"
group = "net.thauvin.erik" group = "net.thauvin.erik"
artifactId = name artifactId = name
version = "0.6.5" version = "0.6.6"
pom = Model().apply { pom = Model().apply {
description = "Command Line Execution plug-in for the Kobalt build system." description = "Command Line Execution plug-in for the Kobalt build system."
@ -41,7 +47,7 @@ val p = project {
} }
dependencies { dependencies {
compile("com.beust:$kobaltDependency:") compileOnly("com.beust:$kobaltDependency:")
} }
dependenciesTest { dependenciesTest {

View file

@ -44,8 +44,8 @@ import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@Singleton @Singleton
class ExecPlugin @Inject constructor(val configActor: ConfigActor<ExecConfig>) : class ExecPlugin @Inject constructor(val taskContributor: TaskContributor, val configActor: ConfigsActor<ExecConfig>) :
BasePlugin(), ITaskContributor, IConfigActor<ExecConfig> by configActor { BasePlugin(), ITaskContributor, IConfigsActor<ExecConfig> by configActor {
// ITaskContributor // ITaskContributor
override fun tasksFor(project: Project, context: KobaltContext): List<DynamicTask> { override fun tasksFor(project: Project, context: KobaltContext): List<DynamicTask> {
return emptyList() return emptyList()
@ -57,14 +57,19 @@ class ExecPlugin @Inject constructor(val configActor: ConfigActor<ExecConfig>) :
override val name = NAME override val name = NAME
@Suppress("unused") override fun apply(project: Project, context: KobaltContext) {
@Task(name = "exec", description = "Execute a command line process.") configurationFor(project)?.let { configs ->
fun taskExec(project: Project): TaskResult { configs.forEach { config ->
configurationFor(project)?.let { config -> taskManager.addTask(this, project, config.taskName,
return executeCommands(project, config) description = "Execute a command line process.",
group = "Other",
dependsOn = config.dependsOn,
task = { executeCommands(project, config) })
taskContributor.addVariantTasks(this, project, context, config.taskName,
dependsOn = config.dependsOn,
runTask = { executeCommands(project, config) })
}
} }
return TaskResult()
} }
private fun matchOs(os: Os): Boolean { private fun matchOs(os: Os): Boolean {
@ -187,12 +192,14 @@ data class CommandLine(var args: List<String> = emptyList(), var dir: String = "
@Directive @Directive
class ExecConfig { class ExecConfig {
var taskName: String = "exec"
val commandLines = arrayListOf<CommandLine>() val commandLines = arrayListOf<CommandLine>()
var dependsOn = listOf("assemble")
@Suppress("unused") @Suppress("unused")
fun commandLine(args: List<String> = emptyList(), dir: String = "", os: Set<Os> = emptySet(), fun commandLine(vararg args: String, dir: String = "", os: Set<Os> = emptySet(),
fail: Set<Fail> = setOf(Fail.NORMAL)) { fail: Set<Fail> = setOf(Fail.NORMAL)) {
if (args.isNotEmpty()) commandLines.add(CommandLine(args, dir, os, fail)) if (args.isNotEmpty()) commandLines.add(CommandLine(listOf(*args), dir, os, fail))
} }
} }