buildParametersDescription(InitializrMetadata metadata) {
@@ -228,27 +234,25 @@ public class CommandLineHelpGenerator {
*/
private static class TableGenerator {
- static final String NEW_LINE = System.getProperty("line.separator");
-
/**
* Generate a table description for the specified {@code content}.
*
* The {@code content} is a two-dimensional array holding the rows of the table.
* The first entry holds the header of the table.
* @param content the table content
- * @param emptyRowSeparation add an empty row separator
- * @param desiredWidth the width bound for each column
+ * @param emptyRow add an empty row separator
+ * @param maxWidth the width bound for each column
* @return the generated table
*/
- static String generate(String[][] content, boolean emptyRowSeparation, int desiredWidth) {
+ static String generate(String[][] content, boolean emptyRow, int maxWidth) {
StringBuilder sb = new StringBuilder();
- int[] columnsLength = computeColumnsLength(content, desiredWidth);
+ int[] columnsLength = computeColumnsLength(content, maxWidth);
appendTableSeparation(sb, columnsLength);
- appendRow(sb, content, columnsLength, 0, desiredWidth); // Headers
+ appendRow(sb, content, columnsLength, 0, maxWidth); // Headers
appendTableSeparation(sb, columnsLength);
for (int i = 1; i < content.length; i++) {
- appendRow(sb, content, columnsLength, i, desiredWidth);
- if (emptyRowSeparation && i < content.length - 1) {
+ appendRow(sb, content, columnsLength, i, maxWidth);
+ if (emptyRow && i < content.length - 1) {
appendEmptyRow(sb, columnsLength);
}
}
@@ -257,17 +261,15 @@ public class CommandLineHelpGenerator {
}
private static void appendRow(StringBuilder sb, String[][] content, int[] columnsLength, int rowIndex,
- int desiredWidth) {
+ int maxWidth) {
String[] line = content[rowIndex];
- List rows = HelpFormatter.format(line, desiredWidth);
- if (rows != null) {
- for (String[] row : rows) {
- for (int i = 0; i < row.length; i++) {
- sb.append("| ").append(fill(row[i], columnsLength[i])).append(" ");
- }
- sb.append("|");
- sb.append(NEW_LINE);
+ List rows = HelpFormatter.format(line, maxWidth);
+ for (String[] row : rows) {
+ for (int i = 0; i < row.length; i++) {
+ sb.append("| ").append(fill(row[i], columnsLength[i])).append(" ");
}
+ sb.append("|");
+ sb.append(NEW_LINE);
}
}
@@ -305,16 +307,16 @@ public class CommandLineHelpGenerator {
return s.toString();
}
- private static int[] computeColumnsLength(String[][] content, int desiredWidth) {
+ private static int[] computeColumnsLength(String[][] content, int maxWidth) {
int count = content[0].length;
int[] result = new int[count];
for (int i = 0; i < count; i++) {
- result[i] = largest(content, i, desiredWidth);
+ result[i] = largest(content, i, maxWidth);
}
return result;
}
- private static int largest(String[][] content, int column, int desiredWidth) {
+ private static int largest(String[][] content, int column, int maxWidth) {
int max = 0;
for (String[] rows : content) {
if (rows != null) {
@@ -324,23 +326,21 @@ public class CommandLineHelpGenerator {
}
}
}
- return (max < desiredWidth) ? max : desiredWidth;
+ return Math.min(max, maxWidth);
}
}
private static class HelpFormatter {
- private static final String NEW_LINE = System.getProperty("line.separator");
-
/**
- * Formats a given content to a desired width.
+ * Formats a given content to a max width.
* @param content the content to format.
- * @param desiredWidth the desired width of each column
+ * @param maxWidth the max width of each column
* @return the formatted rows.
*/
- private static List format(String[] content, int desiredWidth) {
- List columns = lineWrap(content, desiredWidth);
+ private static List format(String[] content, int maxWidth) {
+ List columns = lineWrap(content, maxWidth);
List rows = new ArrayList<>();
for (int i = 0; i < largest(columns); ++i) {
rows.add(computeRow(columns, i));
@@ -358,14 +358,14 @@ public class CommandLineHelpGenerator {
return line;
}
- private static List lineWrap(String[] content, int desiredWidth) {
+ private static List lineWrap(String[] content, int maxWidth) {
List lineWrapped = new ArrayList<>();
for (String column : content) {
if (column == null) {
lineWrapped.add(new String[0]);
}
else {
- lineWrapped.add(WordUtils.wrap(column, desiredWidth).split(NEW_LINE));
+ lineWrapped.add(WordUtils.wrap(column, maxWidth).split(NEW_LINE));
}
}
return lineWrapped;
diff --git a/initializr-web/src/test/java/io/spring/initializr/web/support/CommandLineHelpGeneratorTests.java b/initializr-web/src/test/java/io/spring/initializr/web/support/CommandLineHelpGeneratorTests.java
index 777f3b37..724eb5c5 100755
--- a/initializr-web/src/test/java/io/spring/initializr/web/support/CommandLineHelpGeneratorTests.java
+++ b/initializr-web/src/test/java/io/spring/initializr/web/support/CommandLineHelpGeneratorTests.java
@@ -18,13 +18,13 @@ package io.spring.initializr.web.support;
import java.io.IOException;
import java.util.Arrays;
+import java.util.List;
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.Type;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,12 +36,9 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class CommandLineHelpGeneratorTests {
- private CommandLineHelpGenerator generator;
+ private static final MustacheTemplateRenderer template = new MustacheTemplateRenderer("classpath:/templates");
- @BeforeEach
- void init() {
- this.generator = new CommandLineHelpGenerator(new MustacheTemplateRenderer("classpath:/templates"));
- }
+ private CommandLineHelpGenerator generator = new CommandLineHelpGenerator(template);
@Test
void generateGenericCapabilities() throws IOException {
@@ -95,17 +92,37 @@ class CommandLineHelpGeneratorTests {
}
@Test
- void generateGeneralCapabilitiesWithLineWrap() throws IOException {
+ void generateGeneralCapabilitiesWithDefaultLineWrap() throws IOException {
+ CommandLineHelpGenerator lineWrapTemplateGenerator = new CommandLineHelpGenerator(template);
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
- .addDependencyGroup("test", createDependency("id-b",
- "Ratpack is a set of Java libraries that facilitate fast, efficient, evolvable and well tested HTTP applications. Built on Netty the event-driven networking engine."))
+ .addDependencyGroup("test", createDependency("id-a", "Short description"), createDependency("id-b",
+ "Version control for your database so you can migrate from any version (incl. an empty database) to the latest version of the schema."))
.build();
- String content = this.generator.generateGenericCapabilities(metadata, "https://fake-service");
+ String content = lineWrapTemplateGenerator.generateGenericCapabilities(metadata, "https://fake-service");
assertCommandLineCapabilities(content);
- assertThat(content).contains(
- "id-b | Ratpack is a set of Java libraries that facilitate fast, efficient, evolvable and well tested HTTP |");
- assertThat(content).contains(
- " | applications. Built on Netty the event-driven networking engine. |");
+ assertThat(readAllLines(content)).containsSequence(
+ "| id-a | Short description | |",
+ "| | | |",
+ "| id-b | Version control for your database so you can migrate from | |",
+ "| | any version (incl. an empty database) to the latest version | |",
+ "| | of the schema. | |");
+ assertThat(content).contains("https://fake-service");
+ }
+
+ @Test
+ void generateGeneralCapabilitiesWithCustomLineWrap() throws IOException {
+ CommandLineHelpGenerator lineWrapTemplateGenerator = new CommandLineHelpGenerator(template, 100);
+ InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
+ .addDependencyGroup("test", createDependency("id-a", "Short description"), createDependency("id-b",
+ "Version control for your database so you can migrate from any version (incl. an empty database) to the latest version of the schema."))
+ .build();
+ String content = lineWrapTemplateGenerator.generateGenericCapabilities(metadata, "https://fake-service");
+ assertCommandLineCapabilities(content);
+ assertThat(readAllLines(content)).containsSequence(
+ "| id-a | Short description | |",
+ "| | | |",
+ "| id-b | Version control for your database so you can migrate from any version (incl. an empty database) to | |",
+ "| | the latest version of the schema. | |");
assertThat(content).contains("https://fake-service");
}
@@ -175,4 +192,9 @@ class CommandLineHelpGeneratorTests {
return dependency;
}
+ private static List readAllLines(String source) {
+ String[] lines = source.split("\\r?\\n");
+ return Arrays.asList(lines);
+ }
+
}
diff --git a/pom.xml b/pom.xml
index 96f81a66..6bcf3c06 100644
--- a/pom.xml
+++ b/pom.xml
@@ -42,6 +42,7 @@
${basedir}
UTF-8
1.18
+ 1.7
5.4.2
3.6.1
1.3.3
@@ -111,6 +112,11 @@
commons-compress
${commons-compress.version}
+
+ org.apache.commons
+ commons-text
+ ${commons-text.version}
+
org.apache.maven
maven-resolver-provider