Fixed forEach loops.

Added Fail.NORMAL.
This commit is contained in:
Erik C. Thauvin 2016-07-11 22:27:05 -07:00
parent e39200912b
commit cca65b73c1
7 changed files with 52 additions and 37 deletions

1
.gitignore vendored
View file

@ -27,3 +27,4 @@
Thumbs.db Thumbs.db
ehthumbs.db ehthumbs.db
kobaltBuild kobaltBuild
/example/libs/

2
.idea/kobalt.xml generated
View file

@ -5,7 +5,7 @@
<KobaltProjectSettings> <KobaltProjectSettings>
<option name="autoDownloadKobalt" value="true" /> <option name="autoDownloadKobalt" value="true" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="kobaltHome" value="$USER_HOME$/.kobalt/wrapper/dist/kobalt-0.843" /> <option name="kobaltHome" value="$USER_HOME$/.kobalt/wrapper/dist/kobalt-0.846" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

4
.idea/misc.xml generated
View file

@ -40,6 +40,10 @@
</component> </component>
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<entry_points version="2.0" /> <entry_points version="2.0" />
<list size="2">
<item index="0" class="java.lang.String" itemvalue="com.beust.kobalt.api.annotation.Directive" />
<item index="1" class="java.lang.String" itemvalue="com.beust.kobalt.api.annotation.Task" />
</list>
</component> </component>
<component name="MavenImportPreferences"> <component name="MavenImportPreferences">
<option name="generalSettings"> <option name="generalSettings">

3
.idea/vcs.xml generated
View file

@ -2,5 +2,8 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component> </component>
</project> </project>

View file

@ -37,7 +37,7 @@ val p = project {
} }
exec { exec {
commandLine(args = listOf("ls", "-- a"), dir = "../libs", fail = setOf(Fail.STDERR, Fail.EXIT)) commandLine(args = listOf("ls", "-l"), dir = "../libs", fail = setOf(Fail.NORMAL))
commandLine(listOf("cmd", "/c", "echo", "Test"), os = listOf("Win"), fail = setOf(Fail.STDERR, Fail.EXIT)) commandLine(listOf("cmd", "/c", "echo", "Test"), os = listOf("Win", "Windows"), fail = setOf(Fail.NORMAL))
} }
} }

View file

@ -1 +1 @@
kobalt.version=0.845 kobalt.version=0.846

View file

