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.
  • Loading branch information
jcgay committed Nov 10, 2022
1 parent 6cc881e commit 19418ea
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 26 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 @@ -54,12 +54,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 @@ -20,12 +20,10 @@
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
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,11 +34,12 @@ protected static Map<TableColumn, Integer> findMaxSize(Collection<MojoExecution>
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:
count.put(column, safeLength(execution.getArtifactId()));
count.put(column,
safeLength(execution.getArtifactId()));
break;
case EXECUTION_ID:
count.put(column, safeLength(execution.getExecutionId()));
Expand All @@ -49,10 +48,10 @@ protected static Map<TableColumn, Integer> findMaxSize(Collection<MojoExecution>
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:
Expand All @@ -61,7 +60,7 @@ protected static Map<TableColumn, Integer> findMaxSize(Collection<MojoExecution>

}
}
}
});
for (TableColumn column : TableColumn.values()) {
count.put(column, column.title().length());
}
Expand All @@ -73,14 +72,6 @@ protected static Map<TableColumn, Integer> findMaxSize(Collection<MojoExecution>
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,17 @@
*/
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 @@ -45,14 +47,18 @@ 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).append(getLifecycleSize()).append(FORMAT_STRING)
.append(SEPARATOR);
}
builder.append(FORMAT_LEFT_ALIGN).append(getPhaseSize()).append(FORMAT_STRING)
.append(SEPARATOR)
.append(FORMAT_LEFT_ALIGN).append(getPluginSize()).append(FORMAT_STRING)
.append(FORMAT_LEFT_ALIGN).append(pluginSize).append(FORMAT_STRING)
.append(SEPARATOR)
.append(FORMAT_LEFT_ALIGN).append(getVersionSize()).append(FORMAT_STRING)
.append(SEPARATOR);
Expand All @@ -63,8 +69,20 @@ 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,43 @@
/*
* 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 All @@ -53,4 +61,5 @@ public String getGoal() {
public String getExecutionId() {
return defaultString(execution.getExecutionId());
}

}
54 changes: 54 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,59 @@
@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 @@ -49,9 +49,12 @@ void should_add_all_size_and_the_separator_size_to_get_descriptor_width() {
ListTableDescriptor descriptor = new ListTableDescriptor().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 @@ -15,12 +15,13 @@
*/
package org.codehaus.mojo.buildplan.display;

import org.apache.maven.plugin.MojoExecution;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
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 {

@Test
Expand Down Expand Up @@ -62,7 +63,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 @@ -71,4 +72,40 @@ 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 19418ea

Please sign in to comment.