Skip to content

Commit 7fc872d

Browse files
authored
fix(android): getCommandOutput exceptions (#2414)
1 parent 509e49c commit 7fc872d

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

packages/cli-platform-android/native_modules.gradle

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import groovy.json.JsonSlurper
2+
import groovy.transform.CompileStatic
23
import org.gradle.initialization.DefaultSettings
34
import org.apache.tools.ant.taskdefs.condition.Os
45

@@ -389,28 +390,37 @@ class ReactNativeModules {
389390
}
390391

391392
/**
392-
* Runs a specified command using providers.exec in a specified directory.
393-
* Throws when the command result is empty.
394-
*/
393+
* Runs a specified command using ProcessBuilder in a specified directory.
394+
* Throws an exception if the command fails or produces an empty output.
395+
*
396+
* @param command The command to execute as an array of strings.
397+
* @param directory The directory in which to execute the command.
398+
* @return The trimmed output of the command.
399+
* @throws Exception if the command fails or produces an empty output.
400+
*/
401+
@CompileStatic
395402
String getCommandOutput(String[] command, File directory) {
396-
try {
397-
def execOutput = providers.exec {
398-
commandLine(command)
399-
workingDir(directory)
400-
}
401-
def output = execOutput.standardOutput.asText.get().trim()
402-
if (!output) {
403-
this.logger.error("${LOG_PREFIX}Unexpected empty result of running '${command}' command.")
404-
def error = execOutput.standardError.asText.get().trim()
405-
throw new Exception(error)
403+
try {
404+
def process = new ProcessBuilder(command as String[])
405+
.directory(directory)
406+
.redirectErrorStream(true)
407+
.start()
408+
int exitCode = process.waitFor()
409+
def output = process.inputStream.text.trim()
410+
if (exitCode != 0) {
411+
throw new Exception("Command '${command}' failed with exit code ${exitCode}.")
412+
}
413+
if (!output) {
414+
throw new Exception("Empty output received from command '${command}'.")
415+
}
416+
return output
417+
} catch (Exception e) {
418+
println "Error executing command '${command}': ${e.message}"
419+
throw e
406420
}
407-
return output
408-
} catch (Exception exception) {
409-
this.logger.error("${LOG_PREFIX}Running '${command}' command failed.")
410-
throw exception
411-
}
412421
}
413422

423+
414424
/**
415425
* Runs a process to call the React Native CLI Config command and parses the output
416426
*/

0 commit comments

Comments
 (0)