From 1d05647db0db98685308fc888c62a7fc61eefce4 Mon Sep 17 00:00:00 2001 From: Besmir Beqiri Date: Tue, 23 Apr 2024 14:31:41 +0200 Subject: [PATCH] Add more `runAsync` methods to CommandRunner class in order to handle more use cases --- .../platform/internal/util/CommandRunner.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/internal/util/src/main/java/one/jpro/platform/internal/util/CommandRunner.java b/internal/util/src/main/java/one/jpro/platform/internal/util/CommandRunner.java index a133e3be..8585ee76 100644 --- a/internal/util/src/main/java/one/jpro/platform/internal/util/CommandRunner.java +++ b/internal/util/src/main/java/one/jpro/platform/internal/util/CommandRunner.java @@ -152,26 +152,52 @@ public int run(String processName) throws IOException, InterruptedException { */ public int run(String processName, File workingDirectory) throws IOException, InterruptedException { Process process = setupProcess(processName, workingDirectory); - Thread logThread = mergeProcessOutput(process.getInputStream()); + Thread mergeOutputThread = mergeProcessOutput(process.getInputStream()); int result = process.waitFor(); - logThread.join(); + mergeOutputThread.join(); logger.debug("Result for {}: {}", processName, result); if (result != 0) logger.error("Process {} failed with result: {}", processName, result); return result; } + /** + * Runs a process asynchronously with a given set of command line arguments. + * By default, it merges the output of the process. + * + * @param processName the name of the process + * @return the {@link Process} object + * @throws IOException if an I/O error occurs + */ + public Process runAsync(String processName) throws IOException { + return runAsync(processName, null, true); + } + /** * Runs a process asynchronously with a given set of command line arguments, in a given - * working directory. + * working directory. By default, it merges the output of the process. * * @param processName the name of the process * @param workingDirectory a file with the working directory of the process - * @return the process object + * @return the {@link Process} object * @throws IOException if an I/O error occurs */ public Process runAsync(String processName, File workingDirectory) throws IOException { + return runAsync(processName, workingDirectory, true); + } + + /** + * Runs a process asynchronously with a given set of command line arguments, in a given + * working directory. + * + * @param processName the name of the process + * @param workingDirectory a file with the working directory of the process + * @param mergeOutput a boolean that sets the merge output mode + * @return the {@link Process} object + * @throws IOException if an I/O error occurs + */ + public Process runAsync(String processName, File workingDirectory, boolean mergeOutput) throws IOException { Process process = setupProcess(processName, workingDirectory); - mergeProcessOutput(process.getInputStream()); + if (mergeOutput) mergeProcessOutput(process.getInputStream()); return process; }