Slackbot
09/08/2023, 11:38 AMVampire
09/08/2023, 12:03 PMscript.sh
or it calls something that is not found.
Also I don't think you can display the streams that way, especially when the command failed as your doLast
action will then not be found.
You should probably either set the property to ignore the exit code and then fail after outputting the streams in your doLast
action, or right away do not use an Exec
task, but use exec { ... }
instead. But also there you will need to configure to ignore the exit code and then fail after outputting the streams.Vampire
09/08/2023, 12:04 PMGuilherme Delgado
09/08/2023, 12:08 PMMaybe your working directory is not as expected to find theShouldn’t the same behavior apply to the terminal? I can run my script via the terminal in the same directory. Does Gradle have different access policies?or it calls something that is not found.script.sh
Guilherme Delgado
09/08/2023, 12:17 PMGuilherme Delgado
09/08/2023, 12:23 PMOh wow I could make it work by this:
tasks.register("runBashScript") {
doFirst {
println("> Executing runBashScript task")
}
val output = listOf("zsh", "-c", "./script.sh").runCommand(project.projectDir)
doLast {
println("> Output: $output")
}
}
fun List<String>.runCommand(workingDir: File = File("."), timeoutAmount: Long = 60, timeoutUnit: TimeUnit = TimeUnit.SECONDS): String {
return try {
runCatching {
ProcessBuilder(this)
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.also { it.waitFor(timeoutAmount, timeoutUnit) }
.inputStream.bufferedReader().readText()
}.onFailure { it.printStackTrace() }.getOrThrow()
} catch (e: Exception) {
e.message ?: e.cause?.message ?: "Exception occurred, but no message found"
}
}
I still cannot see the output from the script, but it gives me success 🤔Vampire
09/08/2023, 12:23 PMFile
constructor with a relative path is inherently broken and can fail any time.Guilherme Delgado
09/08/2023, 12:24 PM./gradlew runBashScript
> Task :runBashScript
> Executing runBashScript task
> Output:
BUILD SUCCESSFUL in 2s
Guilherme Delgado
09/08/2023, 12:24 PMproject.projectDir
Vampire
09/08/2023, 12:24 PMExec
or exec
Vampire
09/08/2023, 12:25 PMGuilherme Delgado
09/08/2023, 12:26 PMtasks.register<Exec>("script") {
workingDir = project.projectDir
}
but for some reason it wasn’t working. Then I removed the “exec” type and used my helper function and now I have BUILD SUCCESSFUL.Guilherme Delgado
09/08/2023, 12:26 PMignore the exit codeI was trying to find an Exec property
Vampire
09/08/2023, 12:28 PM_isIgnoreExitValue_ = true
Vampire
09/08/2023, 12:30 PMexec { ... }
instead of an Exec
task to then access the exit value to actually make it fail after outputting the streams. Iirc you cannot do this with an Exec
task.Vampire
09/08/2023, 12:32 PMtasks.register("script") {
doLast {
val result = exec {
workingDir(layout.projectDirectory)
// ...
isIgnoreExitValue = true
}
// output streams
result.assertNormalExitValue()
}
}
Guilherme Delgado
09/08/2023, 12:37 PMtasks.register("runBashScript") {
doLast {
println("> Executing runBashScript task")
val result = exec {
workingDir(layout.projectDirectory)
isIgnoreExitValue = true
commandLine("bash", "-c", "./script.sh")
}
result.assertNormalExitValue()
println(result.exitValue)
}
}
Vampire
09/08/2023, 12:38 PMassertNormalExitValue
throws on non-0, so your println will only reached on success, so it will print either 0 or not be called.
The assert should be last, as it actually fails the task immediately.Vampire
09/08/2023, 12:38 PMVampire
09/08/2023, 12:39 PMGuilherme Delgado
09/08/2023, 12:46 PMGuilherme Delgado
09/08/2023, 12:49 PMGuilherme Delgado
09/08/2023, 12:49 PMtasks.register("runBashScript") {
doFirst {
println("> Executing runBashScript task")
}
val process = ProcessBuilder("fuuuuuuuuulPath/script.sh")
.directory(layout.projectDirectory.asFile)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
val output = process.inputStream.bufferedReader().readText()
doLast {
val exitCode = process.waitFor()
if (exitCode == 0) {
println("> Script executed successfully: \n $output")
} else {
println("> Script execution failed with exit code $exitCode.")
}
}
}
Guilherme Delgado
09/08/2023, 12:50 PMGuilherme Delgado
09/08/2023, 12:50 PM.directory(layout.projectDirectory.asFile)
Vampire
09/08/2023, 12:53 PMtasks.register("runBashScript") {
doLast {
println("> Executing runBashScript task")
val out = ByteArrayOutputStream()
val err = ByteArrayOutputStream()
val result = exec {
standardOutput = out
errorOutput = err
workingDir(layout.projectDirectory)
isIgnoreExitValue = true
commandLine("bash", "-c", "./script.sh")
}
println("Command Output:")
println(out.toString())
println("Command Error:")
println(err.toString())
result.assertNormalExitValue()
}
}
Vampire
09/08/2023, 12:54 PMVampire
09/08/2023, 12:54 PMVampire
09/08/2023, 12:55 PMtasks.register<Exec>("runBashScript") {
doFirst {
println("> Executing runBashScript task")
}
workingDir(layout.projectDirectory)
commandLine("bash", "-c", "./script.sh")
}
Vampire
09/08/2023, 12:55 PMVampire
09/08/2023, 12:56 PMGuilherme Delgado
09/08/2023, 1:05 PMGuilherme Delgado
09/08/2023, 1:05 PM../script.sh
instead of ./script.sh
👀 🤦 😞Vampire
09/08/2023, 1:42 PMGuilherme Delgado
09/08/2023, 1:44 PMGuilherme Delgado
09/08/2023, 1:45 PM