Skip to content

Commit 176349c

Browse files
authored
[MNG-8600] Allow Terminal and raw streams customization (#2134)
Without disturbing the control flow. --- https://issues.apache.org/jira/browse/MNG-8600
1 parent 2018b89 commit 176349c

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -300,29 +300,15 @@ protected BuildEventListener doDetermineBuildEventListener(C context) {
300300
return new SimpleBuildEventListener(writer);
301301
}
302302

303-
protected void createTerminal(C context) {
303+
protected final void createTerminal(C context) {
304304
if (context.terminal == null) {
305305
// Create the build log appender; also sets MavenSimpleLogger sink
306306
ProjectBuildLogAppender projectBuildLogAppender =
307307
new ProjectBuildLogAppender(determineBuildEventListener(context));
308308
context.closeables.add(projectBuildLogAppender);
309309

310310
MessageUtils.systemInstall(
311-
builder -> {
312-
if (context.invokerRequest.embedded()) {
313-
InputStream in = context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
314-
OutputStream out = context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
315-
builder.streams(in, out);
316-
builder.provider(TerminalBuilder.PROP_PROVIDER_EXEC);
317-
context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false;
318-
context.closeables.add(out::flush);
319-
} else {
320-
builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
321-
}
322-
if (context.coloredOutput != null) {
323-
builder.color(context.coloredOutput);
324-
}
325-
},
311+
builder -> doCreateTerminal(context, builder),
326312
terminal -> doConfigureWithTerminal(context, terminal));
327313

328314
context.terminal = MessageUtils.getTerminal();
@@ -333,7 +319,31 @@ protected void createTerminal(C context) {
333319
}
334320
}
335321

336-
protected void doConfigureWithTerminal(C context, Terminal terminal) {
322+
/**
323+
* Override this method to create Terminal as you want.
324+
*
325+
* @see #createTerminal(LookupContext)
326+
*/
327+
protected void doCreateTerminal(C context, TerminalBuilder builder) {
328+
if (context.invokerRequest.embedded()) {
329+
InputStream in = context.invokerRequest.stdIn().orElse(InputStream.nullInputStream());
330+
OutputStream out = context.invokerRequest.stdOut().orElse(OutputStream.nullOutputStream());
331+
builder.streams(in, out);
332+
builder.provider(TerminalBuilder.PROP_PROVIDER_EXEC);
333+
context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false;
334+
context.closeables.add(out::flush);
335+
} else {
336+
builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
337+
}
338+
if (context.coloredOutput != null) {
339+
builder.color(context.coloredOutput);
340+
}
341+
}
342+
343+
/**
344+
* Called from {@link #createTerminal(LookupContext)} when Terminal was built.
345+
*/
346+
protected final void doConfigureWithTerminal(C context, Terminal terminal) {
337347
context.terminal = terminal;
338348
Options options = context.invokerRequest.options();
339349
// tricky thing: align what JLine3 detected and Maven thinks:
@@ -344,21 +354,37 @@ protected void doConfigureWithTerminal(C context, Terminal terminal) {
344354
// not be not colored (good), but Maven will print out "Message scheme: color".
345355
MessageUtils.setColorEnabled(
346356
context.coloredOutput != null ? context.coloredOutput : !Terminal.TYPE_DUMB.equals(terminal.getType()));
347-
if (!options.rawStreams().orElse(false)) {
348-
MavenSimpleLogger stdout = (MavenSimpleLogger) context.loggerFactory.getLogger("stdout");
349-
MavenSimpleLogger stderr = (MavenSimpleLogger) context.loggerFactory.getLogger("stderr");
350-
stdout.setLogLevel(LocationAwareLogger.INFO_INT);
351-
stderr.setLogLevel(LocationAwareLogger.INFO_INT);
352-
PrintStream psOut = new LoggingOutputStream(s -> stdout.info("[stdout] " + s)).printStream();
353-
context.closeables.add(() -> LoggingOutputStream.forceFlush(psOut));
354-
PrintStream psErr = new LoggingOutputStream(s -> stderr.warn("[stderr] " + s)).printStream();
355-
context.closeables.add(() -> LoggingOutputStream.forceFlush(psErr));
356-
System.setOut(psOut);
357-
System.setErr(psErr);
358-
// no need to set them back, this is already handled by MessageUtils.systemUninstall() above
357+
358+
// handle rawStreams: some would like to act on true, some on false
359+
if (options.rawStreams().orElse(false)) {
360+
doConfigureWithTerminalWithRawStreamsEnabled(context);
361+
} else {
362+
doConfigureWithTerminalWithRawStreamsDisabled(context);
359363
}
360364
}
361365

366+
/**
367+
* Override this method to add some special handling for "raw streams" <em>enabled</em> option.
368+
*/
369+
protected void doConfigureWithTerminalWithRawStreamsEnabled(C context) {}
370+
371+
/**
372+
* Override this method to add some special handling for "raw streams" <em>disabled</em> option.
373+
*/
374+
protected void doConfigureWithTerminalWithRawStreamsDisabled(C context) {
375+
MavenSimpleLogger stdout = (MavenSimpleLogger) context.loggerFactory.getLogger("stdout");
376+
MavenSimpleLogger stderr = (MavenSimpleLogger) context.loggerFactory.getLogger("stderr");
377+
stdout.setLogLevel(LocationAwareLogger.INFO_INT);
378+
stderr.setLogLevel(LocationAwareLogger.INFO_INT);
379+
PrintStream psOut = new LoggingOutputStream(s -> stdout.info("[stdout] " + s)).printStream();
380+
context.closeables.add(() -> LoggingOutputStream.forceFlush(psOut));
381+
PrintStream psErr = new LoggingOutputStream(s -> stderr.warn("[stderr] " + s)).printStream();
382+
context.closeables.add(() -> LoggingOutputStream.forceFlush(psErr));
383+
System.setOut(psOut);
384+
System.setErr(psErr);
385+
// no need to set them back, this is already handled by MessageUtils.systemUninstall() above
386+
}
387+
362388
protected Consumer<String> determineWriter(C context) {
363389
if (context.writer == null) {
364390
context.writer = doDetermineWriter(context);

0 commit comments

Comments
 (0)