@ -48,7 +48,6 @@ class ExecPlugin : BasePlugin(), ITaskContributor {
return emptyList() return emptyList()
} }
companion object { companion object {
const val NAME: String = "Exec" const val NAME: String = "Exec"
} }
@ -63,54 +62,63 @@ class ExecPlugin : BasePlugin(), ITaskContributor {
private fun executeCommands(project: Project): TaskResult { private fun executeCommands(project: Project): TaskResult {
var success = true var success = true
val config = configs[project.name] val config = configs[project.name]
val errorMessage = StringBuilder()
if (config != null) { if (config != null) {
config.commandLines.forEach { for ((args, dir, os, fail) in config.commandLines) {
val dir = if (it.dir.isNullOrBlank()) project.directory else it.dir val wrkDir = File(if (dir.isNullOrBlank()) project.directory else dir)
val loc = File(dir) if (wrkDir.isDirectory) {
if (loc.isDirectory()) { var execute = (os.size == 0)
var execute = (it.os.size == 0)
if (!execute) { if (!execute) {
val curOs: String = System.getProperty("os.name") val curOs: String = System.getProperty("os.name")
it.os.forEach os@ { for (name in os) {
execute = curOs.startsWith(it, true) execute = curOs.startsWith(name, true)
if (execute) return@os if (execute) break
} }
} }
if (execute) { if (execute) {
log(2, "> " + it.args.joinToString(" ")) log(2, "> " + args.joinToString(" "))
val pb = ProcessBuilder().command(it.args.toList()) val pb = ProcessBuilder().command(args.toList())
pb.directory(loc) pb.directory(wrkDir)
val proc = pb.start() val proc = pb.start()
val err = proc.waitFor(30, TimeUnit.SECONDS) val err = proc.waitFor(30, TimeUnit.SECONDS)
val stdout = if (proc.inputStream.available() > 0) fromStream(proc.inputStream) else emptyList() val stdout = if (proc.inputStream.available() > 0) fromStream(proc.inputStream) else emptyList()
val stderr = if (proc.errorStream.available() > 0) fromStream(proc.errorStream) else emptyList() val stderr = if (proc.errorStream.available() > 0) fromStream(proc.errorStream) else emptyList()
val cmdInfo = "Program \"" + args.joinToString(" ") + "\" (in directory \"${wrkDir.path}\"): "
val errMsg = "Program \"" + it.args.joinToString(" ") + "\" (in directory \"$dir\"): "
if (err == false) { if (err == false) {
error(errMsg + "TIMEOUT") errorMessage.append(cmdInfo).append("TIMEOUT")
} else if (it.fail.isNotEmpty()) { success = false
val all = it.fail.contains(Fail.ALL) } else if (fail.isNotEmpty()) {
val output = it.fail.contains(Fail.OUTPUT) val all = fail.contains(Fail.ALL)
if ((all || it.fail.contains(Fail.EXIT)) && proc.exitValue() > 0) { val output = fail.contains(Fail.OUTPUT)
error(errMsg + "EXIT ${proc.exitValue()}") if ((all || fail.contains(Fail.EXIT) || fail.contains(Fail.NORMAL)) && proc.exitValue() > 0) {
} else if ((all || output || it.fail.contains(Fail.STDERR)) && stderr.isNotEmpty()) { errorMessage.append(cmdInfo).append("EXIT ${proc.exitValue()}")
error(errMsg + "STDERR, " + stderr[0]) if (stderr.isNotEmpty()) errorMessage.append(", STDERR: ").append(stderr[0])
} else if ((all || output || it.fail.contains(Fail.STDOUT)) && stdout.isNotEmpty()) { success = false
error(errMsg + "STDOUT, " + stdout[0]) } else if ((all || output || fail.contains(Fail.STDERR) || fail.contains(Fail.NORMAL))
&& stderr.isNotEmpty()) {
errorMessage.append(cmdInfo).append("STDERR, ").append(stderr[0])
success = false
} else if ((all || output || fail.contains(Fail.STDOUT)) && stdout.isNotEmpty()) {
errorMessage.append(cmdInfo).append("STDOUT, ").append(stdout[0])
success = false
} }
} }
success = true
} }
} else { } else {
error("Invalid directory: ${loc.canonicalPath}") errorMessage.append("Invalid working directory: \"${wrkDir.canonicalPath}\"")
success = false
} }
if (!success) break
} }
} }
return TaskResult(success) //@TODO until cedric fixes it.
if (!success) error(errorMessage)
return TaskResult(success, errorMessage.toString())
} }
private val configs = hashMapOf<String, ExecConfig>() private val configs = hashMapOf<String, ExecConfig>()
@ -135,7 +143,7 @@ class ExecPlugin : BasePlugin(), ITaskContributor {
} }
enum class Fail() { enum class Fail() {
ALL, STDERR, STDOUT, OUTPUT, EXIT ALL, NORMAL, STDERR, STDOUT, OUTPUT, EXIT
} }
data class CommandLine(var args: List<String> = emptyList(), var dir: String = "", data class CommandLine(var args: List<String> = emptyList(), var dir: String = "",
@ -144,9 +152,8 @@ data class CommandLine(var args: List<String> = emptyList(), var dir: String = "
data class ExecConfig(val project: Project) { data class ExecConfig(val project: Project) {
val commandLines = arrayListOf<CommandLine>() val commandLines = arrayListOf<CommandLine>()
@Directive @Directive fun commandLine(args: List<String> = emptyList(), dir: String = "", os: List<String> = emptyList(),
public fun commandLine(args: List<String> = emptyList(), dir: String = "", os: List<String> = emptyList(), fail: Set<Fail> = emptySet()) {
fail: Set<Fail> = emptySet()) {
if (args.size > 0) commandLines.add(CommandLine(args, dir, os, fail)) if (args.size > 0) commandLines.add(CommandLine(args, dir, os, fail))
} }
} }