From 216c054c70052695d36bb3705c65fa239c773b09 Mon Sep 17 00:00:00 2001 From: vlaw Date: Thu, 12 Oct 2023 18:51:21 +0800 Subject: [PATCH] fix: Do not output ANSI escape if outputFile is enabled Fix #189 --- .../mojo/buildplan/AbstractLifecycleMojo.java | 2 +- .../java/org/codehaus/mojo/buildplan/ListMojo.java | 8 +++++++- .../org/codehaus/mojo/buildplan/ListPhaseMojo.java | 5 ++++- .../buildplan/display/ListTableDescriptor.java | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/buildplan/AbstractLifecycleMojo.java b/src/main/java/org/codehaus/mojo/buildplan/AbstractLifecycleMojo.java index 8719a51..6e5dc8c 100644 --- a/src/main/java/org/codehaus/mojo/buildplan/AbstractLifecycleMojo.java +++ b/src/main/java/org/codehaus/mojo/buildplan/AbstractLifecycleMojo.java @@ -50,7 +50,7 @@ public abstract class AbstractLifecycleMojo extends AbstractMojo { /** Allow to specify an output file to bypass console output */ @Parameter(property = "buildplan.outputFile") - private File outputFile; + protected File outputFile; /** Allow to specify appending to the output file */ @Parameter(property = "buildplan.appendOutput", defaultValue = "false") diff --git a/src/main/java/org/codehaus/mojo/buildplan/ListMojo.java b/src/main/java/org/codehaus/mojo/buildplan/ListMojo.java index e26569e..cbd23ea 100644 --- a/src/main/java/org/codehaus/mojo/buildplan/ListMojo.java +++ b/src/main/java/org/codehaus/mojo/buildplan/ListMojo.java @@ -16,6 +16,7 @@ package org.codehaus.mojo.buildplan; import static java.lang.System.lineSeparator; +import static java.util.Objects.isNull; import static org.codehaus.mojo.buildplan.display.TableColumn.ARTIFACT_ID; import static org.codehaus.mojo.buildplan.display.TableColumn.EXECUTION_ID; import static org.codehaus.mojo.buildplan.display.TableColumn.GOAL; @@ -30,6 +31,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.buildplan.display.ListTableDescriptor; import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay; +import org.codehaus.mojo.buildplan.display.PlainTextMojoExecutionDisplay; import org.codehaus.mojo.buildplan.display.TableDescriptor; import org.codehaus.plexus.util.StringUtils; @@ -49,6 +51,9 @@ public void executeInternal() throws MojoFailureException { if (!showLifecycles) { descriptor.hideLifecycle(); } + + descriptor.setFileOutput(!isNull(outputFile)); + String row = descriptor.rowFormat(); String head = descriptor.titleFormat(); @@ -69,7 +74,8 @@ public void executeInternal() throws MojoFailureException { private String tableRow(String row, MojoExecution execution) { - MojoExecutionDisplay display = new MojoExecutionDisplay(execution); + MojoExecutionDisplay display = + isNull(outputFile) ? new MojoExecutionDisplay(execution) : new PlainTextMojoExecutionDisplay(execution); if (showLifecycles) { return String.format( diff --git a/src/main/java/org/codehaus/mojo/buildplan/ListPhaseMojo.java b/src/main/java/org/codehaus/mojo/buildplan/ListPhaseMojo.java index 35c33fb..34839a9 100644 --- a/src/main/java/org/codehaus/mojo/buildplan/ListPhaseMojo.java +++ b/src/main/java/org/codehaus/mojo/buildplan/ListPhaseMojo.java @@ -18,6 +18,7 @@ import static java.lang.System.lineSeparator; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; +import static java.util.Objects.isNull; import java.util.Collection; import java.util.Map; @@ -28,6 +29,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.buildplan.display.ListPhaseTableDescriptor; import org.codehaus.mojo.buildplan.display.MojoExecutionDisplay; +import org.codehaus.mojo.buildplan.display.PlainTextMojoExecutionDisplay; import org.codehaus.mojo.buildplan.display.TableDescriptor; import org.codehaus.mojo.buildplan.util.Multimap; import org.codehaus.plexus.util.StringUtils; @@ -94,7 +96,8 @@ public void executeInternal() throws MojoFailureException { } private String line(String rowFormat, MojoExecution execution) { - MojoExecutionDisplay display = new MojoExecutionDisplay(execution); + MojoExecutionDisplay display = + isNull(outputFile) ? new MojoExecutionDisplay(execution) : new PlainTextMojoExecutionDisplay(execution); return String.format(rowFormat, display.getArtifactId(), display.getGoal(), display.getExecutionId()); } diff --git a/src/main/java/org/codehaus/mojo/buildplan/display/ListTableDescriptor.java b/src/main/java/org/codehaus/mojo/buildplan/display/ListTableDescriptor.java index d07d1b3..968593a 100644 --- a/src/main/java/org/codehaus/mojo/buildplan/display/ListTableDescriptor.java +++ b/src/main/java/org/codehaus/mojo/buildplan/display/ListTableDescriptor.java @@ -35,6 +35,12 @@ public class ListTableDescriptor extends AbstractTableDescriptor { private int executionIdSize; private int goalSize; + private boolean isFileOutput; + + public void setFileOutput(boolean fileOutput) { + isFileOutput = fileOutput; + } + public static ListTableDescriptor of(Collection executions, DefaultLifecycles defaultLifecycles) { Map maxSize = findMaxSize(executions, defaultLifecycles, TableColumn.values()); @@ -48,6 +54,10 @@ public static ListTableDescriptor of(Collection executions, Defau .setExecutionIdSize(maxSize.get(TableColumn.EXECUTION_ID)); } + private boolean isEscCodeEnabled() { + return !isFileOutput && MessageUtils.isColorEnabled(); + } + public String rowFormat() { return columns(getPluginSize()); } @@ -84,7 +94,7 @@ private String columns(int pluginSize) { } public String titleFormat() { - if (MessageUtils.isColorEnabled()) { + if (isEscCodeEnabled()) { return columns(getPluginSize() - ANSI_COLOR_CODES_LENGTH); } return columns(getPluginSize()); @@ -102,7 +112,7 @@ public int width() { private int withSeparator(int... ints) { int width = Arrays.stream(ints).sum() + (ints.length - 1) * SEPARATOR.length(); - return MessageUtils.isColorEnabled() ? width - ANSI_COLOR_CODES_LENGTH : width; + return isEscCodeEnabled() ? width - ANSI_COLOR_CODES_LENGTH : width; } @Override