mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-17 17:41:31 +08:00
Polish "Make sure project descriptions line-wrap at a decent length"
See gh-966
This commit is contained in:
@@ -44,6 +44,10 @@
|
|||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-compress</artifactId>
|
<artifactId>commons-compress</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-text</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.cache</groupId>
|
<groupId>javax.cache</groupId>
|
||||||
@@ -114,11 +118,6 @@
|
|||||||
<artifactId>jsonassert</artifactId>
|
<artifactId>jsonassert</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-text</artifactId>
|
|
||||||
<version>1.7</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -46,12 +46,19 @@ public class CommandLineHelpGenerator {
|
|||||||
+ " \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )\n" + " ' |____| .__|_| |_|_| |_\\__, | / / / /\n"
|
+ " \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )\n" + " ' |____| .__|_| |_|_| |_\\__, | / / / /\n"
|
||||||
+ " =========|_|==============|___/=/_/_/_/";
|
+ " =========|_|==============|___/=/_/_/_/";
|
||||||
|
|
||||||
|
private static final String NEW_LINE = System.getProperty("line.separator");
|
||||||
|
|
||||||
private final TemplateRenderer template;
|
private final TemplateRenderer template;
|
||||||
|
|
||||||
private final int desiredWidth = 100;
|
private final int maxColumnWidth;
|
||||||
|
|
||||||
public CommandLineHelpGenerator(TemplateRenderer template) {
|
public CommandLineHelpGenerator(TemplateRenderer template) {
|
||||||
|
this(template, 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineHelpGenerator(TemplateRenderer template, int maxColumnWidth) {
|
||||||
this.template = template;
|
this.template = template;
|
||||||
|
this.maxColumnWidth = maxColumnWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,7 +86,6 @@ public class CommandLineHelpGenerator {
|
|||||||
Map<String, Object> model = initializeCommandLineModel(metadata, serviceUrl);
|
Map<String, Object> model = initializeCommandLineModel(metadata, serviceUrl);
|
||||||
model.put("examples", this.template.render("cli/curl-examples", model));
|
model.put("examples", this.template.render("cli/curl-examples", model));
|
||||||
model.put("hasExamples", true);
|
model.put("hasExamples", true);
|
||||||
|
|
||||||
return this.template.render("cli/cli-capabilities", model);
|
return this.template.render("cli/cli-capabilities", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +141,7 @@ public class CommandLineHelpGenerator {
|
|||||||
data[2] = (String) defaults.get(id);
|
data[2] = (String) defaults.get(id);
|
||||||
parameterTable[i++] = data;
|
parameterTable[i++] = data;
|
||||||
}
|
}
|
||||||
model.put("parameters", TableGenerator.generate(parameterTable, false, this.desiredWidth));
|
model.put("parameters", TableGenerator.generate(parameterTable, false, this.maxColumnWidth));
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
@@ -159,7 +165,7 @@ public class CommandLineHelpGenerator {
|
|||||||
data[2] = (String) defaults.get(id);
|
data[2] = (String) defaults.get(id);
|
||||||
parameterTable[i++] = data;
|
parameterTable[i++] = data;
|
||||||
}
|
}
|
||||||
model.put("parameters", TableGenerator.generate(parameterTable, false, this.desiredWidth));
|
model.put("parameters", TableGenerator.generate(parameterTable, false, this.maxColumnWidth));
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +181,7 @@ public class CommandLineHelpGenerator {
|
|||||||
data[2] = dep.getVersionRequirement();
|
data[2] = dep.getVersionRequirement();
|
||||||
dependencyTable[i++] = data;
|
dependencyTable[i++] = data;
|
||||||
}
|
}
|
||||||
return TableGenerator.generate(dependencyTable, true, this.desiredWidth);
|
return TableGenerator.generate(dependencyTable, true, this.maxColumnWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String generateTypeTable(InitializrMetadata metadata, String linkHeader, boolean addTags) {
|
protected String generateTypeTable(InitializrMetadata metadata, String linkHeader, boolean addTags) {
|
||||||
@@ -197,7 +203,7 @@ public class CommandLineHelpGenerator {
|
|||||||
}
|
}
|
||||||
typeTable[i++] = data;
|
typeTable[i++] = data;
|
||||||
}
|
}
|
||||||
return TableGenerator.generate(typeTable, false, this.desiredWidth);
|
return TableGenerator.generate(typeTable, false, this.maxColumnWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<String, Object> buildParametersDescription(InitializrMetadata metadata) {
|
protected Map<String, Object> buildParametersDescription(InitializrMetadata metadata) {
|
||||||
@@ -228,27 +234,25 @@ public class CommandLineHelpGenerator {
|
|||||||
*/
|
*/
|
||||||
private static class TableGenerator {
|
private static class TableGenerator {
|
||||||
|
|
||||||
static final String NEW_LINE = System.getProperty("line.separator");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a table description for the specified {@code content}.
|
* Generate a table description for the specified {@code content}.
|
||||||
* <p>
|
* <p>
|
||||||
* The {@code content} is a two-dimensional array holding the rows of the table.
|
* The {@code content} is a two-dimensional array holding the rows of the table.
|
||||||
* The first entry holds the header of the table.
|
* The first entry holds the header of the table.
|
||||||
* @param content the table content
|
* @param content the table content
|
||||||
* @param emptyRowSeparation add an empty row separator
|
* @param emptyRow add an empty row separator
|
||||||
* @param desiredWidth the width bound for each column
|
* @param maxWidth the width bound for each column
|
||||||
* @return the generated table
|
* @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();
|
StringBuilder sb = new StringBuilder();
|
||||||
int[] columnsLength = computeColumnsLength(content, desiredWidth);
|
int[] columnsLength = computeColumnsLength(content, maxWidth);
|
||||||
appendTableSeparation(sb, columnsLength);
|
appendTableSeparation(sb, columnsLength);
|
||||||
appendRow(sb, content, columnsLength, 0, desiredWidth); // Headers
|
appendRow(sb, content, columnsLength, 0, maxWidth); // Headers
|
||||||
appendTableSeparation(sb, columnsLength);
|
appendTableSeparation(sb, columnsLength);
|
||||||
for (int i = 1; i < content.length; i++) {
|
for (int i = 1; i < content.length; i++) {
|
||||||
appendRow(sb, content, columnsLength, i, desiredWidth);
|
appendRow(sb, content, columnsLength, i, maxWidth);
|
||||||
if (emptyRowSeparation && i < content.length - 1) {
|
if (emptyRow && i < content.length - 1) {
|
||||||
appendEmptyRow(sb, columnsLength);
|
appendEmptyRow(sb, columnsLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,10 +261,9 @@ public class CommandLineHelpGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void appendRow(StringBuilder sb, String[][] content, int[] columnsLength, int rowIndex,
|
private static void appendRow(StringBuilder sb, String[][] content, int[] columnsLength, int rowIndex,
|
||||||
int desiredWidth) {
|
int maxWidth) {
|
||||||
String[] line = content[rowIndex];
|
String[] line = content[rowIndex];
|
||||||
List<String[]> rows = HelpFormatter.format(line, desiredWidth);
|
List<String[]> rows = HelpFormatter.format(line, maxWidth);
|
||||||
if (rows != null) {
|
|
||||||
for (String[] row : rows) {
|
for (String[] row : rows) {
|
||||||
for (int i = 0; i < row.length; i++) {
|
for (int i = 0; i < row.length; i++) {
|
||||||
sb.append("| ").append(fill(row[i], columnsLength[i])).append(" ");
|
sb.append("| ").append(fill(row[i], columnsLength[i])).append(" ");
|
||||||
@@ -269,7 +272,6 @@ public class CommandLineHelpGenerator {
|
|||||||
sb.append(NEW_LINE);
|
sb.append(NEW_LINE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void appendEmptyRow(StringBuilder sb, int[] columnsLength) {
|
private static void appendEmptyRow(StringBuilder sb, int[] columnsLength) {
|
||||||
for (int columnLength : columnsLength) {
|
for (int columnLength : columnsLength) {
|
||||||
@@ -305,16 +307,16 @@ public class CommandLineHelpGenerator {
|
|||||||
return s.toString();
|
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 count = content[0].length;
|
||||||
int[] result = new int[count];
|
int[] result = new int[count];
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
result[i] = largest(content, i, desiredWidth);
|
result[i] = largest(content, i, maxWidth);
|
||||||
}
|
}
|
||||||
return result;
|
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;
|
int max = 0;
|
||||||
for (String[] rows : content) {
|
for (String[] rows : content) {
|
||||||
if (rows != null) {
|
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 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 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.
|
* @return the formatted rows.
|
||||||
*/
|
*/
|
||||||
private static List<String[]> format(String[] content, int desiredWidth) {
|
private static List<String[]> format(String[] content, int maxWidth) {
|
||||||
List<String[]> columns = lineWrap(content, desiredWidth);
|
List<String[]> columns = lineWrap(content, maxWidth);
|
||||||
List<String[]> rows = new ArrayList<>();
|
List<String[]> rows = new ArrayList<>();
|
||||||
for (int i = 0; i < largest(columns); ++i) {
|
for (int i = 0; i < largest(columns); ++i) {
|
||||||
rows.add(computeRow(columns, i));
|
rows.add(computeRow(columns, i));
|
||||||
@@ -358,14 +358,14 @@ public class CommandLineHelpGenerator {
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String[]> lineWrap(String[] content, int desiredWidth) {
|
private static List<String[]> lineWrap(String[] content, int maxWidth) {
|
||||||
List<String[]> lineWrapped = new ArrayList<>();
|
List<String[]> lineWrapped = new ArrayList<>();
|
||||||
for (String column : content) {
|
for (String column : content) {
|
||||||
if (column == null) {
|
if (column == null) {
|
||||||
lineWrapped.add(new String[0]);
|
lineWrapped.add(new String[0]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lineWrapped.add(WordUtils.wrap(column, desiredWidth).split(NEW_LINE));
|
lineWrapped.add(WordUtils.wrap(column, maxWidth).split(NEW_LINE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lineWrapped;
|
return lineWrapped;
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ package io.spring.initializr.web.support;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
|
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
|
||||||
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
|
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
|
||||||
import io.spring.initializr.metadata.Dependency;
|
import io.spring.initializr.metadata.Dependency;
|
||||||
import io.spring.initializr.metadata.InitializrMetadata;
|
import io.spring.initializr.metadata.InitializrMetadata;
|
||||||
import io.spring.initializr.metadata.Type;
|
import io.spring.initializr.metadata.Type;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@@ -36,12 +36,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
*/
|
*/
|
||||||
class CommandLineHelpGeneratorTests {
|
class CommandLineHelpGeneratorTests {
|
||||||
|
|
||||||
private CommandLineHelpGenerator generator;
|
private static final MustacheTemplateRenderer template = new MustacheTemplateRenderer("classpath:/templates");
|
||||||
|
|
||||||
@BeforeEach
|
private CommandLineHelpGenerator generator = new CommandLineHelpGenerator(template);
|
||||||
void init() {
|
|
||||||
this.generator = new CommandLineHelpGenerator(new MustacheTemplateRenderer("classpath:/templates"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateGenericCapabilities() throws IOException {
|
void generateGenericCapabilities() throws IOException {
|
||||||
@@ -95,17 +92,37 @@ class CommandLineHelpGeneratorTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateGeneralCapabilitiesWithLineWrap() throws IOException {
|
void generateGeneralCapabilitiesWithDefaultLineWrap() throws IOException {
|
||||||
|
CommandLineHelpGenerator lineWrapTemplateGenerator = new CommandLineHelpGenerator(template);
|
||||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||||
.addDependencyGroup("test", createDependency("id-b",
|
.addDependencyGroup("test", createDependency("id-a", "Short description"), 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."))
|
"Version control for your database so you can migrate from any version (incl. an empty database) to the latest version of the schema."))
|
||||||
.build();
|
.build();
|
||||||
String content = this.generator.generateGenericCapabilities(metadata, "https://fake-service");
|
String content = lineWrapTemplateGenerator.generateGenericCapabilities(metadata, "https://fake-service");
|
||||||
assertCommandLineCapabilities(content);
|
assertCommandLineCapabilities(content);
|
||||||
assertThat(content).contains(
|
assertThat(readAllLines(content)).containsSequence(
|
||||||
"id-b | Ratpack is a set of Java libraries that facilitate fast, efficient, evolvable and well tested HTTP |");
|
"| id-a | Short description | |",
|
||||||
assertThat(content).contains(
|
"| | | |",
|
||||||
" | applications. Built on Netty the event-driven networking engine. |");
|
"| 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");
|
assertThat(content).contains("https://fake-service");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,4 +192,9 @@ class CommandLineHelpGeneratorTests {
|
|||||||
return dependency;
|
return dependency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<String> readAllLines(String source) {
|
||||||
|
String[] lines = source.split("\\r?\\n");
|
||||||
|
return Arrays.asList(lines);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
6
pom.xml
6
pom.xml
@@ -42,6 +42,7 @@
|
|||||||
<main.basedir>${basedir}</main.basedir>
|
<main.basedir>${basedir}</main.basedir>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<commons-compress.version>1.18</commons-compress.version>
|
<commons-compress.version>1.18</commons-compress.version>
|
||||||
|
<commons-text.version>1.7</commons-text.version>
|
||||||
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
<junit-jupiter.version>5.4.2</junit-jupiter.version>
|
||||||
<maven.version>3.6.1</maven.version>
|
<maven.version>3.6.1</maven.version>
|
||||||
<maven-resolver.version>1.3.3</maven-resolver.version>
|
<maven-resolver.version>1.3.3</maven-resolver.version>
|
||||||
@@ -111,6 +112,11 @@
|
|||||||
<artifactId>commons-compress</artifactId>
|
<artifactId>commons-compress</artifactId>
|
||||||
<version>${commons-compress.version}</version>
|
<version>${commons-compress.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-text</artifactId>
|
||||||
|
<version>${commons-text.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven</groupId>
|
<groupId>org.apache.maven</groupId>
|
||||||
<artifactId>maven-resolver-provider</artifactId>
|
<artifactId>maven-resolver-provider</artifactId>
|
||||||
|
|||||||
Reference in New Issue
Block a user