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"
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
exec {
commandLine(listOf("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(listOf("/bin/sh", "-c", "ps aux | grep tomcat"), fail = setOf(Fail.EXIT))
commandLine("cmd", "/c", "stop.bat", dir = "../tomcat/bin", os = setOf(Os.WINDOWS))
commandLine("./stop.sh", dir = "../tomcat/bin", os = setOf(Os.MAC, Os.LINUX))
commandLine("/bin/sh", "-c", "ps aux | grep tomcat", fail = setOf(Fail.EXIT))
}
```
@ -45,12 +45,13 @@ exec {
### `args`
The full command line including the executable and all parameters.
The full command line arguments including the executable and all parameters.
```kotlin
exec {
commandLine(listOf("ls", "-l"))
commandLine(args = listOf("cmd", "/c", "dir /Q"))
commandLine(args="ls")
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
exec {
commandLine(listOf("cmd", "/c", "stop.bat"), dir = "../tomcat/bin")
commandLine("cmd", "/c", "stop.bat", dir = "../tomcat/bin")
commandLine("./stop.sh", dir = "../tomcat/bin")
}
```
@ -85,8 +86,8 @@ Name | Operating System
```kotlin
exec {
commandLine(listOf("cmd", "/c", "stop.cmd"), os = setOf(Os.WINDOWS))
commandLine(listOf("./stop.sh"), os = setOf(Os.LINUX, Os.MAC))
commandLine("cmd", "/c", "stop.cmd", os = setOf(Os.WINDOWS))
commandLine("./stop.sh", os = setOf(Os.LINUX, Os.MAC))
}
```
@ -110,8 +111,41 @@ Name | Failure When
```kotlin
exec {
commandLine(listOf("cmd", "/c", "stop.bat"), fail = setOf(Fail.EXIT))
commandLine(listOf("./stop.sh"), fail = setOf(Fail.EXIT, Fail.STDOUT))
commandLine("cmd", "/c", "stop.bat", fail = setOf(Fail.EXIT))
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
exec {
commandLine(listOf("/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("/bin/sh", "-c", "./stop.sh 2> error.txt", os = setOf(Os.LINUX))
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 net.thauvin.erik.kobalt.plugin.exec.*
// ./kobaltw exec echo ps --log 2
// ./kobaltw exec --log 2
// ./kobaltw example:exec --log 2
// ./kobaltw example2:exec --log 2
// ./kobaltw echo --log 2
// ./kobaltw ps --log 2
val bs = buildScript {
//repos(file("K:/maven/repository"))
plugins("net.thauvin.erik:kobalt-exec:")
repos(file("K:/maven/repository"))
plugins("net.thauvin.erik:kobalt-exec:0.6.6")
}
val example = project {
@ -29,20 +30,23 @@ val example = project {
}
exec {
commandLine(listOf("echo", "Test Example 1"), os = setOf(Os.LINUX))
commandLine(listOf("cmd", "/c", "echo", "Test Example 1"), os = setOf(Os.WINDOWS))
commandLine(args = listOf("ls", "-l"), dir = "../libs", os = setOf(Os.LINUX))
commandLine(args = listOf("cmd", "/c", "dir /Q"), dir = "../libs", os = setOf(Os.WINDOWS))
commandLine("echo", "Test Example 1", os = setOf(Os.LINUX))
commandLine("cmd", "/c", "echo", "Test Example 1", os = setOf(Os.WINDOWS))
commandLine("ls", "-l", dir = "../libs", os = setOf(Os.LINUX))
commandLine("cmd", "/c", "dir /Q", dir = "../libs", os = setOf(Os.WINDOWS))
}
}
val example2 = project {
name = "example2"
exec {
commandLine(listOf("cmd", "/c", "echo", "Test Example 2"), os = setOf(Os.WINDOWS))
commandLine(listOf("echo", "Test example 2"), os = setOf(Os.LINUX))
//commandLine(listOf("cmd", "/c", "tasklist | find \"cmd.exe\""), os = setOf(Os.WINDOWS), fail = setOf(Fail.NONE))
commandLine(listOf("/bin/sh", "-c", "ps aux | grep bash"), os = setOf(Os.LINUX))
taskName = "echo"
dependsOn = listOf("exec", "run")
commandLine("cmd", "/c", "echo", "Test Example 2", os = setOf(Os.WINDOWS))
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.publish.autoGitTag
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 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 {
repos(file("K:/maven/repository"))
@ -19,7 +25,7 @@ val p = project {
name = "kobalt-exec"
group = "net.thauvin.erik"
artifactId = name
version = "0.6.5"
version = "0.6.6"
pom = Model().apply {
description = "Command Line Execution plug-in for the Kobalt build system."
@ -41,7 +47,7 @@ val p = project {
}
dependencies {
compile("com.beust:$kobaltDependency:")
compileOnly("com.beust:$kobaltDependency:")
}
dependenciesTest {

View file

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