You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2025/02/28 13:16:51 UTC

(maven) branch master updated: [MNG-8600] Allow Terminal and raw streams customization (#2134)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 176349c3bf [MNG-8600] Allow Terminal and raw streams customization (#2134)
176349c3bf is described below

commit 176349c3bf0b99097713aade4d20b20a345458ba
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Fri Feb 28 14:16:46 2025 +0100

    [MNG-8600] Allow Terminal and raw streams customization (#2134)
    
    Without disturbing the control flow.
    
    ---
    
    https://issues.apache.org/jira/browse/MNG-8600
---
 .../apache/maven/cling/invoker/LookupInvoker.java  | 84 ++++++++++++++--------
 1 file changed, 55 insertions(+), 29 deletions(-)

diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java
index 5ab134f19d..abd90ebe9f 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java
@@ -300,7 +300,7 @@ protected BuildEventListener doDetermineBuildEventListener(C context) {
         return new SimpleBuildEventListener(writer);
     }
 
-    protected void createTerminal(C context) {
+    protected final void createTerminal(C context) {
         if (context.terminal == null) {
             // Create the build log appender; also sets MavenSimpleLogger sink
             ProjectBuildLogAppender projectBuildLogAppender =
@@ -308,21 +308,7 @@ protected void createTerminal(C context) {
             context.closeables.add(projectBuildLogAppender);
 
             MessageUtils.systemInstall(
-                    builder -> {
-                        if (context.invokerRequest.embedded()) {
-                            InputStream in = context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
-                            OutputStream out = context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
-                            builder.streams(in, out);
-                            builder.provider(TerminalBuilder.PROP_PROVIDER_EXEC);
-                            context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false;
-                            context.closeables.add(out::flush);
-                        } else {
-                            builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
-                        }
-                        if (context.coloredOutput != null) {
-                            builder.color(context.coloredOutput);
-                        }
-                    },
+                    builder -> doCreateTerminal(context, builder),
                     terminal -> doConfigureWithTerminal(context, terminal));
 
             context.terminal = MessageUtils.getTerminal();
@@ -333,7 +319,31 @@ protected void createTerminal(C context) {
         }
     }
 
-    protected void doConfigureWithTerminal(C context, Terminal terminal) {
+    /**
+     * Override this method to create Terminal as you want.
+     *
+     * @see #createTerminal(LookupContext)
+     */
+    protected void doCreateTerminal(C context, TerminalBuilder builder) {
+        if (context.invokerRequest.embedded()) {
+            InputStream in = context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
+            OutputStream out = context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
+            builder.streams(in, out);
+            builder.provider(TerminalBuilder.PROP_PROVIDER_EXEC);
+            context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false;
+            context.closeables.add(out::flush);
+        } else {
+            builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
+        }
+        if (context.coloredOutput != null) {
+            builder.color(context.coloredOutput);
+        }
+    }
+
+    /**
+     * Called from {@link #createTerminal(LookupContext)} when Terminal was built.
+     */
+    protected final void doConfigureWithTerminal(C context, Terminal terminal) {
         context.terminal = terminal;
         Options options = context.invokerRequest.options();
         // tricky thing: align what JLine3 detected and Maven thinks:
@@ -344,21 +354,37 @@ protected void doConfigureWithTerminal(C context, Terminal terminal) {
         // not be not colored (good), but Maven will print out "Message scheme: color".
         MessageUtils.setColorEnabled(
                 context.coloredOutput != null ? context.coloredOutput : !Terminal.TYPE_DUMB.equals(terminal.getType()));
-        if (!options.rawStreams().orElse(false)) {
-            MavenSimpleLogger stdout = (MavenSimpleLogger) context.loggerFactory.getLogger("stdout");
-            MavenSimpleLogger stderr = (MavenSimpleLogger) context.loggerFactory.getLogger("stderr");
-            stdout.setLogLevel(LocationAwareLogger.INFO_INT);
-            stderr.setLogLevel(LocationAwareLogger.INFO_INT);
-            PrintStream psOut = new LoggingOutputStream(s -> stdout.info("[stdout] " + s)).printStream();
-            context.closeables.add(() -> LoggingOutputStream.forceFlush(psOut));
-            PrintStream psErr = new LoggingOutputStream(s -> stderr.warn("[stderr] " + s)).printStream();
-            context.closeables.add(() -> LoggingOutputStream.forceFlush(psErr));
-            System.setOut(psOut);
-            System.setErr(psErr);
-            // no need to set them back, this is already handled by MessageUtils.systemUninstall() above
+
+        // handle rawStreams: some would like to act on true, some on false
+        if (options.rawStreams().orElse(false)) {
+            doConfigureWithTerminalWithRawStreamsEnabled(context);
+        } else {
+            doConfigureWithTerminalWithRawStreamsDisabled(context);
         }
     }
 
+    /**
+     * Override this method to add some special handling for "raw streams" <em>enabled</em> option.
+     */
+    protected void doConfigureWithTerminalWithRawStreamsEnabled(C context) {}
+
+    /**
+     * Override this method to add some special handling for "raw streams" <em>disabled</em> option.
+     */
+    protected void doConfigureWithTerminalWithRawStreamsDisabled(C context) {
+        MavenSimpleLogger stdout = (MavenSimpleLogger) context.loggerFactory.getLogger("stdout");
+        MavenSimpleLogger stderr = (MavenSimpleLogger) context.loggerFactory.getLogger("stderr");
+        stdout.setLogLevel(LocationAwareLogger.INFO_INT);
+        stderr.setLogLevel(LocationAwareLogger.INFO_INT);
+        PrintStream psOut = new LoggingOutputStream(s -> stdout.info("[stdout] " + s)).printStream();
+        context.closeables.add(() -> LoggingOutputStream.forceFlush(psOut));
+        PrintStream psErr = new LoggingOutputStream(s -> stderr.warn("[stderr] " + s)).printStream();
+        context.closeables.add(() -> LoggingOutputStream.forceFlush(psErr));
+        System.setOut(psOut);
+        System.setErr(psErr);
+        // no need to set them back, this is already handled by MessageUtils.systemUninstall() above
+    }
+
     protected Consumer<String> determineWriter(C context) {
         if (context.writer == null) {
             context.writer = doDetermineWriter(context);