diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/LogEvent.java b/cli/src/main/java/com/devonfw/tools/ide/process/OutputMessage.java similarity index 79% rename from cli/src/main/java/com/devonfw/tools/ide/process/LogEvent.java rename to cli/src/main/java/com/devonfw/tools/ide/process/OutputMessage.java index 055f81c68..7683427bb 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/LogEvent.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/OutputMessage.java @@ -6,6 +6,6 @@ * @param error A boolean flag that indicates whether the log event represents and error or standard output * @param message A string containing the log message */ -public record LogEvent(boolean error, String message) { +public record OutputMessage(boolean error, String message) { } diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java index 488491ce0..13e113827 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java @@ -168,18 +168,16 @@ public ProcessResult run(ProcessMode processMode) { this.processBuilder.command(args); - List logs = new ArrayList<>(); - List out = null; - List err = null; + List output = new ArrayList<>(); Process process = this.processBuilder.start(); try { if (processMode == ProcessMode.DEFAULT_CAPTURE) { - CompletableFuture> outFut = readInputStream(process.getInputStream()); - CompletableFuture> errFut = readInputStream(process.getErrorStream()); - out = outFut.get(); - err = errFut.get(); + CompletableFuture> outFut = readInputStream(process.getInputStream(), false, output); + CompletableFuture> errFut = readInputStream(process.getErrorStream(), true, output); + outFut.get(); + errFut.get(); } int exitCode; @@ -190,7 +188,7 @@ public ProcessResult run(ProcessMode processMode) { exitCode = process.waitFor(); } - ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, out, err); + ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, output); performLogging(result, exitCode, interpreter); @@ -221,7 +219,7 @@ public ProcessResult run(ProcessMode processMode) { * @param is {@link InputStream}. * @return {@link CompletableFuture}. */ - private static CompletableFuture> readInputStream(InputStream is, boolean errorStream, List logs) { + private static CompletableFuture> readInputStream(InputStream is, boolean errorStream, List logs) { return CompletableFuture.supplyAsync(() -> { @@ -230,8 +228,8 @@ private static CompletableFuture> readInputStream(InputStream is, b String line; while ((line = br.readLine()) != null) { synchronized (logs) { - LogEvent logEvent = new LogEvent(errorStream, line); - logs.add(logEvent); + OutputMessage outputMessage = new OutputMessage(errorStream, line); + logs.add(outputMessage); } } return br.lines().toList(); diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java index 84420d2be..99b49e0f7 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessResultImpl.java @@ -3,6 +3,7 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; import com.devonfw.tools.ide.cli.CliProcessException; import com.devonfw.tools.ide.context.IdeContext; @@ -23,7 +24,7 @@ public class ProcessResultImpl implements ProcessResult { private final List err; - private final List logEvents; + private final List outputMessages; /** * The constructor. @@ -31,19 +32,17 @@ public class ProcessResultImpl implements ProcessResult { * @param executable the {@link #getExecutable() executable}. * @param command the {@link #getCommand() command}. * @param exitCode the {@link #getExitCode() exit code}. - * @param out the {@link #getOut() out}. - * @param err the {@link #getErr() err}. - * @param logEvents the {@link #getLogEvents()} () logEvents}. + * @param output {@link #getOutputMessages() output Messages}. */ - public ProcessResultImpl(String executable, String command, int exitCode, List out, List err) { + public ProcessResultImpl(String executable, String command, int exitCode, List output) { super(); this.executable = executable; this.command = command; this.exitCode = exitCode; - this.out = Objects.requireNonNullElse(out, Collections.emptyList()); - this.err = Objects.requireNonNullElse(err, Collections.emptyList()); - this.logEvents = Objects.requireNonNullElse(logEvents, Collections.emptyList()); + this.outputMessages = Objects.requireNonNullElse(output, Collections.emptyList()); + this.out = this.outputMessages.stream().filter(outputMessage -> !outputMessage.error()).map(OutputMessage::message).collect(Collectors.toList()); + this.err = this.outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList()); } @Override @@ -67,18 +66,18 @@ public int getExitCode() { @Override public List getOut() { - return this.out; + return outputMessages.stream().filter(output -> !output.error()).map(OutputMessage::message).collect(Collectors.toList()); } @Override public List getErr() { - return this.err; + return outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList()); } - public List getLogEvents() { + public List getOutputMessages() { - return this.logEvents; + return this.outputMessages; } @Override diff --git a/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java b/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java index 5c891d257..1ea375e22 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java +++ b/cli/src/test/java/com/devonfw/tools/ide/context/ProcessContextGitMock.java @@ -7,7 +7,9 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import com.devonfw.tools.ide.process.OutputMessage; import com.devonfw.tools.ide.process.ProcessContext; import com.devonfw.tools.ide.process.ProcessErrorHandling; import com.devonfw.tools.ide.process.ProcessMode; @@ -21,9 +23,7 @@ public class ProcessContextGitMock implements ProcessContext { private final List arguments; - private final List errors; - - private final List outs; + private final List outputMessages; private final LocalDateTime now; @@ -37,8 +37,7 @@ public class ProcessContextGitMock implements ProcessContext { public ProcessContextGitMock(Path directory) { this.arguments = new ArrayList<>(); - this.errors = new ArrayList<>(); - this.outs = new ArrayList<>(); + this.outputMessages = new ArrayList<>(); this.exitCode = ProcessResult.SUCCESS; this.directory = directory; this.now = LocalDateTime.now(); @@ -65,7 +64,8 @@ public void setExitCode(int exitCode) { */ public List getErrors() { - return errors; + return this.outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).collect(Collectors.toList()); + } /** @@ -73,7 +73,8 @@ public List getErrors() { */ public List getOuts() { - return outs; + return this.outputMessages.stream().filter(outputMessage -> !outputMessage.error()).map(OutputMessage::message).collect(Collectors.toList()); + } @Override @@ -139,7 +140,8 @@ public ProcessResult run(ProcessMode processMode) { // part of git cleanup checks if a new directory 'new-folder' exists if (this.arguments.contains("ls-files")) { if (Files.exists(this.directory.resolve("new-folder"))) { - this.outs.add("new-folder"); + OutputMessage outputMessage = new OutputMessage(false, "new-folder"); + this.outputMessages.add(outputMessage); this.exitCode = 0; } } @@ -181,7 +183,7 @@ public ProcessResult run(ProcessMode processMode) { } } this.arguments.clear(); - return new ProcessResultImpl("git", command.toString(), this.exitCode, this.outs, this.errors); + return new ProcessResultImpl("git", command.toString(), this.exitCode, this.outputMessages); } }