Skip to content

Commit

Permalink
Add property 'mvnd.preserveProjectLog' to not decorate the stdout/std…
Browse files Browse the repository at this point in the history
…err stream
  • Loading branch information
gzm55 committed Jan 11, 2023
1 parent f3c336a commit 0233a8c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public static void main(String[] argv) throws Exception {

int exitCode = 0;
boolean noBuffering = batchMode || parameters.noBuffering();
try (TerminalOutput output = new TerminalOutput(noBuffering, parameters.rollingWindowSize(), logFile)) {
try (TerminalOutput output = new TerminalOutput(
noBuffering, parameters.rollingWindowSize(), logFile, parameters.preserveProjectLog())) {
try {
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
exitCode = result.getExitCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ public boolean serial() {
return value(Environment.SERIAL).orSystemProperty().orDefault().asBoolean();
}

/**
* @return <code>true</code> if the stdout/stderr project log should be preserved.
*/
public boolean preserveProjectLog() {
return value(Environment.MVND_PRESERVE_PROJECT_LOG)
.orSystemProperty()
.orDefault()
.asBoolean();
}

/**
* @param newUserDir where to change the current directory to
* @return a new {@link DaemonParameters} with {@code userDir} set to the given {@code newUserDir}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public enum Environment {
MAVEN_DEFINE(null, null, null, OptionType.STRING, Flags.INTERNAL, "mvn:-D", "mvn:--define"),
/** Whether the output should be styled using ANSI color codes; possible values: auto, always, never */
MAVEN_COLOR("style.color", null, "auto", OptionType.STRING, Flags.OPTIONAL, "mvnd:--color"),
/** Keep stdout/stderr log undecorated when not in --raw-streams mode. */
MVND_PRESERVE_PROJECT_LOG("mvnd.preserveProjectLog", null, Boolean.FALSE, OptionType.BOOLEAN, Flags.OPTIONAL),

//
// mvnd properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -96,6 +97,8 @@ public class TerminalOutput implements ClientOutput {
private static final AttributedStyle GREEN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.GREEN);
private static final AttributedStyle CYAN_FOREGROUND = new AttributedStyle().foreground(AttributedStyle.CYAN);

private static final Pattern PROJECT_LOG_DECO_PATTERN = Pattern.compile("^\\[[^\\]]*\\] \\[(stdout|stderr)\\] ");

private final Terminal terminal;
private final Terminal.SignalHandler previousIntHandler;
private final Display display;
Expand Down Expand Up @@ -140,6 +143,7 @@ public class TerminalOutput implements ClientOutput {
private String buildStatus;
private boolean displayDone = false;
private boolean noBuffering;
private boolean preserveProjectLog;

/**
* {@link Project} is owned by the display loop thread and is accessed only from there. Therefore it does not need
Expand All @@ -155,7 +159,8 @@ public Project(String id) {
}
}

public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile) throws IOException {
public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile, boolean preserveProjectLog)
throws IOException {
this.start = System.currentTimeMillis();
TerminalBuilder builder = TerminalBuilder.builder();
boolean outRedirected = CLibrary.isatty(1) == 0;
Expand All @@ -165,6 +170,7 @@ public TerminalOutput(boolean noBuffering, int rollingWindowSize, Path logFile)
this.terminal = builder.build();
this.dumb = terminal.getType().startsWith("dumb");
this.noBuffering = noBuffering;
this.preserveProjectLog = preserveProjectLog;
this.linesPerProject = rollingWindowSize;
terminal.enterRawMode();
Thread mainThread = Thread.currentThread();
Expand Down Expand Up @@ -380,18 +386,19 @@ private boolean doAccept(Message entry) {
case Message.PROJECT_LOG_MESSAGE: {
final ProjectEvent bm = (ProjectEvent) entry;
final Project prj = projects.get(bm.getProjectId());
String msg = bm.getMessage();
if (preserveProjectLog) {
msg = PROJECT_LOG_DECO_PATTERN.matcher(msg).replaceFirst("");
}
if (prj == null) {
log.accept(bm.getMessage());
log.accept(msg);
} else if (noBuffering || dumb) {
String msg;
if (maxThreads > 1) {
msg = String.format("[%s] %s", bm.getProjectId(), bm.getMessage());
} else {
msg = bm.getMessage();
msg = String.format("[%s] %s", bm.getProjectId(), msg);
}
log.accept(msg);
} else {
prj.log.add(bm.getMessage());
prj.log.add(msg);
}
break;
}
Expand Down

0 comments on commit 0233a8c

Please sign in to comment.