From bc4d2e617675a5c4e2b341a47fd3089027fdae72 Mon Sep 17 00:00:00 2001 From: Peter Palaga Date: Mon, 8 Mar 2021 23:17:13 +0100 Subject: [PATCH] Leverage Maven's -Dstyle.color to avoid coloring instead of stripping the ASCII codes in the client --- .../mvndaemon/mvnd/client/DefaultClient.java | 30 +++++++------------ .../mvndaemon/mvnd/common/Environment.java | 11 +++++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java b/client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java index b2b728b1..0c088ee5 100644 --- a/client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java +++ b/client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java @@ -41,6 +41,7 @@ import org.mvndaemon.mvnd.common.DaemonException; import org.mvndaemon.mvnd.common.DaemonInfo; import org.mvndaemon.mvnd.common.DaemonRegistry; import org.mvndaemon.mvnd.common.Environment; +import org.mvndaemon.mvnd.common.Environment.Color; import org.mvndaemon.mvnd.common.Message; import org.mvndaemon.mvnd.common.Message.BuildException; import org.mvndaemon.mvnd.common.Message.BuildFinished; @@ -83,25 +84,16 @@ public class DefaultClient implements Client { || Environment.COMPLETION.hasCommandLineOption(args); // Color - String styleColor = Environment.MAVEN_COLOR.getCommandLineOption(args); - if ("always".equals(styleColor) || "never".equals(styleColor)) { - /* If the user knows what he wants, pass his preference unchanged to the daemon */ - } else if ("auto".equals(styleColor)) { - /* Do not pass auto to daemon, we handle it below */ - Environment.MAVEN_COLOR.removeCommandLineOption(args); - styleColor = null; - } else if (styleColor != null) { - throw new IllegalArgumentException("Invalid color configuration option [" + styleColor - + "]. Supported values are (auto|always|never)."); - } - - /* stdout is not a terminal e.g. when stdout is redirected to a file */ - final boolean stdoutIsTerminal = CLibrary.isatty(1) != 0; - if (styleColor == null) { - Environment.MAVEN_COLOR.addCommandLineOption( - args, - (batchMode || logFile != null || !stdoutIsTerminal) ? "never" : "always"); + Color styleColor = Color.of(Environment.MAVEN_COLOR.removeCommandLineOption(args)).orElse(Color.auto); + if (styleColor == Color.auto) { + /* Translate from auto to either always or never */ + /* stdout is not a terminal e.g. when stdout is redirected to a file */ + final boolean stdoutIsTerminal = CLibrary.isatty(1) != 0; + styleColor = (batchMode || logFile != null || !stdoutIsTerminal) ? Color.never : Color.always; } + /* We cannot use Environment.addCommandLineOption() because that one would pass --color to the daemon + * and --color is not supported there yet. */ + args.add("-D" + Environment.MAVEN_COLOR.getProperty() + "=" + styleColor.name()); // System properties setSystemPropertiesFromCommandLine(args); @@ -114,7 +106,7 @@ public class DefaultClient implements Client { } int exitCode = 0; - boolean noBuffering = batchMode || parameters.noBuffering() || !stdoutIsTerminal; + boolean noBuffering = batchMode || parameters.noBuffering(); try (TerminalOutput output = new TerminalOutput(noBuffering, parameters.rollingWindowSize(), logFile)) { try { final ExecutionResult result = new DefaultClient(parameters).execute(output, args); diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/Environment.java b/common/src/main/java/org/mvndaemon/mvnd/common/Environment.java index 3e2d9cce..e3898564 100644 --- a/common/src/main/java/org/mvndaemon/mvnd/common/Environment.java +++ b/common/src/main/java/org/mvndaemon/mvnd/common/Environment.java @@ -473,6 +473,17 @@ public enum Environment { } } + /** + * The values of {@link Environment#MAVEN_COLOR} option. + */ + public enum Color { + always, never, auto; + + public static Optional of(String color) { + return color == null ? Optional.empty() : Optional.of(Color.valueOf(color)); + } + } + public static class DocumentedEnumEntry { private final E entry;