Skip to content

Commit

Permalink
Fixed sporadic errors while executing a shell command (#14)
Browse files Browse the repository at this point in the history
* Fixed sporadic errors while executing a shell command
Added shell commands logging

* Removed empty params in System.execute()

Co-authored-by: arturm <arturm@hexagontech.co.il>
  • Loading branch information
amatsegor and arturm-hexagon authored Feb 21, 2022
1 parent 5c745d5 commit b213576
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CocoapodsDiagnostic : Diagnostic("Cocoapods") {

messages.addSuccess("${cocoapodsGenerate.name} (${cocoapodsGenerate.version})")

val locale = System.execute("/usr/bin/locale", "-k", "LC_CTYPE", "") //trailing empty arg is required
val locale = System.execute("/usr/bin/locale", "-k", "LC_CTYPE")
if (locale.output == null || !locale.output.contains("UTF-8")) {
val hint = """
Consider adding the following to ${System.getShell()?.profile ?: "shell profile"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class JavaDiagnostic : Diagnostic("Java") {
var javaLocation = System.execute("which", "java").output
val javaVersionCmd = System.execute("java", "-version")
val javaVersion = if (javaVersionCmd.code == 0) javaVersionCmd.error?.lineSequence()?.firstOrNull() else null
//java_home requires empty argument, returns empty response otherwise
val systemJavaHome = System.execute("/usr/libexec/java_home", "").output
val systemJavaHome = System.execute("/usr/libexec/java_home").output
val javaHome = System.getEnvVar("JAVA_HOME")
if (javaLocation == "/usr/bin/java") {
javaLocation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ expect fun System.getHomeDir(): String
expect fun System.getEnvVar(name: String): String?
expect fun System.fileExists(path: String): Boolean
expect fun System.readFile(path: String): String?
expect fun System.execute(command: String, vararg args: String): ProcessResult
expect fun System.execute(command: String, vararg args: String, verbose: Boolean = false): ProcessResult
expect fun System.findAppsPathsInDirectory(prefix: String, directory: String, recursively: Boolean = false): List<String>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ actual fun System.readFile(path: String): String? = memScoped {
readFd(fd)
}

actual fun System.execute(command: String, vararg args: String): ProcessResult = memScoped {
actual fun System.execute(command: String, vararg args: String, verbose: Boolean): ProcessResult = memScoped {
val readEnd = 0
val writeEnd = 1

Expand Down Expand Up @@ -58,8 +58,8 @@ actual fun System.execute(command: String, vararg args: String): ProcessResult =
close(errorPipe[writeEnd])

//execute command
val newArgs = listOf(command) + args
val result = execvp(command, newArgs.map { it.cstr.ptr }.toCValues())
val newArgs = listOf(command) + args + null
val result = execvp(command, newArgs.map { it?.cstr?.ptr }.toCValues())
exit(result)
}

Expand All @@ -83,6 +83,26 @@ actual fun System.execute(command: String, vararg args: String): ProcessResult =
retCode.value
}

if (verbose) {
val commandWithArgs = "$command ${args.joinToString(separator = " ")}"
val returnCodeShifted = (returnCode shr 8)
println("-----> Command \"$commandWithArgs\" returned with code $returnCodeShifted")

val outputLines = output?.lines()
if (outputLines != null && outputLines.size > 1) {
println("<----- output:")
outputLines.forEach {
println(it)
}
} else {
println("<----- output: $output")
}

error?.let {
println("<----- error: $it")
}
}

ProcessResult(returnCode, output, error)
}

Expand Down

0 comments on commit b213576

Please # to comment.