Skip to content

Commit

Permalink
[MNG-8600] Allow Terminal and raw streams customization (#2134)
Browse files Browse the repository at this point in the history
Without disturbing the control flow.

---

https://issues.apache.org/jira/browse/MNG-8600
  • Loading branch information
cstamas authored Feb 28, 2025
1 parent 2018b89 commit 176349c
Showing 1 changed file with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,29 +300,15 @@ 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 =
new ProjectBuildLogAppender(determineBuildEventListener(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();
Expand All @@ -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:
Expand All @@ -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);
Expand Down

0 comments on commit 176349c

Please sign in to comment.