Skip to content

Commit

Permalink
Highlight plugin prefix in buildplan:list
Browse files Browse the repository at this point in the history
Respecting convention xxx-maven-plugin and maven-xxx-plugin xxx is now displayed
with the Maven mojo color style when available.

It should be easier to spot the "important" name portion for a plugin.

Fixes #143, closes #155
  • Loading branch information
jcgay committed Nov 22, 2022
1 parent da69aae commit 1436b5f
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 27 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
<version>1.15.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/codehaus/mojo/buildplan/ListMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public void executeInternal() throws MojoFailureException {
descriptor.hideLifecycle();
}
String row = descriptor.rowFormat();
String head = descriptor.titleFormat();

StringBuilder output = new StringBuilder()
.append(lineSeparator())
.append(titleSeparator(descriptor))
.append(lineSeparator())
.append(tableHead(row))
.append(tableHead(head))
.append(lineSeparator())
.append(titleSeparator(descriptor));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.maven.lifecycle.DefaultLifecycles;
import org.apache.maven.lifecycle.Lifecycle;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.codehaus.mojo.buildplan.util.LinkedMultimap;
import org.codehaus.mojo.buildplan.util.Multimap;

Expand All @@ -36,7 +35,7 @@ protected static Map<TableColumn, Integer> findMaxSize(
Map<TableColumn, Integer> result = new EnumMap<>(TableColumn.class);

Multimap<TableColumn, Integer> count = new LinkedMultimap<>();
for (MojoExecution execution : executions) {
executions.stream().map(MojoExecutionDisplay::new).forEach(execution -> {
for (TableColumn column : columns) {
switch (column) {
case ARTIFACT_ID:
Expand All @@ -49,18 +48,18 @@ protected static Map<TableColumn, Integer> findMaxSize(
count.put(column, safeLength(execution.getGoal()));
break;
case PHASE:
count.put(column, safeLength(phase(execution)));
count.put(column, safeLength(execution.getPhase()));
break;
case LIFECYCLE:
Lifecycle lifecycle = defaultLifecycles.get(phase(execution));
Lifecycle lifecycle = defaultLifecycles.get(execution.getPhase());
count.put(column, lifecycle == null ? 0 : safeLength(lifecycle.getId()));
break;
case VERSION:
count.put(column, safeLength(execution.getVersion()));
break;
}
}
}
});
for (TableColumn column : TableColumn.values()) {
count.put(column, column.title().length());
}
Expand All @@ -72,14 +71,6 @@ protected static Map<TableColumn, Integer> findMaxSize(
return result;
}

public static String phase(MojoExecution execution) {
MojoDescriptor mojoDescriptor = execution.getMojoDescriptor();
if (mojoDescriptor != null && mojoDescriptor.getPhase() != null) {
return mojoDescriptor.getPhase();
}
return execution.getLifecyclePhase();
}

private static int safeLength(String string) {
return defaultString(string).length();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/
package org.codehaus.mojo.buildplan.display;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.StringJoiner;
import org.apache.maven.lifecycle.DefaultLifecycles;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.shared.utils.logging.MessageUtils;

public class ListTableDescriptor extends AbstractTableDescriptor {

static final int SEPARATOR_SIZE = 5 * SEPARATOR.length();
private static final int ANSI_COLOR_CODES_LENGTH =
MessageUtils.buffer().mojo("").toString().length();

private int pluginSize;
private int versionSize;
Expand All @@ -46,6 +49,10 @@ public static ListTableDescriptor of(Collection<MojoExecution> executions, Defau
}

public String rowFormat() {
return columns(getPluginSize());
}

private String columns(int pluginSize) {
StringBuilder builder = new StringBuilder();
if (lifecycleSize > 0) {
builder.append(FORMAT_LEFT_ALIGN)
Expand All @@ -58,7 +65,7 @@ public String rowFormat() {
.append(FORMAT_STRING)
.append(SEPARATOR)
.append(FORMAT_LEFT_ALIGN)
.append(getPluginSize())
.append(pluginSize)
.append(FORMAT_STRING)
.append(SEPARATOR)
.append(FORMAT_LEFT_ALIGN)
Expand All @@ -76,14 +83,26 @@ public String rowFormat() {
return builder.toString();
}

public String titleFormat() {
if (MessageUtils.isColorEnabled()) {
return columns(getPluginSize() - ANSI_COLOR_CODES_LENGTH);
}
return columns(getPluginSize());
}

public int width() {
return getPluginSize()
+ getVersionSize()
+ getPhaseSize()
+ getLifecycleSize()
+ getExecutionIdSize()
+ getGoalSize()
+ SEPARATOR_SIZE;
return withSeparator(
getPluginSize(),
getVersionSize(),
getPhaseSize(),
getLifecycleSize(),
getExecutionIdSize(),
getGoalSize());
}

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;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2012 Jean-Christophe Gay (contact@jeanchristophegay.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.mojo.buildplan.display;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.shared.utils.logging.MessageUtils;

enum MavenPluginPatterns {
IN_FIRST("(.*)-maven-plugin"),
IN_THE_MIDDLE("maven-(.*)-plugin");

private final Pattern pattern;

MavenPluginPatterns(String pattern) {
this.pattern = Pattern.compile(pattern);
}

static String mayHighlightPrefix(String artifactId) {
return Arrays.stream(values())
.map(p -> p.pattern.matcher(artifactId))
.filter(Matcher::matches)
.findFirst()
.map(matcher -> new StringBuilder(artifactId)
.replace(
matcher.start(1),
matcher.end(1),
MessageUtils.buffer().mojo(matcher.group(1)).toString())
.toString())
.orElse(artifactId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.maven.lifecycle.DefaultLifecycles;
import org.apache.maven.lifecycle.Lifecycle;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;

public class MojoExecutionDisplay {

Expand All @@ -30,6 +31,10 @@ public MojoExecutionDisplay(MojoExecution execution) {
}

public String getPhase() {
MojoDescriptor mojoDescriptor = execution.getMojoDescriptor();
if (mojoDescriptor != null && mojoDescriptor.getPhase() != null) {
return mojoDescriptor.getPhase();
}
return defaultString(execution.getLifecyclePhase());
}

Expand All @@ -39,7 +44,10 @@ public String getLifecycle(DefaultLifecycles defaultLifecycles) {
}

public String getArtifactId() {
return defaultString(execution.getArtifactId());
if (execution.getArtifactId() == null) {
return "";
}
return MavenPluginPatterns.mayHighlightPrefix(execution.getArtifactId());
}

public String getVersion() {
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/codehaus/mojo/buildplan/ListIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.codehaus.mojo.buildplan;

import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;
import static org.fusesource.jansi.Ansi.ansi;

import com.soebes.itf.jupiter.extension.MavenGoal;
import com.soebes.itf.jupiter.extension.MavenJupiterExtension;
Expand All @@ -29,6 +30,67 @@
@MavenJupiterExtension
class ListIT {

@Nested
@MavenProject
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:list")
class SimpleProject {

@MavenTest
@SystemProperty(value = "style.color", content = "always")
void list_with_color_activated(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.plain()
.containsSequence(
"-------------------------------------------------------------------------------------------------",
"PHASE | PLUGIN | VERSION | GOAL | EXECUTION ID ",
"-------------------------------------------------------------------------------------------------",
"process-resources | maven-" + green("resources")
+ "-plugin | 2.6 | resources | default-resources ",
"compile | maven-" + green("compiler")
+ "-plugin | 3.1 | compile | default-compile ",
"process-test-resources | maven-" + green("resources")
+ "-plugin | 2.6 | testResources | default-testResources",
"test-compile | maven-" + green("compiler")
+ "-plugin | 3.1 | testCompile | default-testCompile ",
"test | maven-" + green("surefire")
+ "-plugin | 2.12.4 | test | default-test ",
"package | maven-" + green("jar")
+ "-plugin | 2.4 | jar | default-jar ",
"install | maven-" + green("install")
+ "-plugin | 2.4 | install | default-install ",
"deploy | maven-" + green("deploy")
+ "-plugin | 2.7 | deploy | default-deploy ");
}

@MavenTest
@SystemProperty(value = "style.color", content = "never")
void list_with_color_deactivated(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.plain()
.containsSequence(
"[INFO] Build Plan for list-simple-project: ",
"-------------------------------------------------------------------------------------------------",
"PHASE | PLUGIN | VERSION | GOAL | EXECUTION ID ",
"-------------------------------------------------------------------------------------------------",
"process-resources | maven-resources-plugin | 2.6 | resources | default-resources ",
"compile | maven-compiler-plugin | 3.1 | compile | default-compile ",
"process-test-resources | maven-resources-plugin | 2.6 | testResources | default-testResources",
"test-compile | maven-compiler-plugin | 3.1 | testCompile | default-testCompile ",
"test | maven-surefire-plugin | 2.12.4 | test | default-test ",
"package | maven-jar-plugin | 2.4 | jar | default-jar ",
"install | maven-install-plugin | 2.4 | install | default-install ",
"deploy | maven-deploy-plugin | 2.7 | deploy | default-deploy ");
}

private String green(String text) {
return ansi().fgGreen().a(text).reset().toString();
}
}

@Nested
@MavenProject
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:list")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ void should_add_all_size_and_the_separator_size_to_get_descriptor_width() {
.setExecutionIdSize(1)
.setGoalSize(2)
.setPluginSize(3)
.setPhaseSize(4);
.setPhaseSize(4)
.setVersionSize(5);

assertThat(descriptor.width()).isEqualTo(10 + ListTableDescriptor.SEPARATOR_SIZE);
assertThat(descriptor.width())
.as("Each column size + (size needed for separators - ansi color code size)")
.isEqualTo(1 + 2 + 3 + 4 + 5 + (5 * TableDescriptor.SEPARATOR.length() - 8));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.codehaus.mojo.buildplan.model.builder.MojoExecutionBuilder.aMojoExecution;

import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.junit.jupiter.api.Test;

class MojoExecutionDisplayTest {
Expand Down Expand Up @@ -65,7 +66,7 @@ void should_get_mojo_lifecycle_phase_when_descriptor_phase_is_null() {
MojoExecution execution = aMojoExecution()
.withArtifactId("plugin-a")
.withLifecyclePhase("phase-a")
.withDescriptorPhase("phase-b")
.withDescriptorPhase(null)
.withExecutionId("execution-id-a")
.withGoal("goal-a")
.build();
Expand All @@ -74,4 +75,46 @@ void should_get_mojo_lifecycle_phase_when_descriptor_phase_is_null() {

assertThat(result.getPhase()).isEqualTo("phase-a");
}

@Test
void should_highlight_mojo_for_mojo_pattern() {
MojoExecution execution =
aMojoExecution().withArtifactId("maven-jar-plugin").build();

MojoExecutionDisplay result = new MojoExecutionDisplay(execution);

assertThat(result.getArtifactId())
.isEqualTo("maven-" + MessageUtils.buffer().mojo("jar") + "-plugin");
}

@Test
void should_highlight_longer_mojo_for_mojo_pattern() {
MojoExecution execution =
aMojoExecution().withArtifactId("maven-surefire-report-plugin").build();

MojoExecutionDisplay result = new MojoExecutionDisplay(execution);

assertThat(result.getArtifactId())
.isEqualTo("maven-" + MessageUtils.buffer().mojo("surefire-report") + "-plugin");
}

@Test
void should_highlight_mojo_for_another_mojo_pattern() {
MojoExecution execution =
aMojoExecution().withArtifactId("versions-maven-plugin").build();

MojoExecutionDisplay result = new MojoExecutionDisplay(execution);

assertThat(result.getArtifactId()).isEqualTo(MessageUtils.buffer().mojo("versions") + "-maven-plugin");
}

@Test
void should_highlight_longer_mojo_for_another_mojo_pattern() {
MojoExecution execution =
aMojoExecution().withArtifactId("versions-report-maven-plugin").build();

MojoExecutionDisplay result = new MojoExecutionDisplay(execution);

assertThat(result.getArtifactId()).isEqualTo(MessageUtils.buffer().mojo("versions-report") + "-maven-plugin");
}
}
Loading

0 comments on commit 1436b5f

Please sign in to comment.